swap_member.pass.cpp revision b64f8b07c104c6cc986570ac8ee0ed16a9f23976
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
26int main()
27{
28    {
29        typedef test_hash<std::hash<int> > Hash;
30        typedef test_compare<std::equal_to<int> > Compare;
31        typedef test_allocator<std::pair<const int, std::string> > Alloc;
32        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
33        typedef std::pair<int, std::string> P;
34        C c1(0, Hash(1), Compare(1), Alloc(1));
35        C c2(0, Hash(2), Compare(2), Alloc(2));
36        c2.max_load_factor(2);
37        c1.swap(c2);
38
39        assert(c1.bucket_count() == 0);
40        assert(c1.size() == 0);
41        assert(c1.hash_function() == Hash(2));
42        assert(c1.key_eq() == Compare(2));
43        assert(c1.get_allocator() == Alloc(1));
44        assert(std::distance(c1.begin(), c1.end()) == c1.size());
45        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
46        assert(c1.max_load_factor() == 2);
47
48        assert(c2.bucket_count() == 0);
49        assert(c2.size() == 0);
50        assert(c2.hash_function() == Hash(1));
51        assert(c2.key_eq() == Compare(1));
52        assert(c2.get_allocator() == Alloc(2));
53        assert(std::distance(c2.begin(), c2.end()) == c2.size());
54        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
55        assert(c2.max_load_factor() == 1);
56    }
57    {
58        typedef test_hash<std::hash<int> > Hash;
59        typedef test_compare<std::equal_to<int> > Compare;
60        typedef test_allocator<std::pair<const int, std::string> > Alloc;
61        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
62        typedef std::pair<int, std::string> P;
63        P a2[] =
64        {
65            P(10, "ten"),
66            P(20, "twenty"),
67            P(30, "thirty"),
68            P(40, "fourty"),
69            P(50, "fifty"),
70            P(60, "sixty"),
71            P(70, "seventy"),
72            P(80, "eighty"),
73        };
74        C c1(0, Hash(1), Compare(1), Alloc(1));
75        C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
76        c2.max_load_factor(2);
77        c1.swap(c2);
78
79        assert(c1.bucket_count() >= 11);
80        assert(c1.size() == 8);
81        assert(c1.at(10) == "ten");
82        assert(c1.at(20) == "twenty");
83        assert(c1.at(30) == "thirty");
84        assert(c1.at(40) == "fourty");
85        assert(c1.at(50) == "fifty");
86        assert(c1.at(60) == "sixty");
87        assert(c1.at(70) == "seventy");
88        assert(c1.at(80) == "eighty");
89        assert(c1.hash_function() == Hash(2));
90        assert(c1.key_eq() == Compare(2));
91        assert(c1.get_allocator() == Alloc(1));
92        assert(std::distance(c1.begin(), c1.end()) == c1.size());
93        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
94        assert(c1.max_load_factor() == 2);
95
96        assert(c2.bucket_count() == 0);
97        assert(c2.size() == 0);
98        assert(c2.hash_function() == Hash(1));
99        assert(c2.key_eq() == Compare(1));
100        assert(c2.get_allocator() == Alloc(2));
101        assert(std::distance(c2.begin(), c2.end()) == c2.size());
102        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
103        assert(c2.max_load_factor() == 1);
104    }
105    {
106        typedef test_hash<std::hash<int> > Hash;
107        typedef test_compare<std::equal_to<int> > Compare;
108        typedef test_allocator<std::pair<const int, std::string> > Alloc;
109        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
110        typedef std::pair<int, std::string> P;
111        P a1[] =
112        {
113            P(1, "one"),
114            P(2, "two"),
115            P(3, "three"),
116            P(4, "four"),
117            P(1, "four"),
118            P(2, "four"),
119        };
120        C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
121        C c2(0, Hash(2), Compare(2), Alloc(2));
122        c2.max_load_factor(2);
123        c1.swap(c2);
124
125        assert(c1.bucket_count() == 0);
126        assert(c1.size() == 0);
127        assert(c1.hash_function() == Hash(2));
128        assert(c1.key_eq() == Compare(2));
129        assert(c1.get_allocator() == Alloc(1));
130        assert(std::distance(c1.begin(), c1.end()) == c1.size());
131        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
132        assert(c1.max_load_factor() == 2);
133
134        assert(c2.bucket_count() >= 5);
135        assert(c2.size() == 4);
136        assert(c2.at(1) == "one");
137        assert(c2.at(2) == "two");
138        assert(c2.at(3) == "three");
139        assert(c2.at(4) == "four");
140        assert(c2.hash_function() == Hash(1));
141        assert(c2.key_eq() == Compare(1));
142        assert(c2.get_allocator() == Alloc(2));
143        assert(std::distance(c2.begin(), c2.end()) == c2.size());
144        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
145        assert(c2.max_load_factor() == 1);
146    }
147    {
148        typedef test_hash<std::hash<int> > Hash;
149        typedef test_compare<std::equal_to<int> > Compare;
150        typedef test_allocator<std::pair<const int, std::string> > Alloc;
151        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
152        typedef std::pair<int, std::string> P;
153        P a1[] =
154        {
155            P(1, "one"),
156            P(2, "two"),
157            P(3, "three"),
158            P(4, "four"),
159            P(1, "four"),
160            P(2, "four"),
161        };
162        P a2[] =
163        {
164            P(10, "ten"),
165            P(20, "twenty"),
166            P(30, "thirty"),
167            P(40, "fourty"),
168            P(50, "fifty"),
169            P(60, "sixty"),
170            P(70, "seventy"),
171            P(80, "eighty"),
172        };
173        C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
174        C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
175        c2.max_load_factor(2);
176        c1.swap(c2);
177
178        assert(c1.bucket_count() >= 11);
179        assert(c1.size() == 8);
180        assert(c1.at(10) == "ten");
181        assert(c1.at(20) == "twenty");
182        assert(c1.at(30) == "thirty");
183        assert(c1.at(40) == "fourty");
184        assert(c1.at(50) == "fifty");
185        assert(c1.at(60) == "sixty");
186        assert(c1.at(70) == "seventy");
187        assert(c1.at(80) == "eighty");
188        assert(c1.hash_function() == Hash(2));
189        assert(c1.key_eq() == Compare(2));
190        assert(c1.get_allocator() == Alloc(1));
191        assert(std::distance(c1.begin(), c1.end()) == c1.size());
192        assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
193        assert(c1.max_load_factor() == 2);
194
195        assert(c2.bucket_count() >= 5);
196        assert(c2.size() == 4);
197        assert(c2.at(1) == "one");
198        assert(c2.at(2) == "two");
199        assert(c2.at(3) == "three");
200        assert(c2.at(4) == "four");
201        assert(c2.hash_function() == Hash(1));
202        assert(c2.key_eq() == Compare(1));
203        assert(c2.get_allocator() == Alloc(2));
204        assert(std::distance(c2.begin(), c2.end()) == c2.size());
205        assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
206        assert(c2.max_load_factor() == 1);
207    }
208
209    {
210        typedef test_hash<std::hash<int> > Hash;
211        typedef test_compare<std::equal_to<int> > Compare;
212        typedef other_allocator<std::pair<const int, std::string> > Alloc;
213        typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
214        typedef std::pair<int, std::string> P;
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, "fourty"),
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) == "fourty");
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, "fourty"),
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) == "fourty");
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}
390