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 |
10 | |
11 | // <unordered_map> |
12 | |
13 | // class unordered_map |
14 | |
15 | // template <class H2, class P2> |
16 | // void merge(unordered_map<key_type, value_type, H2, P2, allocator_type>& source); |
17 | // template <class H2, class P2> |
18 | // void merge(unordered_map<key_type, value_type, H2, P2, allocator_type>&& source); |
19 | // template <class H2, class P2> |
20 | // void merge(unordered_multimap<key_type, value_type, H2, P2, allocator_type>& source); |
21 | // template <class H2, class P2> |
22 | // void merge(unordered_multimap<key_type, value_type, H2, P2, allocator_type>&& source); |
23 | |
24 | #include <unordered_map> |
25 | #include <cassert> |
26 | #include "test_macros.h" |
27 | #include "Counter.h" |
28 | |
29 | template <class Map> |
30 | bool map_equal(const Map& map, Map other) |
31 | { |
32 | return map == other; |
33 | } |
34 | |
35 | #ifndef TEST_HAS_NO_EXCEPTIONS |
36 | template <class T> |
37 | struct throw_hasher |
38 | { |
39 | bool& should_throw_; |
40 | |
41 | throw_hasher(bool& should_throw) : should_throw_(should_throw) {} |
42 | |
43 | std::size_t operator()(const T& p) const |
44 | { |
45 | if (should_throw_) |
46 | throw 0; |
47 | return std::hash<T>()(p); |
48 | } |
49 | }; |
50 | #endif |
51 | |
52 | int main(int, char**) |
53 | { |
54 | { |
55 | std::unordered_map<int, int> src{{1, 0}, {3, 0}, {5, 0}}; |
56 | std::unordered_map<int, int> dst{{2, 0}, {4, 0}, {5, 0}}; |
57 | dst.merge(source&: src); |
58 | assert(map_equal(src, {{5,0}})); |
59 | assert(map_equal(dst, {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}})); |
60 | } |
61 | |
62 | #ifndef TEST_HAS_NO_EXCEPTIONS |
63 | { |
64 | bool do_throw = false; |
65 | typedef std::unordered_map<Counter<int>, int, throw_hasher<Counter<int>>> map_type; |
66 | map_type src({{1, 0}, {3, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw)); |
67 | map_type dst({{2, 0}, {4, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw)); |
68 | |
69 | assert(Counter_base::gConstructed == 6); |
70 | |
71 | do_throw = true; |
72 | try |
73 | { |
74 | dst.merge(src); |
75 | } |
76 | catch (int) |
77 | { |
78 | do_throw = false; |
79 | } |
80 | assert(!do_throw); |
81 | assert(map_equal(src, map_type({{1, 0}, {3, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw)))); |
82 | assert(map_equal(dst, map_type({{2, 0}, {4, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw)))); |
83 | } |
84 | #endif |
85 | assert(Counter_base::gConstructed == 0); |
86 | struct equal |
87 | { |
88 | equal() = default; |
89 | |
90 | bool operator()(const Counter<int>& lhs, const Counter<int>& rhs) const |
91 | { |
92 | return lhs == rhs; |
93 | } |
94 | }; |
95 | struct hasher |
96 | { |
97 | hasher() = default; |
98 | std::size_t operator()(const Counter<int>& p) const |
99 | { |
100 | return std::hash<Counter<int>>()(p); |
101 | } |
102 | }; |
103 | { |
104 | typedef std::unordered_map<Counter<int>, int, std::hash<Counter<int>>, std::equal_to<Counter<int>>> first_map_type; |
105 | typedef std::unordered_map<Counter<int>, int, hasher, equal> second_map_type; |
106 | typedef std::unordered_multimap<Counter<int>, int, hasher, equal> third_map_type; |
107 | |
108 | { |
109 | first_map_type first{{1, 0}, {2, 0}, {3, 0}}; |
110 | second_map_type second{{2, 0}, {3, 0}, {4, 0}}; |
111 | third_map_type third{{1, 0}, {3, 0}}; |
112 | |
113 | assert(Counter_base::gConstructed == 8); |
114 | |
115 | first.merge(second); |
116 | first.merge(third); |
117 | |
118 | assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}})); |
119 | assert(map_equal(second, {{2, 0}, {3, 0}})); |
120 | assert(map_equal(third, {{1, 0}, {3, 0}})); |
121 | |
122 | assert(Counter_base::gConstructed == 8); |
123 | } |
124 | assert(Counter_base::gConstructed == 0); |
125 | { |
126 | first_map_type first{{1, 0}, {2, 0}, {3, 0}}; |
127 | second_map_type second{{2, 0}, {3, 0}, {4, 0}}; |
128 | third_map_type third{{1, 0}, {3, 0}}; |
129 | |
130 | assert(Counter_base::gConstructed == 8); |
131 | |
132 | first.merge(std::move(second)); |
133 | first.merge(std::move(third)); |
134 | |
135 | assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}})); |
136 | assert(map_equal(second, {{2, 0}, {3, 0}})); |
137 | assert(map_equal(third, {{1, 0}, {3, 0}})); |
138 | |
139 | assert(Counter_base::gConstructed == 8); |
140 | } |
141 | assert(Counter_base::gConstructed == 0); |
142 | } |
143 | { |
144 | std::unordered_map<int, int> first; |
145 | { |
146 | std::unordered_map<int, int> second; |
147 | first.merge(source&: second); |
148 | first.merge(source: std::move(second)); |
149 | } |
150 | { |
151 | std::unordered_multimap<int, int> second; |
152 | first.merge(source&: second); |
153 | first.merge(source: std::move(second)); |
154 | } |
155 | } |
156 | return 0; |
157 | } |
158 | |