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