Refactor ix86_expand_vecop_qihi2.
[gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_set / modifiers / merge.cc
blob9f36359eeb0826661f9a2b95a039ad554f11e484
1 // Copyright (C) 2016-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++17 } }
20 #include <string>
21 #include <unordered_set>
22 #include <algorithm>
23 #include <testsuite_hooks.h>
25 using test_type = std::unordered_set<int>;
27 struct hash {
28 auto operator()(int i) const noexcept { return ~std::hash<int>()(i); }
30 struct equal : std::equal_to<> { };
32 template<typename C1, typename C2>
33 bool equal_elements(const C1& c1, const C2& c2)
35 if (c2.size() != c1.size())
36 return false;
37 for (auto& i : c1)
38 if (c2.count(i) != c1.count(i))
39 return false;
40 return true;
43 void
44 test01()
46 const test_type c0{ 1, 2, 3, };
47 test_type c1 = c0, c2 = c0;
49 c1.merge(c2);
50 VERIFY( c1 == c0 );
51 VERIFY( c2 == c0 );
53 c1.clear();
54 c1.merge(c2);
55 VERIFY( c1 == c0 );
56 VERIFY( c2.empty() );
58 c2.merge(std::move(c1));
59 VERIFY( c1.empty() );
60 VERIFY( c2 == c0 );
63 void
64 test02()
66 const test_type c0{ 1, 2, 3, };
67 test_type c1 = c0;
68 std::unordered_set<int, hash, equal> c2( c0.begin(), c0.end() );
70 c1.merge(c2);
71 VERIFY( c1 == c0 );
72 VERIFY( equal_elements(c2, c0) );
74 c1.clear();
75 c1.merge(c2);
76 VERIFY( c1 == c0 );
77 VERIFY( c2.empty() );
79 c2.merge(c1);
80 VERIFY( c1.empty() );
81 VERIFY( equal_elements(c2, c0) );
83 c1.merge(std::move(c2));
84 VERIFY( c2.empty() );
85 VERIFY( c1 == c0 );
88 void
89 test03()
91 const test_type c0{ 1, 2, 3, };
92 test_type c1 = c0;
93 std::unordered_multiset<int, hash, equal> c2( c0.begin(), c0.end() );
94 c1.merge(c2);
95 VERIFY( c1 == c0 );
96 VERIFY( equal_elements(c2, c0) );
98 c1.clear();
99 c1.merge(c2);
100 VERIFY( c1 == c0 );
101 VERIFY( c2.empty() );
103 c2.merge(c1);
104 VERIFY( c1.empty() );
105 VERIFY( equal_elements(c2, c0) );
107 c1 = c0;
108 c2.merge(c1);
109 VERIFY( c1.empty() );
110 VERIFY( c2.size() == (2 * c0.size()) );
111 VERIFY( c2.count(1) == 2 );
112 VERIFY( c2.count(2) == 2 );
113 VERIFY( c2.count(3) == 2 );
115 c1.merge(c2);
116 VERIFY( c1 == c0 );
117 VERIFY( equal_elements(c2, c0) );
119 c1.merge(std::move(c2));
120 VERIFY( c1 == c0 );
121 VERIFY( equal_elements(c2, c0) );
123 c1.clear();
124 c1.merge(std::move(c2));
125 VERIFY( c1 == c0 );
126 VERIFY( c2.empty() );
129 void
130 test04()
132 const std::unordered_set<std::string> c0{ "abcd", "efgh", "ijkl", };
133 std::unordered_set<std::string> c1 = c0;
134 std::unordered_multiset<std::string> c2( c0.begin(), c0.end() );
135 c1.merge(c2);
136 VERIFY( c1 == c0 );
137 VERIFY( equal_elements(c2, c0) );
139 c1.clear();
140 c1.merge(c2);
141 VERIFY( c1 == c0 );
142 VERIFY( c2.empty() );
144 c2.merge(c1);
145 VERIFY( c1.empty() );
146 VERIFY( equal_elements(c2, c0) );
148 c1 = c0;
149 c2.merge(c1);
150 VERIFY( c1.empty() );
151 VERIFY( c2.size() == (2 * c0.size()) );
152 VERIFY( c2.count("abcd") == 2 );
153 VERIFY( c2.count("efgh") == 2 );
154 VERIFY( c2.count("ijkl") == 2 );
156 c1.merge(c2);
157 VERIFY( c1 == c0 );
158 VERIFY( equal_elements(c2, c0) );
160 c1.merge(std::move(c2));
161 VERIFY( c1 == c0 );
162 VERIFY( equal_elements(c2, c0) );
164 c1.clear();
165 c1.merge(std::move(c2));
166 VERIFY( c1 == c0 );
167 VERIFY( c2.empty() );
170 void
171 test07()
173 test_type c1{ 1, 3, 5 };
174 test_type c2{ 2, 4, 6 };
175 const test_type c3 = c2;
177 c1.merge(c2);
178 VERIFY( c1.size() == 6 );
179 VERIFY( c2.empty() );
181 c2 = c3;
182 c1.clear();
183 c1.merge(std::move(c2));
184 VERIFY( c1 == c3 );
185 VERIFY( c2.empty() );
187 c2.merge(std::move(c1));
188 VERIFY( c1.empty() );
189 VERIFY( c2 == c3 );
191 c2.merge(c1);
192 VERIFY( c1.empty() );
193 VERIFY( c2 == c3 );
195 c2.merge(c2);
196 VERIFY( c2 == c3 );
198 test_type c4 = c3;
199 c2.merge(c4);
200 VERIFY( c2 == c3 );
201 VERIFY( c4 == c3 );
203 c4.emplace(9);
204 c2.merge(c4);
205 VERIFY( c2.size() == c3.size() + 1 );
206 VERIFY( c4 == c3 );
209 void
210 test08()
212 test_type c1{ 1, 3, 5 };
213 std::unordered_set<int, hash, equal> c2{ 2, 4, 6 };
214 const auto c3 = c2;
216 c1.merge(c2);
217 VERIFY( c1.size() == 6 );
218 VERIFY( c2.empty() );
220 c2 = c3;
221 c1.clear();
222 c1.merge(std::move(c2));
223 VERIFY( equal_elements(c1, c3) );
224 VERIFY( c2.empty() );
226 c2.merge(std::move(c1));
227 VERIFY( c1.empty() );
228 VERIFY( c2 == c3 );
230 c2.merge(c1);
231 VERIFY( c1.empty() );
232 VERIFY( c2 == c3 );
234 c2.merge(c2);
235 VERIFY( c2 == c3 );
237 auto c4 = c3;
238 c2.merge(c4);
239 VERIFY( c2 == c3 );
240 VERIFY( c4 == c3 );
242 c4.emplace(9);
243 c2.merge(c4);
244 VERIFY( c2.size() == c3.size() + 1 );
245 VERIFY( c4 == c3 );
248 void
249 test09()
251 struct stateful_hash
253 size_t seed = 0;
255 auto operator()(const int& i) const noexcept
256 { return std::hash<int>()(i) + seed; }
259 using set_type = std::unordered_set<int, stateful_hash>;
260 set_type c1({ 1, 3, 5 }, 0, stateful_hash{1});
261 set_type c2({ 2, 4, 6 }, 0, stateful_hash{2});
262 const auto c3 = c2;
264 c1.merge(c2);
265 VERIFY( c1.size() == 6 );
266 VERIFY( c2.empty() );
268 c2 = c3;
269 c1.clear();
270 c1.merge(std::move(c2));
271 VERIFY( c1 == c3 );
272 VERIFY( c2.empty() );
274 c2.merge(std::move(c1));
275 VERIFY( c1.empty() );
276 VERIFY( c2 == c3 );
278 c2.merge(c1);
279 VERIFY( c1.empty() );
280 VERIFY( c2 == c3 );
282 c2.merge(c2);
283 VERIFY( c2 == c3 );
285 test_type c4{ -1, -3, -5 };
286 c2.merge(c4);
287 VERIFY( c2.size() == 6 );
288 VERIFY( c4.empty() );
289 auto c6 = c3;
290 c6.merge(c2);
291 VERIFY( c6.size() == 6 );
292 VERIFY( c2.size() == 3 );
296 main()
298 test01();
299 test02();
300 test03();
301 test04();
302 test07();
303 test08();
304 test09();