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// <unordered_set>
10
11// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12// class Alloc = allocator<Value>>
13// class unordered_multiset
14
15// void swap(unordered_multiset& x, unordered_multiset& y);
16
17#include <unordered_set>
18#include <cassert>
19#include <cstddef>
20
21#include "test_macros.h"
22#include "../../../test_compare.h"
23#include "../../../test_hash.h"
24#include "test_allocator.h"
25#include "min_allocator.h"
26
27int main(int, char**) {
28 {
29 typedef test_hash<int> Hash;
30 typedef test_equal_to<int> Compare;
31 typedef test_allocator<int> Alloc;
32 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
33 C c1(0, Hash(1), Compare(1), Alloc(1, 1));
34 C c2(0, Hash(2), Compare(2), Alloc(1, 2));
35 c2.max_load_factor(z: 2);
36 swap(c1, c2);
37
38 LIBCPP_ASSERT(c1.bucket_count() == 0);
39 assert(c1.size() == 0);
40 assert(c1.hash_function() == Hash(2));
41 assert(c1.key_eq() == Compare(2));
42 assert(c1.get_allocator().get_id() == 1);
43 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
44 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
45 assert(c1.max_load_factor() == 2);
46
47 LIBCPP_ASSERT(c2.bucket_count() == 0);
48 assert(c2.size() == 0);
49 assert(c2.hash_function() == Hash(1));
50 assert(c2.key_eq() == Compare(1));
51 assert(c2.get_allocator().get_id() == 2);
52 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
53 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
54 assert(c2.max_load_factor() == 1);
55 }
56 {
57 typedef test_hash<int> Hash;
58 typedef test_equal_to<int> Compare;
59 typedef test_allocator<int> Alloc;
60 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
61 typedef int P;
62 P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};
63 C c1(0, Hash(1), Compare(1), Alloc(1, 1));
64 C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(1, 2));
65 c2.max_load_factor(z: 2);
66 C::iterator it2 = c2.begin();
67 swap(x&: c1, y&: c2);
68
69 assert(c1.bucket_count() >= 8);
70 assert(c1.size() == 8);
71 assert(*c1.find(10) == 10);
72 assert(*c1.find(20) == 20);
73 assert(*c1.find(30) == 30);
74 assert(*c1.find(40) == 40);
75 assert(*c1.find(50) == 50);
76 assert(*c1.find(60) == 60);
77 assert(*c1.find(70) == 70);
78 assert(*c1.find(80) == 80);
79 assert(c1.hash_function() == Hash(2));
80 assert(c1.key_eq() == Compare(2));
81 assert(c1.get_allocator().get_id() == 1);
82 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
83 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
84 assert(c1.max_load_factor() == 2);
85 assert(it2 == c1.begin()); // Iterators are not invalidated
86
87 LIBCPP_ASSERT(c2.bucket_count() == 0);
88 assert(c2.size() == 0);
89 assert(c2.hash_function() == Hash(1));
90 assert(c2.key_eq() == Compare(1));
91 assert(c2.get_allocator().get_id() == 2);
92 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
93 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
94 assert(c2.max_load_factor() == 1);
95 }
96 {
97 typedef test_hash<int> Hash;
98 typedef test_equal_to<int> Compare;
99 typedef test_allocator<int> Alloc;
100 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
101 typedef int P;
102 P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
103 C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1, 1));
104 C c2(0, Hash(2), Compare(2), Alloc(1, 2));
105 c2.max_load_factor(z: 2);
106 C::iterator it1 = c1.begin();
107 swap(x&: c1, y&: c2);
108
109 LIBCPP_ASSERT(c1.bucket_count() == 0);
110 assert(c1.size() == 0);
111 assert(c1.hash_function() == Hash(2));
112 assert(c1.key_eq() == Compare(2));
113 assert(c1.get_allocator().get_id() == 1);
114 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
115 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
116 assert(c1.max_load_factor() == 2);
117
118 assert(c2.bucket_count() >= 6);
119 assert(c2.size() == 6);
120 assert(c2.count(1) == 2);
121 assert(c2.count(2) == 2);
122 assert(c2.count(3) == 1);
123 assert(c2.count(4) == 1);
124 assert(c2.hash_function() == Hash(1));
125 assert(c2.key_eq() == Compare(1));
126 assert(c2.get_allocator().get_id() == 2);
127 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
128 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
129 assert(c2.max_load_factor() == 1);
130 assert(it1 == c2.begin()); // Iterators are not invalidated
131 }
132 {
133 typedef test_hash<int> Hash;
134 typedef test_equal_to<int> Compare;
135 typedef test_allocator<int> Alloc;
136 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
137 typedef int P;
138 P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
139 P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};
140 C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1, 1));
141 C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(1, 2));
142 c2.max_load_factor(z: 2);
143 C::iterator it1 = c1.begin();
144 C::iterator it2 = c2.begin();
145 swap(x&: c1, y&: c2);
146
147 assert(c1.bucket_count() >= 8);
148 assert(c1.size() == 8);
149 assert(*c1.find(10) == 10);
150 assert(*c1.find(20) == 20);
151 assert(*c1.find(30) == 30);
152 assert(*c1.find(40) == 40);
153 assert(*c1.find(50) == 50);
154 assert(*c1.find(60) == 60);
155 assert(*c1.find(70) == 70);
156 assert(*c1.find(80) == 80);
157 assert(c1.hash_function() == Hash(2));
158 assert(c1.key_eq() == Compare(2));
159 assert(c1.get_allocator().get_id() == 1);
160 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
161 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
162 assert(c1.max_load_factor() == 2);
163 assert(it2 == c1.begin()); // Iterators are not invalidated
164
165 assert(c2.bucket_count() >= 6);
166 assert(c2.size() == 6);
167 assert(c2.count(1) == 2);
168 assert(c2.count(2) == 2);
169 assert(c2.count(3) == 1);
170 assert(c2.count(4) == 1);
171 assert(c2.hash_function() == Hash(1));
172 assert(c2.key_eq() == Compare(1));
173 assert(c2.get_allocator().get_id() == 2);
174 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
175 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
176 assert(c2.max_load_factor() == 1);
177 assert(it1 == c2.begin()); // Iterators are not invalidated
178 }
179
180 {
181 typedef test_hash<int> Hash;
182 typedef test_equal_to<int> Compare;
183 typedef other_allocator<int> Alloc;
184 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
185 C c1(0, Hash(1), Compare(1), Alloc(1));
186 C c2(0, Hash(2), Compare(2), Alloc(2));
187 c2.max_load_factor(z: 2);
188 swap(x&: c1, y&: c2);
189
190 LIBCPP_ASSERT(c1.bucket_count() == 0);
191 assert(c1.size() == 0);
192 assert(c1.hash_function() == Hash(2));
193 assert(c1.key_eq() == Compare(2));
194 assert(c1.get_allocator() == Alloc(2));
195 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
196 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
197 assert(c1.max_load_factor() == 2);
198
199 LIBCPP_ASSERT(c2.bucket_count() == 0);
200 assert(c2.size() == 0);
201 assert(c2.hash_function() == Hash(1));
202 assert(c2.key_eq() == Compare(1));
203 assert(c2.get_allocator() == Alloc(1));
204 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
205 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
206 assert(c2.max_load_factor() == 1);
207 }
208 {
209 typedef test_hash<int> Hash;
210 typedef test_equal_to<int> Compare;
211 typedef other_allocator<int> Alloc;
212 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
213 typedef int P;
214 P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};
215 C c1(0, Hash(1), Compare(1), Alloc(1));
216 C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(2));
217 c2.max_load_factor(z: 2);
218 swap(x&: c1, y&: c2);
219
220 assert(c1.bucket_count() >= 8);
221 assert(c1.size() == 8);
222 assert(*c1.find(10) == 10);
223 assert(*c1.find(20) == 20);
224 assert(*c1.find(30) == 30);
225 assert(*c1.find(40) == 40);
226 assert(*c1.find(50) == 50);
227 assert(*c1.find(60) == 60);
228 assert(*c1.find(70) == 70);
229 assert(*c1.find(80) == 80);
230 assert(c1.hash_function() == Hash(2));
231 assert(c1.key_eq() == Compare(2));
232 assert(c1.get_allocator() == Alloc(2));
233 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
234 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
235 assert(c1.max_load_factor() == 2);
236
237 LIBCPP_ASSERT(c2.bucket_count() == 0);
238 assert(c2.size() == 0);
239 assert(c2.hash_function() == Hash(1));
240 assert(c2.key_eq() == Compare(1));
241 assert(c2.get_allocator() == Alloc(1));
242 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
243 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
244 assert(c2.max_load_factor() == 1);
245 }
246 {
247 typedef test_hash<int> Hash;
248 typedef test_equal_to<int> Compare;
249 typedef other_allocator<int> Alloc;
250 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
251 typedef int P;
252 P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
253 C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1));
254 C c2(0, Hash(2), Compare(2), Alloc(2));
255 c2.max_load_factor(z: 2);
256 swap(x&: c1, y&: c2);
257
258 LIBCPP_ASSERT(c1.bucket_count() == 0);
259 assert(c1.size() == 0);
260 assert(c1.hash_function() == Hash(2));
261 assert(c1.key_eq() == Compare(2));
262 assert(c1.get_allocator() == Alloc(2));
263 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
264 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
265 assert(c1.max_load_factor() == 2);
266
267 assert(c2.bucket_count() >= 6);
268 assert(c2.size() == 6);
269 assert(c2.count(1) == 2);
270 assert(c2.count(2) == 2);
271 assert(c2.count(3) == 1);
272 assert(c2.count(4) == 1);
273 assert(c2.hash_function() == Hash(1));
274 assert(c2.key_eq() == Compare(1));
275 assert(c2.get_allocator() == Alloc(1));
276 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
277 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
278 assert(c2.max_load_factor() == 1);
279 }
280 {
281 typedef test_hash<int> Hash;
282 typedef test_equal_to<int> Compare;
283 typedef other_allocator<int> Alloc;
284 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
285 typedef int P;
286 P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
287 P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};
288 C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1));
289 C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(2));
290 c2.max_load_factor(z: 2);
291 swap(x&: c1, y&: c2);
292
293 assert(c1.bucket_count() >= 8);
294 assert(c1.size() == 8);
295 assert(*c1.find(10) == 10);
296 assert(*c1.find(20) == 20);
297 assert(*c1.find(30) == 30);
298 assert(*c1.find(40) == 40);
299 assert(*c1.find(50) == 50);
300 assert(*c1.find(60) == 60);
301 assert(*c1.find(70) == 70);
302 assert(*c1.find(80) == 80);
303 assert(c1.hash_function() == Hash(2));
304 assert(c1.key_eq() == Compare(2));
305 assert(c1.get_allocator() == Alloc(2));
306 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
307 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
308 assert(c1.max_load_factor() == 2);
309
310 assert(c2.bucket_count() >= 6);
311 assert(c2.size() == 6);
312 assert(c2.count(1) == 2);
313 assert(c2.count(2) == 2);
314 assert(c2.count(3) == 1);
315 assert(c2.count(4) == 1);
316 assert(c2.hash_function() == Hash(1));
317 assert(c2.key_eq() == Compare(1));
318 assert(c2.get_allocator() == Alloc(1));
319 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
320 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
321 assert(c2.max_load_factor() == 1);
322 }
323#if TEST_STD_VER >= 11
324 {
325 typedef test_hash<int> Hash;
326 typedef test_equal_to<int> Compare;
327 typedef min_allocator<int> Alloc;
328 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
329 C c1(0, Hash(1), Compare(1), Alloc());
330 C c2(0, Hash(2), Compare(2), Alloc());
331 c2.max_load_factor(2);
332 swap(c1, c2);
333
334 LIBCPP_ASSERT(c1.bucket_count() == 0);
335 assert(c1.size() == 0);
336 assert(c1.hash_function() == Hash(2));
337 assert(c1.key_eq() == Compare(2));
338 assert(c1.get_allocator() == Alloc());
339 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
340 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
341 assert(c1.max_load_factor() == 2);
342
343 LIBCPP_ASSERT(c2.bucket_count() == 0);
344 assert(c2.size() == 0);
345 assert(c2.hash_function() == Hash(1));
346 assert(c2.key_eq() == Compare(1));
347 assert(c2.get_allocator() == Alloc());
348 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
349 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
350 assert(c2.max_load_factor() == 1);
351 }
352 {
353 typedef test_hash<int> Hash;
354 typedef test_equal_to<int> Compare;
355 typedef min_allocator<int> Alloc;
356 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
357 typedef int P;
358 P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};
359 C c1(0, Hash(1), Compare(1), Alloc());
360 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
361 c2.max_load_factor(2);
362 swap(c1, c2);
363
364 assert(c1.bucket_count() >= 8);
365 assert(c1.size() == 8);
366 assert(*c1.find(10) == 10);
367 assert(*c1.find(20) == 20);
368 assert(*c1.find(30) == 30);
369 assert(*c1.find(40) == 40);
370 assert(*c1.find(50) == 50);
371 assert(*c1.find(60) == 60);
372 assert(*c1.find(70) == 70);
373 assert(*c1.find(80) == 80);
374 assert(c1.hash_function() == Hash(2));
375 assert(c1.key_eq() == Compare(2));
376 assert(c1.get_allocator() == Alloc());
377 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
378 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
379 assert(c1.max_load_factor() == 2);
380
381 LIBCPP_ASSERT(c2.bucket_count() == 0);
382 assert(c2.size() == 0);
383 assert(c2.hash_function() == Hash(1));
384 assert(c2.key_eq() == Compare(1));
385 assert(c2.get_allocator() == Alloc());
386 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
387 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
388 assert(c2.max_load_factor() == 1);
389 }
390 {
391 typedef test_hash<int> Hash;
392 typedef test_equal_to<int> Compare;
393 typedef min_allocator<int> Alloc;
394 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
395 typedef int P;
396 P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
397 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
398 C c2(0, Hash(2), Compare(2), Alloc());
399 c2.max_load_factor(2);
400 swap(c1, c2);
401
402 LIBCPP_ASSERT(c1.bucket_count() == 0);
403 assert(c1.size() == 0);
404 assert(c1.hash_function() == Hash(2));
405 assert(c1.key_eq() == Compare(2));
406 assert(c1.get_allocator() == Alloc());
407 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
408 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
409 assert(c1.max_load_factor() == 2);
410
411 assert(c2.bucket_count() >= 6);
412 assert(c2.size() == 6);
413 assert(c2.count(1) == 2);
414 assert(c2.count(2) == 2);
415 assert(c2.count(3) == 1);
416 assert(c2.count(4) == 1);
417 assert(c2.hash_function() == Hash(1));
418 assert(c2.key_eq() == Compare(1));
419 assert(c2.get_allocator() == Alloc());
420 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
421 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
422 assert(c2.max_load_factor() == 1);
423 }
424 {
425 typedef test_hash<int> Hash;
426 typedef test_equal_to<int> Compare;
427 typedef min_allocator<int> Alloc;
428 typedef std::unordered_multiset<int, Hash, Compare, Alloc> C;
429 typedef int P;
430 P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
431 P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};
432 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
433 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
434 c2.max_load_factor(2);
435 swap(c1, c2);
436
437 assert(c1.bucket_count() >= 8);
438 assert(c1.size() == 8);
439 assert(*c1.find(10) == 10);
440 assert(*c1.find(20) == 20);
441 assert(*c1.find(30) == 30);
442 assert(*c1.find(40) == 40);
443 assert(*c1.find(50) == 50);
444 assert(*c1.find(60) == 60);
445 assert(*c1.find(70) == 70);
446 assert(*c1.find(80) == 80);
447 assert(c1.hash_function() == Hash(2));
448 assert(c1.key_eq() == Compare(2));
449 assert(c1.get_allocator() == Alloc());
450 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
451 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
452 assert(c1.max_load_factor() == 2);
453
454 assert(c2.bucket_count() >= 6);
455 assert(c2.size() == 6);
456 assert(c2.count(1) == 2);
457 assert(c2.count(2) == 2);
458 assert(c2.count(3) == 1);
459 assert(c2.count(4) == 1);
460 assert(c2.hash_function() == Hash(1));
461 assert(c2.key_eq() == Compare(1));
462 assert(c2.get_allocator() == Alloc());
463 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
464 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
465 assert(c2.max_load_factor() == 1);
466 }
467#endif
468
469 return 0;
470}
471

source code of libcxx/test/std/containers/unord/unord.multiset/unord.multiset.swap/swap_non_member.pass.cpp