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 "../../../check_consecutive.h" |
26 | #include "../../../test_compare.h" |
27 | #include "../../../test_hash.h" |
28 | #include "test_allocator.h" |
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 | swap(c1, 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 | C::iterator it2 = c2.begin(); |
80 | swap(x&: c1, y&: c2); |
81 | |
82 | assert(c1.bucket_count() >= 8); |
83 | assert(c1.size() == 8); |
84 | assert(c1.find(10)->second == "ten" ); |
85 | assert(c1.find(20)->second == "twenty" ); |
86 | assert(c1.find(30)->second == "thirty" ); |
87 | assert(c1.find(40)->second == "forty" ); |
88 | assert(c1.find(50)->second == "fifty" ); |
89 | assert(c1.find(60)->second == "sixty" ); |
90 | assert(c1.find(70)->second == "seventy" ); |
91 | assert(c1.find(80)->second == "eighty" ); |
92 | assert(c1.hash_function() == Hash(2)); |
93 | assert(c1.key_eq() == Compare(2)); |
94 | assert(c1.get_allocator().get_id() == 1); |
95 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
96 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
97 | assert(c1.max_load_factor() == 2); |
98 | assert(it2 == c1.begin()); // Iterators are not invalidated |
99 | |
100 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
101 | assert(c2.size() == 0); |
102 | assert(c2.hash_function() == Hash(1)); |
103 | assert(c2.key_eq() == Compare(1)); |
104 | assert(c2.get_allocator().get_id() == 2); |
105 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
106 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
107 | assert(c2.max_load_factor() == 1); |
108 | } |
109 | { |
110 | typedef test_hash<int> Hash; |
111 | typedef test_equal_to<int> Compare; |
112 | typedef test_allocator<std::pair<const int, std::string> > Alloc; |
113 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
114 | typedef std::pair<int, std::string> P; |
115 | P a1[] = { |
116 | P(1, "one" ), |
117 | P(2, "two" ), |
118 | P(3, "three" ), |
119 | P(4, "four" ), |
120 | P(1, "four" ), |
121 | P(2, "four" ), |
122 | }; |
123 | C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1, 1)); |
124 | C c2(0, Hash(2), Compare(2), Alloc(1, 2)); |
125 | c2.max_load_factor(z: 2); |
126 | C::iterator it1 = c1.begin(); |
127 | swap(x&: c1, y&: c2); |
128 | |
129 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
130 | assert(c1.size() == 0); |
131 | assert(c1.hash_function() == Hash(2)); |
132 | assert(c1.key_eq() == Compare(2)); |
133 | assert(c1.get_allocator().get_id() == 1); |
134 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
135 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
136 | assert(c1.max_load_factor() == 2); |
137 | |
138 | assert(c2.bucket_count() >= 6); |
139 | assert(c2.size() == 6); |
140 | std::multiset<std::string> s; |
141 | s.insert(x: "one" ); |
142 | s.insert(x: "four" ); |
143 | CheckConsecutiveKeys<C::const_iterator>(pos: c2.find(x: 1), end: c2.end(), key: 1, values&: s); |
144 | s.insert(x: "two" ); |
145 | s.insert(x: "four" ); |
146 | CheckConsecutiveKeys<C::const_iterator>(pos: c2.find(x: 2), end: c2.end(), key: 2, values&: s); |
147 | assert(c2.find(3)->second == "three" ); |
148 | assert(c2.find(4)->second == "four" ); |
149 | assert(c2.hash_function() == Hash(1)); |
150 | assert(c2.key_eq() == Compare(1)); |
151 | assert(c2.get_allocator().get_id() == 2); |
152 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
153 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
154 | assert(c2.max_load_factor() == 1); |
155 | assert(it1 == c2.begin()); // Iterators are not invalidated |
156 | } |
157 | { |
158 | typedef test_hash<int> Hash; |
159 | typedef test_equal_to<int> Compare; |
160 | typedef test_allocator<std::pair<const int, std::string> > Alloc; |
161 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
162 | typedef std::pair<int, std::string> P; |
163 | P a1[] = { |
164 | P(1, "one" ), |
165 | P(2, "two" ), |
166 | P(3, "three" ), |
167 | P(4, "four" ), |
168 | P(1, "four" ), |
169 | P(2, "four" ), |
170 | }; |
171 | P a2[] = { |
172 | P(10, "ten" ), |
173 | P(20, "twenty" ), |
174 | P(30, "thirty" ), |
175 | P(40, "forty" ), |
176 | P(50, "fifty" ), |
177 | P(60, "sixty" ), |
178 | P(70, "seventy" ), |
179 | P(80, "eighty" ), |
180 | }; |
181 | C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1, 1)); |
182 | C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(1, 2)); |
183 | c2.max_load_factor(z: 2); |
184 | C::iterator it1 = c1.begin(); |
185 | C::iterator it2 = c2.begin(); |
186 | swap(x&: c1, y&: c2); |
187 | |
188 | assert(c1.bucket_count() >= 8); |
189 | assert(c1.size() == 8); |
190 | assert(c1.find(10)->second == "ten" ); |
191 | assert(c1.find(20)->second == "twenty" ); |
192 | assert(c1.find(30)->second == "thirty" ); |
193 | assert(c1.find(40)->second == "forty" ); |
194 | assert(c1.find(50)->second == "fifty" ); |
195 | assert(c1.find(60)->second == "sixty" ); |
196 | assert(c1.find(70)->second == "seventy" ); |
197 | assert(c1.find(80)->second == "eighty" ); |
198 | assert(c1.hash_function() == Hash(2)); |
199 | assert(c1.key_eq() == Compare(2)); |
200 | assert(c1.get_allocator().get_id() == 1); |
201 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
202 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
203 | assert(c1.max_load_factor() == 2); |
204 | assert(it2 == c1.begin()); // Iterators are not invalidated |
205 | |
206 | assert(c2.bucket_count() >= 6); |
207 | assert(c2.size() == 6); |
208 | std::multiset<std::string> s; |
209 | s.insert(x: "one" ); |
210 | s.insert(x: "four" ); |
211 | CheckConsecutiveKeys<C::const_iterator>(pos: c2.find(x: 1), end: c2.end(), key: 1, values&: s); |
212 | s.insert(x: "two" ); |
213 | s.insert(x: "four" ); |
214 | CheckConsecutiveKeys<C::const_iterator>(pos: c2.find(x: 2), end: c2.end(), key: 2, values&: s); |
215 | assert(c2.find(3)->second == "three" ); |
216 | assert(c2.find(4)->second == "four" ); |
217 | assert(c2.hash_function() == Hash(1)); |
218 | assert(c2.key_eq() == Compare(1)); |
219 | assert(c2.get_allocator().get_id() == 2); |
220 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
221 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
222 | assert(c2.max_load_factor() == 1); |
223 | assert(it1 == c2.begin()); // Iterators are not invalidated |
224 | } |
225 | |
226 | { |
227 | typedef test_hash<int> Hash; |
228 | typedef test_equal_to<int> Compare; |
229 | typedef other_allocator<std::pair<const int, std::string> > Alloc; |
230 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
231 | C c1(0, Hash(1), Compare(1), Alloc(1)); |
232 | C c2(0, Hash(2), Compare(2), Alloc(2)); |
233 | c2.max_load_factor(z: 2); |
234 | swap(x&: c1, y&: c2); |
235 | |
236 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
237 | assert(c1.size() == 0); |
238 | assert(c1.hash_function() == Hash(2)); |
239 | assert(c1.key_eq() == Compare(2)); |
240 | assert(c1.get_allocator() == Alloc(2)); |
241 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
242 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
243 | assert(c1.max_load_factor() == 2); |
244 | |
245 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
246 | assert(c2.size() == 0); |
247 | assert(c2.hash_function() == Hash(1)); |
248 | assert(c2.key_eq() == Compare(1)); |
249 | assert(c2.get_allocator() == Alloc(1)); |
250 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
251 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
252 | assert(c2.max_load_factor() == 1); |
253 | } |
254 | { |
255 | typedef test_hash<int> Hash; |
256 | typedef test_equal_to<int> Compare; |
257 | typedef other_allocator<std::pair<const int, std::string> > Alloc; |
258 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
259 | typedef std::pair<int, std::string> P; |
260 | P a2[] = { |
261 | P(10, "ten" ), |
262 | P(20, "twenty" ), |
263 | P(30, "thirty" ), |
264 | P(40, "forty" ), |
265 | P(50, "fifty" ), |
266 | P(60, "sixty" ), |
267 | P(70, "seventy" ), |
268 | P(80, "eighty" ), |
269 | }; |
270 | C c1(0, Hash(1), Compare(1), Alloc(1)); |
271 | C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(2)); |
272 | c2.max_load_factor(z: 2); |
273 | swap(x&: c1, y&: c2); |
274 | |
275 | assert(c1.bucket_count() >= 8); |
276 | assert(c1.size() == 8); |
277 | assert(c1.find(10)->second == "ten" ); |
278 | assert(c1.find(20)->second == "twenty" ); |
279 | assert(c1.find(30)->second == "thirty" ); |
280 | assert(c1.find(40)->second == "forty" ); |
281 | assert(c1.find(50)->second == "fifty" ); |
282 | assert(c1.find(60)->second == "sixty" ); |
283 | assert(c1.find(70)->second == "seventy" ); |
284 | assert(c1.find(80)->second == "eighty" ); |
285 | assert(c1.hash_function() == Hash(2)); |
286 | assert(c1.key_eq() == Compare(2)); |
287 | assert(c1.get_allocator() == Alloc(2)); |
288 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
289 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
290 | assert(c1.max_load_factor() == 2); |
291 | |
292 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
293 | assert(c2.size() == 0); |
294 | assert(c2.hash_function() == Hash(1)); |
295 | assert(c2.key_eq() == Compare(1)); |
296 | assert(c2.get_allocator() == Alloc(1)); |
297 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
298 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
299 | assert(c2.max_load_factor() == 1); |
300 | } |
301 | { |
302 | typedef test_hash<int> Hash; |
303 | typedef test_equal_to<int> Compare; |
304 | typedef other_allocator<std::pair<const int, std::string> > Alloc; |
305 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
306 | typedef std::pair<int, std::string> P; |
307 | P a1[] = { |
308 | P(1, "one" ), |
309 | P(2, "two" ), |
310 | P(3, "three" ), |
311 | P(4, "four" ), |
312 | P(1, "four" ), |
313 | P(2, "four" ), |
314 | }; |
315 | C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1)); |
316 | C c2(0, Hash(2), Compare(2), Alloc(2)); |
317 | c2.max_load_factor(z: 2); |
318 | swap(x&: c1, y&: c2); |
319 | |
320 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
321 | assert(c1.size() == 0); |
322 | assert(c1.hash_function() == Hash(2)); |
323 | assert(c1.key_eq() == Compare(2)); |
324 | assert(c1.get_allocator() == Alloc(2)); |
325 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
326 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
327 | assert(c1.max_load_factor() == 2); |
328 | |
329 | assert(c2.bucket_count() >= 6); |
330 | assert(c2.size() == 6); |
331 | std::multiset<std::string> s; |
332 | s.insert(x: "one" ); |
333 | s.insert(x: "four" ); |
334 | CheckConsecutiveKeys<C::const_iterator>(pos: c2.find(x: 1), end: c2.end(), key: 1, values&: s); |
335 | s.insert(x: "two" ); |
336 | s.insert(x: "four" ); |
337 | CheckConsecutiveKeys<C::const_iterator>(pos: c2.find(x: 2), end: c2.end(), key: 2, values&: s); |
338 | assert(c2.find(3)->second == "three" ); |
339 | assert(c2.find(4)->second == "four" ); |
340 | assert(c2.hash_function() == Hash(1)); |
341 | assert(c2.key_eq() == Compare(1)); |
342 | assert(c2.get_allocator() == Alloc(1)); |
343 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
344 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
345 | assert(c2.max_load_factor() == 1); |
346 | } |
347 | { |
348 | typedef test_hash<int> Hash; |
349 | typedef test_equal_to<int> Compare; |
350 | typedef other_allocator<std::pair<const int, std::string> > Alloc; |
351 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
352 | typedef std::pair<int, std::string> P; |
353 | P a1[] = { |
354 | P(1, "one" ), |
355 | P(2, "two" ), |
356 | P(3, "three" ), |
357 | P(4, "four" ), |
358 | P(1, "four" ), |
359 | P(2, "four" ), |
360 | }; |
361 | P a2[] = { |
362 | P(10, "ten" ), |
363 | P(20, "twenty" ), |
364 | P(30, "thirty" ), |
365 | P(40, "forty" ), |
366 | P(50, "fifty" ), |
367 | P(60, "sixty" ), |
368 | P(70, "seventy" ), |
369 | P(80, "eighty" ), |
370 | }; |
371 | C c1(std::begin(arr&: a1), std::end(arr&: a1), 0, Hash(1), Compare(1), Alloc(1)); |
372 | C c2(std::begin(arr&: a2), std::end(arr&: a2), 0, Hash(2), Compare(2), Alloc(2)); |
373 | c2.max_load_factor(z: 2); |
374 | swap(x&: c1, y&: c2); |
375 | |
376 | assert(c1.bucket_count() >= 8); |
377 | assert(c1.size() == 8); |
378 | assert(c1.find(10)->second == "ten" ); |
379 | assert(c1.find(20)->second == "twenty" ); |
380 | assert(c1.find(30)->second == "thirty" ); |
381 | assert(c1.find(40)->second == "forty" ); |
382 | assert(c1.find(50)->second == "fifty" ); |
383 | assert(c1.find(60)->second == "sixty" ); |
384 | assert(c1.find(70)->second == "seventy" ); |
385 | assert(c1.find(80)->second == "eighty" ); |
386 | assert(c1.hash_function() == Hash(2)); |
387 | assert(c1.key_eq() == Compare(2)); |
388 | assert(c1.get_allocator() == Alloc(2)); |
389 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
390 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
391 | assert(c1.max_load_factor() == 2); |
392 | |
393 | assert(c2.bucket_count() >= 6); |
394 | assert(c2.size() == 6); |
395 | std::multiset<std::string> s; |
396 | s.insert(x: "one" ); |
397 | s.insert(x: "four" ); |
398 | CheckConsecutiveKeys<C::const_iterator>(pos: c2.find(x: 1), end: c2.end(), key: 1, values&: s); |
399 | s.insert(x: "two" ); |
400 | s.insert(x: "four" ); |
401 | CheckConsecutiveKeys<C::const_iterator>(pos: c2.find(x: 2), end: c2.end(), key: 2, values&: s); |
402 | assert(c2.find(3)->second == "three" ); |
403 | assert(c2.find(4)->second == "four" ); |
404 | assert(c2.hash_function() == Hash(1)); |
405 | assert(c2.key_eq() == Compare(1)); |
406 | assert(c2.get_allocator() == Alloc(1)); |
407 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
408 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
409 | assert(c2.max_load_factor() == 1); |
410 | } |
411 | #if TEST_STD_VER >= 11 |
412 | { |
413 | typedef test_hash<int> Hash; |
414 | typedef test_equal_to<int> Compare; |
415 | typedef min_allocator<std::pair<const int, std::string> > Alloc; |
416 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
417 | C c1(0, Hash(1), Compare(1), Alloc()); |
418 | C c2(0, Hash(2), Compare(2), Alloc()); |
419 | c2.max_load_factor(2); |
420 | swap(c1, c2); |
421 | |
422 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
423 | assert(c1.size() == 0); |
424 | assert(c1.hash_function() == Hash(2)); |
425 | assert(c1.key_eq() == Compare(2)); |
426 | assert(c1.get_allocator() == Alloc()); |
427 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
428 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
429 | assert(c1.max_load_factor() == 2); |
430 | |
431 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
432 | assert(c2.size() == 0); |
433 | assert(c2.hash_function() == Hash(1)); |
434 | assert(c2.key_eq() == Compare(1)); |
435 | assert(c2.get_allocator() == Alloc()); |
436 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
437 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
438 | assert(c2.max_load_factor() == 1); |
439 | } |
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 | typedef std::pair<int, std::string> P; |
446 | P a2[] = { |
447 | P(10, "ten" ), |
448 | P(20, "twenty" ), |
449 | P(30, "thirty" ), |
450 | P(40, "forty" ), |
451 | P(50, "fifty" ), |
452 | P(60, "sixty" ), |
453 | P(70, "seventy" ), |
454 | P(80, "eighty" ), |
455 | }; |
456 | C c1(0, Hash(1), Compare(1), Alloc()); |
457 | C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc()); |
458 | c2.max_load_factor(2); |
459 | swap(c1, c2); |
460 | |
461 | assert(c1.bucket_count() >= 8); |
462 | assert(c1.size() == 8); |
463 | assert(c1.find(10)->second == "ten" ); |
464 | assert(c1.find(20)->second == "twenty" ); |
465 | assert(c1.find(30)->second == "thirty" ); |
466 | assert(c1.find(40)->second == "forty" ); |
467 | assert(c1.find(50)->second == "fifty" ); |
468 | assert(c1.find(60)->second == "sixty" ); |
469 | assert(c1.find(70)->second == "seventy" ); |
470 | assert(c1.find(80)->second == "eighty" ); |
471 | assert(c1.hash_function() == Hash(2)); |
472 | assert(c1.key_eq() == Compare(2)); |
473 | assert(c1.get_allocator() == Alloc()); |
474 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
475 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
476 | assert(c1.max_load_factor() == 2); |
477 | |
478 | LIBCPP_ASSERT(c2.bucket_count() == 0); |
479 | assert(c2.size() == 0); |
480 | assert(c2.hash_function() == Hash(1)); |
481 | assert(c2.key_eq() == Compare(1)); |
482 | assert(c2.get_allocator() == Alloc()); |
483 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
484 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
485 | assert(c2.max_load_factor() == 1); |
486 | } |
487 | { |
488 | typedef test_hash<int> Hash; |
489 | typedef test_equal_to<int> Compare; |
490 | typedef min_allocator<std::pair<const int, std::string> > Alloc; |
491 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
492 | typedef std::pair<int, std::string> P; |
493 | P a1[] = { |
494 | P(1, "one" ), |
495 | P(2, "two" ), |
496 | P(3, "three" ), |
497 | P(4, "four" ), |
498 | P(1, "four" ), |
499 | P(2, "four" ), |
500 | }; |
501 | C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc()); |
502 | C c2(0, Hash(2), Compare(2), Alloc()); |
503 | c2.max_load_factor(2); |
504 | swap(c1, c2); |
505 | |
506 | LIBCPP_ASSERT(c1.bucket_count() == 0); |
507 | assert(c1.size() == 0); |
508 | assert(c1.hash_function() == Hash(2)); |
509 | assert(c1.key_eq() == Compare(2)); |
510 | assert(c1.get_allocator() == Alloc()); |
511 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
512 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
513 | assert(c1.max_load_factor() == 2); |
514 | |
515 | assert(c2.bucket_count() >= 6); |
516 | assert(c2.size() == 6); |
517 | std::multiset<std::string> s; |
518 | s.insert("one" ); |
519 | s.insert("four" ); |
520 | CheckConsecutiveKeys<C::const_iterator>(c2.find(1), c2.end(), 1, s); |
521 | s.insert("two" ); |
522 | s.insert("four" ); |
523 | CheckConsecutiveKeys<C::const_iterator>(c2.find(2), c2.end(), 2, s); |
524 | assert(c2.find(3)->second == "three" ); |
525 | assert(c2.find(4)->second == "four" ); |
526 | assert(c2.hash_function() == Hash(1)); |
527 | assert(c2.key_eq() == Compare(1)); |
528 | assert(c2.get_allocator() == Alloc()); |
529 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
530 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
531 | assert(c2.max_load_factor() == 1); |
532 | } |
533 | { |
534 | typedef test_hash<int> Hash; |
535 | typedef test_equal_to<int> Compare; |
536 | typedef min_allocator<std::pair<const int, std::string> > Alloc; |
537 | typedef std::unordered_multimap<int, std::string, Hash, Compare, Alloc> C; |
538 | typedef std::pair<int, std::string> P; |
539 | P a1[] = { |
540 | P(1, "one" ), |
541 | P(2, "two" ), |
542 | P(3, "three" ), |
543 | P(4, "four" ), |
544 | P(1, "four" ), |
545 | P(2, "four" ), |
546 | }; |
547 | P a2[] = { |
548 | P(10, "ten" ), |
549 | P(20, "twenty" ), |
550 | P(30, "thirty" ), |
551 | P(40, "forty" ), |
552 | P(50, "fifty" ), |
553 | P(60, "sixty" ), |
554 | P(70, "seventy" ), |
555 | P(80, "eighty" ), |
556 | }; |
557 | C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc()); |
558 | C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc()); |
559 | c2.max_load_factor(2); |
560 | swap(c1, c2); |
561 | |
562 | assert(c1.bucket_count() >= 8); |
563 | assert(c1.size() == 8); |
564 | assert(c1.find(10)->second == "ten" ); |
565 | assert(c1.find(20)->second == "twenty" ); |
566 | assert(c1.find(30)->second == "thirty" ); |
567 | assert(c1.find(40)->second == "forty" ); |
568 | assert(c1.find(50)->second == "fifty" ); |
569 | assert(c1.find(60)->second == "sixty" ); |
570 | assert(c1.find(70)->second == "seventy" ); |
571 | assert(c1.find(80)->second == "eighty" ); |
572 | assert(c1.hash_function() == Hash(2)); |
573 | assert(c1.key_eq() == Compare(2)); |
574 | assert(c1.get_allocator() == Alloc()); |
575 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
576 | assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size()); |
577 | assert(c1.max_load_factor() == 2); |
578 | |
579 | assert(c2.bucket_count() >= 6); |
580 | assert(c2.size() == 6); |
581 | std::multiset<std::string> s; |
582 | s.insert("one" ); |
583 | s.insert("four" ); |
584 | CheckConsecutiveKeys<C::const_iterator>(c2.find(1), c2.end(), 1, s); |
585 | s.insert("two" ); |
586 | s.insert("four" ); |
587 | CheckConsecutiveKeys<C::const_iterator>(c2.find(2), c2.end(), 2, s); |
588 | assert(c2.find(3)->second == "three" ); |
589 | assert(c2.find(4)->second == "four" ); |
590 | assert(c2.hash_function() == Hash(1)); |
591 | assert(c2.key_eq() == Compare(1)); |
592 | assert(c2.get_allocator() == Alloc()); |
593 | assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size()); |
594 | assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size()); |
595 | assert(c2.max_load_factor() == 1); |
596 | } |
597 | #endif |
598 | |
599 | return 0; |
600 | } |
601 | |