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_macros.h"
25#include "test_allocator.h"
26#include "min_allocator.h"
27
28int main()
29{
30    {
31        typedef test_hash<std::hash<int> > Hash;
32        typedef test_compare<std::equal_to<int> > Compare;
33        typedef test_allocator<std::pair<const int, std::string> > Alloc;
34        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
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, "forty"),
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) == "forty");
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, "forty"),
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) == "forty");
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        C c1(0, Hash(1), Compare(1), Alloc(1));
216        C c2(0, Hash(2), Compare(2), Alloc(2));
217        c2.max_load_factor(2);
218        c1.swap(c2);
219
220        assert(c1.bucket_count() == 0);
221        assert(c1.size() == 0);
222        assert(c1.hash_function() == Hash(2));
223        assert(c1.key_eq() == Compare(2));
224        assert(c1.get_allocator() == Alloc(2));
225        assert(std::distance(c1.begin(), c1.end()) == c1.size());
226        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
227        assert(c1.max_load_factor() == 2);
228
229        assert(c2.bucket_count() == 0);
230        assert(c2.size() == 0);
231        assert(c2.hash_function() == Hash(1));
232        assert(c2.key_eq() == Compare(1));
233        assert(c2.get_allocator() == Alloc(1));
234        assert(std::distance(c2.begin(), c2.end()) == c2.size());
235        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
236        assert(c2.max_load_factor() == 1);
237    }
238    {
239        typedef test_hash<std::hash<int> > Hash;
240        typedef test_compare<std::equal_to<int> > Compare;
241        typedef other_allocator<std::pair<const int, std::string> > Alloc;
242        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
243        typedef std::pair<int, std::string> P;
244        P a2[] =
245        {
246            P(10, "ten"),
247            P(20, "twenty"),
248            P(30, "thirty"),
249            P(40, "forty"),
250            P(50, "fifty"),
251            P(60, "sixty"),
252            P(70, "seventy"),
253            P(80, "eighty"),
254        };
255        C c1(0, Hash(1), Compare(1), Alloc(1));
256        C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
257        c2.max_load_factor(2);
258        c1.swap(c2);
259
260        assert(c1.bucket_count() >= 11);
261        assert(c1.size() == 8);
262        assert(c1.at(10) == "ten");
263        assert(c1.at(20) == "twenty");
264        assert(c1.at(30) == "thirty");
265        assert(c1.at(40) == "forty");
266        assert(c1.at(50) == "fifty");
267        assert(c1.at(60) == "sixty");
268        assert(c1.at(70) == "seventy");
269        assert(c1.at(80) == "eighty");
270        assert(c1.hash_function() == Hash(2));
271        assert(c1.key_eq() == Compare(2));
272        assert(c1.get_allocator() == Alloc(2));
273        assert(std::distance(c1.begin(), c1.end()) == c1.size());
274        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
275        assert(c1.max_load_factor() == 2);
276
277        assert(c2.bucket_count() == 0);
278        assert(c2.size() == 0);
279        assert(c2.hash_function() == Hash(1));
280        assert(c2.key_eq() == Compare(1));
281        assert(c2.get_allocator() == Alloc(1));
282        assert(std::distance(c2.begin(), c2.end()) == c2.size());
283        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
284        assert(c2.max_load_factor() == 1);
285    }
286    {
287        typedef test_hash<std::hash<int> > Hash;
288        typedef test_compare<std::equal_to<int> > Compare;
289        typedef other_allocator<std::pair<const int, std::string> > Alloc;
290        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
291        typedef std::pair<int, std::string> P;
292        P a1[] =
293        {
294            P(1, "one"),
295            P(2, "two"),
296            P(3, "three"),
297            P(4, "four"),
298            P(1, "four"),
299            P(2, "four"),
300        };
301        C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
302        C c2(0, Hash(2), Compare(2), Alloc(2));
303        c2.max_load_factor(2);
304        c1.swap(c2);
305
306        assert(c1.bucket_count() == 0);
307        assert(c1.size() == 0);
308        assert(c1.hash_function() == Hash(2));
309        assert(c1.key_eq() == Compare(2));
310        assert(c1.get_allocator() == Alloc(2));
311        assert(std::distance(c1.begin(), c1.end()) == c1.size());
312        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
313        assert(c1.max_load_factor() == 2);
314
315        assert(c2.bucket_count() >= 5);
316        assert(c2.size() == 4);
317        assert(c2.at(1) == "one");
318        assert(c2.at(2) == "two");
319        assert(c2.at(3) == "three");
320        assert(c2.at(4) == "four");
321        assert(c2.hash_function() == Hash(1));
322        assert(c2.key_eq() == Compare(1));
323        assert(c2.get_allocator() == Alloc(1));
324        assert(std::distance(c2.begin(), c2.end()) == c2.size());
325        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
326        assert(c2.max_load_factor() == 1);
327    }
328    {
329        typedef test_hash<std::hash<int> > Hash;
330        typedef test_compare<std::equal_to<int> > Compare;
331        typedef other_allocator<std::pair<const int, std::string> > Alloc;
332        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
333        typedef std::pair<int, std::string> P;
334        P a1[] =
335        {
336            P(1, "one"),
337            P(2, "two"),
338            P(3, "three"),
339            P(4, "four"),
340            P(1, "four"),
341            P(2, "four"),
342        };
343        P a2[] =
344        {
345            P(10, "ten"),
346            P(20, "twenty"),
347            P(30, "thirty"),
348            P(40, "forty"),
349            P(50, "fifty"),
350            P(60, "sixty"),
351            P(70, "seventy"),
352            P(80, "eighty"),
353        };
354        C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
355        C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
356        c2.max_load_factor(2);
357        c1.swap(c2);
358
359        assert(c1.bucket_count() >= 11);
360        assert(c1.size() == 8);
361        assert(c1.at(10) == "ten");
362        assert(c1.at(20) == "twenty");
363        assert(c1.at(30) == "thirty");
364        assert(c1.at(40) == "forty");
365        assert(c1.at(50) == "fifty");
366        assert(c1.at(60) == "sixty");
367        assert(c1.at(70) == "seventy");
368        assert(c1.at(80) == "eighty");
369        assert(c1.hash_function() == Hash(2));
370        assert(c1.key_eq() == Compare(2));
371        assert(c1.get_allocator() == Alloc(2));
372        assert(std::distance(c1.begin(), c1.end()) == c1.size());
373        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
374        assert(c1.max_load_factor() == 2);
375
376        assert(c2.bucket_count() >= 5);
377        assert(c2.size() == 4);
378        assert(c2.at(1) == "one");
379        assert(c2.at(2) == "two");
380        assert(c2.at(3) == "three");
381        assert(c2.at(4) == "four");
382        assert(c2.hash_function() == Hash(1));
383        assert(c2.key_eq() == Compare(1));
384        assert(c2.get_allocator() == Alloc(1));
385        assert(std::distance(c2.begin(), c2.end()) == c2.size());
386        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
387        assert(c2.max_load_factor() == 1);
388    }
389#if TEST_STD_VER >= 11
390    {
391        typedef test_hash<std::hash<int> > Hash;
392        typedef test_compare<std::equal_to<int> > Compare;
393        typedef min_allocator<std::pair<const int, std::string> > Alloc;
394        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
395        C c1(0, Hash(1), Compare(1), Alloc());
396        C c2(0, Hash(2), Compare(2), Alloc());
397        c2.max_load_factor(2);
398        c1.swap(c2);
399
400        assert(c1.bucket_count() == 0);
401        assert(c1.size() == 0);
402        assert(c1.hash_function() == Hash(2));
403        assert(c1.key_eq() == Compare(2));
404        assert(c1.get_allocator() == Alloc());
405        assert(std::distance(c1.begin(), c1.end()) == c1.size());
406        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
407        assert(c1.max_load_factor() == 2);
408
409        assert(c2.bucket_count() == 0);
410        assert(c2.size() == 0);
411        assert(c2.hash_function() == Hash(1));
412        assert(c2.key_eq() == Compare(1));
413        assert(c2.get_allocator() == Alloc());
414        assert(std::distance(c2.begin(), c2.end()) == c2.size());
415        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
416        assert(c2.max_load_factor() == 1);
417    }
418    {
419        typedef test_hash<std::hash<int> > Hash;
420        typedef test_compare<std::equal_to<int> > Compare;
421        typedef min_allocator<std::pair<const int, std::string> > Alloc;
422        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
423        typedef std::pair<int, std::string> P;
424        P a2[] =
425        {
426            P(10, "ten"),
427            P(20, "twenty"),
428            P(30, "thirty"),
429            P(40, "forty"),
430            P(50, "fifty"),
431            P(60, "sixty"),
432            P(70, "seventy"),
433            P(80, "eighty"),
434        };
435        C c1(0, Hash(1), Compare(1), Alloc());
436        C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
437        c2.max_load_factor(2);
438        c1.swap(c2);
439
440        assert(c1.bucket_count() >= 11);
441        assert(c1.size() == 8);
442        assert(c1.at(10) == "ten");
443        assert(c1.at(20) == "twenty");
444        assert(c1.at(30) == "thirty");
445        assert(c1.at(40) == "forty");
446        assert(c1.at(50) == "fifty");
447        assert(c1.at(60) == "sixty");
448        assert(c1.at(70) == "seventy");
449        assert(c1.at(80) == "eighty");
450        assert(c1.hash_function() == Hash(2));
451        assert(c1.key_eq() == Compare(2));
452        assert(c1.get_allocator() == Alloc());
453        assert(std::distance(c1.begin(), c1.end()) == c1.size());
454        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
455        assert(c1.max_load_factor() == 2);
456
457        assert(c2.bucket_count() == 0);
458        assert(c2.size() == 0);
459        assert(c2.hash_function() == Hash(1));
460        assert(c2.key_eq() == Compare(1));
461        assert(c2.get_allocator() == Alloc());
462        assert(std::distance(c2.begin(), c2.end()) == c2.size());
463        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
464        assert(c2.max_load_factor() == 1);
465    }
466    {
467        typedef test_hash<std::hash<int> > Hash;
468        typedef test_compare<std::equal_to<int> > Compare;
469        typedef min_allocator<std::pair<const int, std::string> > Alloc;
470        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
471        typedef std::pair<int, std::string> P;
472        P a1[] =
473        {
474            P(1, "one"),
475            P(2, "two"),
476            P(3, "three"),
477            P(4, "four"),
478            P(1, "four"),
479            P(2, "four"),
480        };
481        C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
482        C c2(0, Hash(2), Compare(2), Alloc());
483        c2.max_load_factor(2);
484        c1.swap(c2);
485
486        assert(c1.bucket_count() == 0);
487        assert(c1.size() == 0);
488        assert(c1.hash_function() == Hash(2));
489        assert(c1.key_eq() == Compare(2));
490        assert(c1.get_allocator() == Alloc());
491        assert(std::distance(c1.begin(), c1.end()) == c1.size());
492        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
493        assert(c1.max_load_factor() == 2);
494
495        assert(c2.bucket_count() >= 5);
496        assert(c2.size() == 4);
497        assert(c2.at(1) == "one");
498        assert(c2.at(2) == "two");
499        assert(c2.at(3) == "three");
500        assert(c2.at(4) == "four");
501        assert(c2.hash_function() == Hash(1));
502        assert(c2.key_eq() == Compare(1));
503        assert(c2.get_allocator() == Alloc());
504        assert(std::distance(c2.begin(), c2.end()) == c2.size());
505        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
506        assert(c2.max_load_factor() == 1);
507    }
508    {
509        typedef test_hash<std::hash<int> > Hash;
510        typedef test_compare<std::equal_to<int> > Compare;
511        typedef min_allocator<std::pair<const int, std::string> > Alloc;
512        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
513        typedef std::pair<int, std::string> P;
514        P a1[] =
515        {
516            P(1, "one"),
517            P(2, "two"),
518            P(3, "three"),
519            P(4, "four"),
520            P(1, "four"),
521            P(2, "four"),
522        };
523        P a2[] =
524        {
525            P(10, "ten"),
526            P(20, "twenty"),
527            P(30, "thirty"),
528            P(40, "forty"),
529            P(50, "fifty"),
530            P(60, "sixty"),
531            P(70, "seventy"),
532            P(80, "eighty"),
533        };
534        C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
535        C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
536        c2.max_load_factor(2);
537        c1.swap(c2);
538
539        assert(c1.bucket_count() >= 11);
540        assert(c1.size() == 8);
541        assert(c1.at(10) == "ten");
542        assert(c1.at(20) == "twenty");
543        assert(c1.at(30) == "thirty");
544        assert(c1.at(40) == "forty");
545        assert(c1.at(50) == "fifty");
546        assert(c1.at(60) == "sixty");
547        assert(c1.at(70) == "seventy");
548        assert(c1.at(80) == "eighty");
549        assert(c1.hash_function() == Hash(2));
550        assert(c1.key_eq() == Compare(2));
551        assert(c1.get_allocator() == Alloc());
552        assert(std::distance(c1.begin(), c1.end()) == c1.size());
553        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
554        assert(c1.max_load_factor() == 2);
555
556        assert(c2.bucket_count() >= 5);
557        assert(c2.size() == 4);
558        assert(c2.at(1) == "one");
559        assert(c2.at(2) == "two");
560        assert(c2.at(3) == "three");
561        assert(c2.at(4) == "four");
562        assert(c2.hash_function() == Hash(1));
563        assert(c2.key_eq() == Compare(1));
564        assert(c2.get_allocator() == Alloc());
565        assert(std::distance(c2.begin(), c2.end()) == c2.size());
566        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
567        assert(c2.max_load_factor() == 1);
568    }
569#endif
570}
571