1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10
11// <flat_map>
12
13// flat_map& operator=(const flat_map& m);
14
15#include <deque>
16#include <flat_map>
17#include <functional>
18#include <vector>
19
20#include "test_macros.h"
21#include "../../../test_compare.h"
22#include "test_allocator.h"
23
24int main(int, char**) {
25 {
26 // test_allocator is not propagated
27 using C = test_less<int>;
28 std::vector<int, test_allocator<int>> ks({1, 3, 5}, test_allocator<int>(6));
29 std::vector<char, test_allocator<char>> vs({2, 2, 1}, test_allocator<char>(7));
30 using M = std::flat_map<int, char, C, decltype(ks), decltype(vs)>;
31 auto mo = M(ks, vs, C(5));
32 auto m = M({{3, 3}, {4, 4}, {5, 5}}, C(3), test_allocator<int>(2));
33 m = mo;
34
35 assert(m.key_comp() == C(5));
36 assert(m.keys() == ks);
37 assert(m.values() == vs);
38 assert(m.keys().get_allocator() == test_allocator<int>(2));
39 assert(m.values().get_allocator() == test_allocator<char>(2));
40
41 // mo is unchanged
42 assert(mo.key_comp() == C(5));
43 assert(mo.keys() == ks);
44 assert(mo.values() == vs);
45 assert(mo.keys().get_allocator() == test_allocator<int>(6));
46 assert(mo.values().get_allocator() == test_allocator<char>(7));
47 }
48 {
49 // other_allocator is propagated
50 using C = test_less<int>;
51 using Ks = std::vector<int, other_allocator<int>>;
52 using Vs = std::vector<char, other_allocator<char>>;
53 auto ks = Ks({1, 3, 5}, other_allocator<int>(6));
54 auto vs = Vs({2, 2, 1}, other_allocator<char>(7));
55 using M = std::flat_map<int, char, C, Ks, Vs>;
56 auto mo = M(Ks(ks, other_allocator<int>(6)), Vs(vs, other_allocator<int>(7)), C(5));
57 auto m = M({{3, 3}, {4, 4}, {5, 5}}, C(3), other_allocator<int>(2));
58 m = mo;
59
60 assert(m.key_comp() == C(5));
61 assert(m.keys() == ks);
62 assert(m.values() == vs);
63 assert(m.keys().get_allocator() == other_allocator<int>(6));
64 assert(m.values().get_allocator() == other_allocator<char>(7));
65
66 // mo is unchanged
67 assert(mo.key_comp() == C(5));
68 assert(mo.keys() == ks);
69 assert(mo.values() == vs);
70 assert(mo.keys().get_allocator() == other_allocator<int>(6));
71 assert(mo.values().get_allocator() == other_allocator<char>(7));
72 }
73 {
74 // comparator is copied and invariant is preserved
75 using M = std::flat_map<int, int, std::function<bool(int, int)>>;
76 M mo = M({{1, 2}, {3, 4}}, std::less<int>());
77 M m = M({{1, 2}, {3, 4}}, std::greater<int>());
78 assert(m.key_comp()(2, 1) == true);
79 assert(m != mo);
80 m = mo;
81 assert(m.key_comp()(2, 1) == false);
82 assert(m == mo);
83 }
84 {
85 // self-assignment
86 using M = std::flat_map<int, int>;
87 M m = {{1, 2}, {3, 4}};
88 m = static_cast<const M&>(m);
89 assert((m == M{{1, 2}, {3, 4}}));
90 }
91 return 0;
92}
93

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.pass.cpp