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_set> |
12 | |
13 | // class unordered_set |
14 | |
15 | // template <class H2, class P2> |
16 | // void merge(unordered_set<key_type, H2, P2, allocator_type>& source); |
17 | // template <class H2, class P2> |
18 | // void merge(unordered_set<key_type, H2, P2, allocator_type>&& source); |
19 | // template <class H2, class P2> |
20 | // void merge(unordered_multiset<key_type, H2, P2, allocator_type>& source); |
21 | // template <class H2, class P2> |
22 | // void merge(unordered_multiset<key_type, H2, P2, allocator_type>&& source); |
23 | |
24 | #include <unordered_set> |
25 | #include <cassert> |
26 | #include "test_macros.h" |
27 | #include "Counter.h" |
28 | |
29 | template <class Set> |
30 | bool set_equal(const Set& set, Set other) |
31 | { |
32 | return set == 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_set<int> src{1, 3, 5}; |
56 | std::unordered_set<int> dst{2, 4, 5}; |
57 | dst.merge(source&: src); |
58 | assert(set_equal(src, {5})); |
59 | assert(set_equal(dst, {1, 2, 3, 4, 5})); |
60 | } |
61 | |
62 | #ifndef TEST_HAS_NO_EXCEPTIONS |
63 | { |
64 | bool do_throw = false; |
65 | typedef std::unordered_set<Counter<int>, throw_hasher<Counter<int>>> set_type; |
66 | set_type src({1, 3, 5}, 0, throw_hasher<Counter<int>>(do_throw)); |
67 | set_type dst({2, 4, 5}, 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(set_equal(src, set_type({1, 3, 5}, 0, throw_hasher<Counter<int>>(do_throw)))); |
82 | assert(set_equal(dst, set_type({2, 4, 5}, 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 { return std::hash<Counter<int>>()(p); } |
99 | }; |
100 | { |
101 | typedef std::unordered_set<Counter<int>, std::hash<Counter<int>>, std::equal_to<Counter<int>>> first_set_type; |
102 | typedef std::unordered_set<Counter<int>, hasher, equal> second_set_type; |
103 | typedef std::unordered_multiset<Counter<int>, hasher, equal> third_set_type; |
104 | |
105 | { |
106 | first_set_type first{1, 2, 3}; |
107 | second_set_type second{2, 3, 4}; |
108 | third_set_type third{1, 3}; |
109 | |
110 | assert(Counter_base::gConstructed == 8); |
111 | |
112 | first.merge(second); |
113 | first.merge(third); |
114 | |
115 | assert(set_equal(first, {1, 2, 3, 4})); |
116 | assert(set_equal(second, {2, 3})); |
117 | assert(set_equal(third, {1, 3})); |
118 | |
119 | assert(Counter_base::gConstructed == 8); |
120 | } |
121 | assert(Counter_base::gConstructed == 0); |
122 | { |
123 | first_set_type first{1, 2, 3}; |
124 | second_set_type second{2, 3, 4}; |
125 | third_set_type third{1, 3}; |
126 | |
127 | assert(Counter_base::gConstructed == 8); |
128 | |
129 | first.merge(std::move(second)); |
130 | first.merge(std::move(third)); |
131 | |
132 | assert(set_equal(first, {1, 2, 3, 4})); |
133 | assert(set_equal(second, {2, 3})); |
134 | assert(set_equal(third, {1, 3})); |
135 | |
136 | assert(Counter_base::gConstructed == 8); |
137 | } |
138 | assert(Counter_base::gConstructed == 0); |
139 | } |
140 | { |
141 | std::unordered_set<int> first; |
142 | { |
143 | std::unordered_set<int> second; |
144 | first.merge(source&: second); |
145 | first.merge(source: std::move(second)); |
146 | } |
147 | { |
148 | std::unordered_multiset<int> second; |
149 | first.merge(source&: second); |
150 | first.merge(source: std::move(second)); |
151 | } |
152 | } |
153 | return 0; |
154 | } |
155 | |