1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <unordered_map>
11
12 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
13 // class Alloc = allocator<pair<const Key, T>>>
14 // class unordered_map
15
16 // void swap(unordered_map& __u);
17
18 #include <unordered_map>
19 #include <string>
20 #include <cassert>
21
22 #include "../../test_compare.h"
23 #include "../../test_hash.h"
24 #include "test_allocator.h"
25 #include "min_allocator.h"
26
main()27 int main()
28 {
29 {
30 typedef test_hash<std::hash<int> > Hash;
31 typedef test_compare<std::equal_to<int> > Compare;
32 typedef test_allocator<std::pair<const int, std::string> > Alloc;
33 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
34 typedef std::pair<int, std::string> P;
35 C c1(0, Hash(1), Compare(1), Alloc(1));
36 C c2(0, Hash(2), Compare(2), Alloc(2));
37 c2.max_load_factor(2);
38 c1.swap(c2);
39
40 assert(c1.bucket_count() == 0);
41 assert(c1.size() == 0);
42 assert(c1.hash_function() == Hash(2));
43 assert(c1.key_eq() == Compare(2));
44 assert(c1.get_allocator() == Alloc(1));
45 assert(std::distance(c1.begin(), c1.end()) == c1.size());
46 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
47 assert(c1.max_load_factor() == 2);
48
49 assert(c2.bucket_count() == 0);
50 assert(c2.size() == 0);
51 assert(c2.hash_function() == Hash(1));
52 assert(c2.key_eq() == Compare(1));
53 assert(c2.get_allocator() == Alloc(2));
54 assert(std::distance(c2.begin(), c2.end()) == c2.size());
55 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
56 assert(c2.max_load_factor() == 1);
57 }
58 {
59 typedef test_hash<std::hash<int> > Hash;
60 typedef test_compare<std::equal_to<int> > Compare;
61 typedef test_allocator<std::pair<const int, std::string> > Alloc;
62 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
63 typedef std::pair<int, std::string> P;
64 P a2[] =
65 {
66 P(10, "ten"),
67 P(20, "twenty"),
68 P(30, "thirty"),
69 P(40, "fourty"),
70 P(50, "fifty"),
71 P(60, "sixty"),
72 P(70, "seventy"),
73 P(80, "eighty"),
74 };
75 C c1(0, Hash(1), Compare(1), Alloc(1));
76 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
77 c2.max_load_factor(2);
78 c1.swap(c2);
79
80 assert(c1.bucket_count() >= 11);
81 assert(c1.size() == 8);
82 assert(c1.at(10) == "ten");
83 assert(c1.at(20) == "twenty");
84 assert(c1.at(30) == "thirty");
85 assert(c1.at(40) == "fourty");
86 assert(c1.at(50) == "fifty");
87 assert(c1.at(60) == "sixty");
88 assert(c1.at(70) == "seventy");
89 assert(c1.at(80) == "eighty");
90 assert(c1.hash_function() == Hash(2));
91 assert(c1.key_eq() == Compare(2));
92 assert(c1.get_allocator() == Alloc(1));
93 assert(std::distance(c1.begin(), c1.end()) == c1.size());
94 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
95 assert(c1.max_load_factor() == 2);
96
97 assert(c2.bucket_count() == 0);
98 assert(c2.size() == 0);
99 assert(c2.hash_function() == Hash(1));
100 assert(c2.key_eq() == Compare(1));
101 assert(c2.get_allocator() == Alloc(2));
102 assert(std::distance(c2.begin(), c2.end()) == c2.size());
103 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
104 assert(c2.max_load_factor() == 1);
105 }
106 {
107 typedef test_hash<std::hash<int> > Hash;
108 typedef test_compare<std::equal_to<int> > Compare;
109 typedef test_allocator<std::pair<const int, std::string> > Alloc;
110 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
111 typedef std::pair<int, std::string> P;
112 P a1[] =
113 {
114 P(1, "one"),
115 P(2, "two"),
116 P(3, "three"),
117 P(4, "four"),
118 P(1, "four"),
119 P(2, "four"),
120 };
121 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
122 C c2(0, Hash(2), Compare(2), Alloc(2));
123 c2.max_load_factor(2);
124 c1.swap(c2);
125
126 assert(c1.bucket_count() == 0);
127 assert(c1.size() == 0);
128 assert(c1.hash_function() == Hash(2));
129 assert(c1.key_eq() == Compare(2));
130 assert(c1.get_allocator() == Alloc(1));
131 assert(std::distance(c1.begin(), c1.end()) == c1.size());
132 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
133 assert(c1.max_load_factor() == 2);
134
135 assert(c2.bucket_count() >= 5);
136 assert(c2.size() == 4);
137 assert(c2.at(1) == "one");
138 assert(c2.at(2) == "two");
139 assert(c2.at(3) == "three");
140 assert(c2.at(4) == "four");
141 assert(c2.hash_function() == Hash(1));
142 assert(c2.key_eq() == Compare(1));
143 assert(c2.get_allocator() == Alloc(2));
144 assert(std::distance(c2.begin(), c2.end()) == c2.size());
145 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
146 assert(c2.max_load_factor() == 1);
147 }
148 {
149 typedef test_hash<std::hash<int> > Hash;
150 typedef test_compare<std::equal_to<int> > Compare;
151 typedef test_allocator<std::pair<const int, std::string> > Alloc;
152 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
153 typedef std::pair<int, std::string> P;
154 P a1[] =
155 {
156 P(1, "one"),
157 P(2, "two"),
158 P(3, "three"),
159 P(4, "four"),
160 P(1, "four"),
161 P(2, "four"),
162 };
163 P a2[] =
164 {
165 P(10, "ten"),
166 P(20, "twenty"),
167 P(30, "thirty"),
168 P(40, "fourty"),
169 P(50, "fifty"),
170 P(60, "sixty"),
171 P(70, "seventy"),
172 P(80, "eighty"),
173 };
174 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
175 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
176 c2.max_load_factor(2);
177 c1.swap(c2);
178
179 assert(c1.bucket_count() >= 11);
180 assert(c1.size() == 8);
181 assert(c1.at(10) == "ten");
182 assert(c1.at(20) == "twenty");
183 assert(c1.at(30) == "thirty");
184 assert(c1.at(40) == "fourty");
185 assert(c1.at(50) == "fifty");
186 assert(c1.at(60) == "sixty");
187 assert(c1.at(70) == "seventy");
188 assert(c1.at(80) == "eighty");
189 assert(c1.hash_function() == Hash(2));
190 assert(c1.key_eq() == Compare(2));
191 assert(c1.get_allocator() == Alloc(1));
192 assert(std::distance(c1.begin(), c1.end()) == c1.size());
193 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
194 assert(c1.max_load_factor() == 2);
195
196 assert(c2.bucket_count() >= 5);
197 assert(c2.size() == 4);
198 assert(c2.at(1) == "one");
199 assert(c2.at(2) == "two");
200 assert(c2.at(3) == "three");
201 assert(c2.at(4) == "four");
202 assert(c2.hash_function() == Hash(1));
203 assert(c2.key_eq() == Compare(1));
204 assert(c2.get_allocator() == Alloc(2));
205 assert(std::distance(c2.begin(), c2.end()) == c2.size());
206 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
207 assert(c2.max_load_factor() == 1);
208 }
209
210 {
211 typedef test_hash<std::hash<int> > Hash;
212 typedef test_compare<std::equal_to<int> > Compare;
213 typedef other_allocator<std::pair<const int, std::string> > Alloc;
214 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
215 typedef std::pair<int, std::string> P;
216 C c1(0, Hash(1), Compare(1), Alloc(1));
217 C c2(0, Hash(2), Compare(2), Alloc(2));
218 c2.max_load_factor(2);
219 c1.swap(c2);
220
221 assert(c1.bucket_count() == 0);
222 assert(c1.size() == 0);
223 assert(c1.hash_function() == Hash(2));
224 assert(c1.key_eq() == Compare(2));
225 assert(c1.get_allocator() == Alloc(2));
226 assert(std::distance(c1.begin(), c1.end()) == c1.size());
227 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
228 assert(c1.max_load_factor() == 2);
229
230 assert(c2.bucket_count() == 0);
231 assert(c2.size() == 0);
232 assert(c2.hash_function() == Hash(1));
233 assert(c2.key_eq() == Compare(1));
234 assert(c2.get_allocator() == Alloc(1));
235 assert(std::distance(c2.begin(), c2.end()) == c2.size());
236 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
237 assert(c2.max_load_factor() == 1);
238 }
239 {
240 typedef test_hash<std::hash<int> > Hash;
241 typedef test_compare<std::equal_to<int> > Compare;
242 typedef other_allocator<std::pair<const int, std::string> > Alloc;
243 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
244 typedef std::pair<int, std::string> P;
245 P a2[] =
246 {
247 P(10, "ten"),
248 P(20, "twenty"),
249 P(30, "thirty"),
250 P(40, "fourty"),
251 P(50, "fifty"),
252 P(60, "sixty"),
253 P(70, "seventy"),
254 P(80, "eighty"),
255 };
256 C c1(0, Hash(1), Compare(1), Alloc(1));
257 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
258 c2.max_load_factor(2);
259 c1.swap(c2);
260
261 assert(c1.bucket_count() >= 11);
262 assert(c1.size() == 8);
263 assert(c1.at(10) == "ten");
264 assert(c1.at(20) == "twenty");
265 assert(c1.at(30) == "thirty");
266 assert(c1.at(40) == "fourty");
267 assert(c1.at(50) == "fifty");
268 assert(c1.at(60) == "sixty");
269 assert(c1.at(70) == "seventy");
270 assert(c1.at(80) == "eighty");
271 assert(c1.hash_function() == Hash(2));
272 assert(c1.key_eq() == Compare(2));
273 assert(c1.get_allocator() == Alloc(2));
274 assert(std::distance(c1.begin(), c1.end()) == c1.size());
275 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
276 assert(c1.max_load_factor() == 2);
277
278 assert(c2.bucket_count() == 0);
279 assert(c2.size() == 0);
280 assert(c2.hash_function() == Hash(1));
281 assert(c2.key_eq() == Compare(1));
282 assert(c2.get_allocator() == Alloc(1));
283 assert(std::distance(c2.begin(), c2.end()) == c2.size());
284 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
285 assert(c2.max_load_factor() == 1);
286 }
287 {
288 typedef test_hash<std::hash<int> > Hash;
289 typedef test_compare<std::equal_to<int> > Compare;
290 typedef other_allocator<std::pair<const int, std::string> > Alloc;
291 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
292 typedef std::pair<int, std::string> P;
293 P a1[] =
294 {
295 P(1, "one"),
296 P(2, "two"),
297 P(3, "three"),
298 P(4, "four"),
299 P(1, "four"),
300 P(2, "four"),
301 };
302 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
303 C c2(0, Hash(2), Compare(2), Alloc(2));
304 c2.max_load_factor(2);
305 c1.swap(c2);
306
307 assert(c1.bucket_count() == 0);
308 assert(c1.size() == 0);
309 assert(c1.hash_function() == Hash(2));
310 assert(c1.key_eq() == Compare(2));
311 assert(c1.get_allocator() == Alloc(2));
312 assert(std::distance(c1.begin(), c1.end()) == c1.size());
313 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
314 assert(c1.max_load_factor() == 2);
315
316 assert(c2.bucket_count() >= 5);
317 assert(c2.size() == 4);
318 assert(c2.at(1) == "one");
319 assert(c2.at(2) == "two");
320 assert(c2.at(3) == "three");
321 assert(c2.at(4) == "four");
322 assert(c2.hash_function() == Hash(1));
323 assert(c2.key_eq() == Compare(1));
324 assert(c2.get_allocator() == Alloc(1));
325 assert(std::distance(c2.begin(), c2.end()) == c2.size());
326 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
327 assert(c2.max_load_factor() == 1);
328 }
329 {
330 typedef test_hash<std::hash<int> > Hash;
331 typedef test_compare<std::equal_to<int> > Compare;
332 typedef other_allocator<std::pair<const int, std::string> > Alloc;
333 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
334 typedef std::pair<int, std::string> P;
335 P a1[] =
336 {
337 P(1, "one"),
338 P(2, "two"),
339 P(3, "three"),
340 P(4, "four"),
341 P(1, "four"),
342 P(2, "four"),
343 };
344 P a2[] =
345 {
346 P(10, "ten"),
347 P(20, "twenty"),
348 P(30, "thirty"),
349 P(40, "fourty"),
350 P(50, "fifty"),
351 P(60, "sixty"),
352 P(70, "seventy"),
353 P(80, "eighty"),
354 };
355 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
356 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
357 c2.max_load_factor(2);
358 c1.swap(c2);
359
360 assert(c1.bucket_count() >= 11);
361 assert(c1.size() == 8);
362 assert(c1.at(10) == "ten");
363 assert(c1.at(20) == "twenty");
364 assert(c1.at(30) == "thirty");
365 assert(c1.at(40) == "fourty");
366 assert(c1.at(50) == "fifty");
367 assert(c1.at(60) == "sixty");
368 assert(c1.at(70) == "seventy");
369 assert(c1.at(80) == "eighty");
370 assert(c1.hash_function() == Hash(2));
371 assert(c1.key_eq() == Compare(2));
372 assert(c1.get_allocator() == Alloc(2));
373 assert(std::distance(c1.begin(), c1.end()) == c1.size());
374 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
375 assert(c1.max_load_factor() == 2);
376
377 assert(c2.bucket_count() >= 5);
378 assert(c2.size() == 4);
379 assert(c2.at(1) == "one");
380 assert(c2.at(2) == "two");
381 assert(c2.at(3) == "three");
382 assert(c2.at(4) == "four");
383 assert(c2.hash_function() == Hash(1));
384 assert(c2.key_eq() == Compare(1));
385 assert(c2.get_allocator() == Alloc(1));
386 assert(std::distance(c2.begin(), c2.end()) == c2.size());
387 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
388 assert(c2.max_load_factor() == 1);
389 }
390 #if __cplusplus >= 201103L
391 {
392 typedef test_hash<std::hash<int> > Hash;
393 typedef test_compare<std::equal_to<int> > Compare;
394 typedef min_allocator<std::pair<const int, std::string> > Alloc;
395 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
396 typedef std::pair<int, std::string> P;
397 C c1(0, Hash(1), Compare(1), Alloc());
398 C c2(0, Hash(2), Compare(2), Alloc());
399 c2.max_load_factor(2);
400 c1.swap(c2);
401
402 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(std::distance(c1.begin(), c1.end()) == c1.size());
408 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
409 assert(c1.max_load_factor() == 2);
410
411 assert(c2.bucket_count() == 0);
412 assert(c2.size() == 0);
413 assert(c2.hash_function() == Hash(1));
414 assert(c2.key_eq() == Compare(1));
415 assert(c2.get_allocator() == Alloc());
416 assert(std::distance(c2.begin(), c2.end()) == c2.size());
417 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
418 assert(c2.max_load_factor() == 1);
419 }
420 {
421 typedef test_hash<std::hash<int> > Hash;
422 typedef test_compare<std::equal_to<int> > Compare;
423 typedef min_allocator<std::pair<const int, std::string> > Alloc;
424 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
425 typedef std::pair<int, std::string> P;
426 P a2[] =
427 {
428 P(10, "ten"),
429 P(20, "twenty"),
430 P(30, "thirty"),
431 P(40, "fourty"),
432 P(50, "fifty"),
433 P(60, "sixty"),
434 P(70, "seventy"),
435 P(80, "eighty"),
436 };
437 C c1(0, Hash(1), Compare(1), Alloc());
438 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
439 c2.max_load_factor(2);
440 c1.swap(c2);
441
442 assert(c1.bucket_count() >= 11);
443 assert(c1.size() == 8);
444 assert(c1.at(10) == "ten");
445 assert(c1.at(20) == "twenty");
446 assert(c1.at(30) == "thirty");
447 assert(c1.at(40) == "fourty");
448 assert(c1.at(50) == "fifty");
449 assert(c1.at(60) == "sixty");
450 assert(c1.at(70) == "seventy");
451 assert(c1.at(80) == "eighty");
452 assert(c1.hash_function() == Hash(2));
453 assert(c1.key_eq() == Compare(2));
454 assert(c1.get_allocator() == Alloc());
455 assert(std::distance(c1.begin(), c1.end()) == c1.size());
456 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
457 assert(c1.max_load_factor() == 2);
458
459 assert(c2.bucket_count() == 0);
460 assert(c2.size() == 0);
461 assert(c2.hash_function() == Hash(1));
462 assert(c2.key_eq() == Compare(1));
463 assert(c2.get_allocator() == Alloc());
464 assert(std::distance(c2.begin(), c2.end()) == c2.size());
465 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
466 assert(c2.max_load_factor() == 1);
467 }
468 {
469 typedef test_hash<std::hash<int> > Hash;
470 typedef test_compare<std::equal_to<int> > Compare;
471 typedef min_allocator<std::pair<const int, std::string> > Alloc;
472 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
473 typedef std::pair<int, std::string> P;
474 P a1[] =
475 {
476 P(1, "one"),
477 P(2, "two"),
478 P(3, "three"),
479 P(4, "four"),
480 P(1, "four"),
481 P(2, "four"),
482 };
483 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
484 C c2(0, Hash(2), Compare(2), Alloc());
485 c2.max_load_factor(2);
486 c1.swap(c2);
487
488 assert(c1.bucket_count() == 0);
489 assert(c1.size() == 0);
490 assert(c1.hash_function() == Hash(2));
491 assert(c1.key_eq() == Compare(2));
492 assert(c1.get_allocator() == Alloc());
493 assert(std::distance(c1.begin(), c1.end()) == c1.size());
494 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
495 assert(c1.max_load_factor() == 2);
496
497 assert(c2.bucket_count() >= 5);
498 assert(c2.size() == 4);
499 assert(c2.at(1) == "one");
500 assert(c2.at(2) == "two");
501 assert(c2.at(3) == "three");
502 assert(c2.at(4) == "four");
503 assert(c2.hash_function() == Hash(1));
504 assert(c2.key_eq() == Compare(1));
505 assert(c2.get_allocator() == Alloc());
506 assert(std::distance(c2.begin(), c2.end()) == c2.size());
507 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
508 assert(c2.max_load_factor() == 1);
509 }
510 {
511 typedef test_hash<std::hash<int> > Hash;
512 typedef test_compare<std::equal_to<int> > Compare;
513 typedef min_allocator<std::pair<const int, std::string> > Alloc;
514 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
515 typedef std::pair<int, std::string> P;
516 P a1[] =
517 {
518 P(1, "one"),
519 P(2, "two"),
520 P(3, "three"),
521 P(4, "four"),
522 P(1, "four"),
523 P(2, "four"),
524 };
525 P a2[] =
526 {
527 P(10, "ten"),
528 P(20, "twenty"),
529 P(30, "thirty"),
530 P(40, "fourty"),
531 P(50, "fifty"),
532 P(60, "sixty"),
533 P(70, "seventy"),
534 P(80, "eighty"),
535 };
536 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
537 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
538 c2.max_load_factor(2);
539 c1.swap(c2);
540
541 assert(c1.bucket_count() >= 11);
542 assert(c1.size() == 8);
543 assert(c1.at(10) == "ten");
544 assert(c1.at(20) == "twenty");
545 assert(c1.at(30) == "thirty");
546 assert(c1.at(40) == "fourty");
547 assert(c1.at(50) == "fifty");
548 assert(c1.at(60) == "sixty");
549 assert(c1.at(70) == "seventy");
550 assert(c1.at(80) == "eighty");
551 assert(c1.hash_function() == Hash(2));
552 assert(c1.key_eq() == Compare(2));
553 assert(c1.get_allocator() == Alloc());
554 assert(std::distance(c1.begin(), c1.end()) == c1.size());
555 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
556 assert(c1.max_load_factor() == 2);
557
558 assert(c2.bucket_count() >= 5);
559 assert(c2.size() == 4);
560 assert(c2.at(1) == "one");
561 assert(c2.at(2) == "two");
562 assert(c2.at(3) == "three");
563 assert(c2.at(4) == "four");
564 assert(c2.hash_function() == Hash(1));
565 assert(c2.key_eq() == Compare(1));
566 assert(c2.get_allocator() == Alloc());
567 assert(std::distance(c2.begin(), c2.end()) == c2.size());
568 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
569 assert(c2.max_load_factor() == 1);
570 }
571 #endif
572 }
573