Debian Codegraph @

libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/assign_copy.pass.cpp

raw
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_map
14
15 // unordered_map& operator=(const unordered_map& u);
16
17 #include <algorithm>
18 #include <cassert>
19 #include <cfloat>
20 #include <cmath>
21 #include <cstddef>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25
26 #include "test_macros.h"
27 #include "../../../test_compare.h"
28 #include "../../../test_hash.h"
29 #include "test_allocator.h"
30 #include "min_allocator.h"
31
32 template <class T>
33 class tracking_allocator {
34 std::vector<void*>* allocs_;
35
36 template <class U>
37 friend class tracking_allocator;
38
39 public:
40 using value_type = T;
41 using propagate_on_container_copy_assignment = std::true_type;
42
43 tracking_allocator(std::vector<void*>& allocs) : allocs_(&allocs) {}
44
45 template <class U>
46 tracking_allocator(const tracking_allocator<U>& other) : allocs_(other.allocs_) {}
47
48 T* allocate(std::size_t n) {
49 T* allocation = std::allocator<T>().allocate(n);
50 allocs_->push_back(allocation);
51 return allocation;
52 }
53
54 void deallocate(T* ptr, std::size_t n) TEST_NOEXCEPT {
55 auto res = std::remove(allocs_->begin(), allocs_->end(), ptr);
56 assert(res != allocs_->end() && "Trying to deallocate memory from different allocator?");
57 allocs_->erase(res);
58 std::allocator<T>().deallocate(ptr, n);
59 }
60
61 friend bool operator==(const tracking_allocator& lhs, const tracking_allocator& rhs) {
62 return lhs.allocs_ == rhs.allocs_;
63 }
64
65 friend bool operator!=(const tracking_allocator& lhs, const tracking_allocator& rhs) {
66 return lhs.allocs_ != rhs.allocs_;
67 }
68 };
69
70 struct NoOp {
71 void operator()() {}
72 };
73
74 template <class Alloc, class AllocatorInvariant = NoOp>
75 void test_alloc(const Alloc& lhs_alloc = Alloc(),
76 const Alloc& rhs_alloc = Alloc(),
77 AllocatorInvariant check_alloc_invariant = NoOp()) {
78 { // Test empty/non-empty combinations
79 { // assign from a non-empty container into an empty one
80 using V = std::pair<const int, int>;
81 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
82
83 V arr[] = {V(1, 1), V(2, 3), V(4, 4), V(5, 2)};
84 const Map orig(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
85 Map copy(lhs_alloc);
86 copy = orig;
87 LIBCPP_ASSERT(copy.bucket_count() == 5);
88 assert(copy.size() == 4);
89 assert(copy.at(1) == 1);
90 assert(copy.at(2) == 3);
91 assert(copy.at(4) == 4);
92 assert(copy.at(5) == 2);
93 assert(std::next(copy.begin(), 4) == copy.end());
94
95 // Check that orig is still what is expected
96 LIBCPP_ASSERT(orig.bucket_count() == 5);
97 assert(orig.size() == 4);
98 assert(orig.at(1) == 1);
99 assert(orig.at(2) == 3);
100 assert(orig.at(4) == 4);
101 assert(orig.at(5) == 2);
102 assert(std::next(orig.begin(), 4) == orig.end());
103 }
104 check_alloc_invariant();
105 { // assign from an empty container into an empty one
106 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
107
108 const Map orig(rhs_alloc);
109 Map copy(lhs_alloc);
110 copy = orig;
111 LIBCPP_ASSERT(copy.bucket_count() == 0);
112 assert(copy.size() == 0);
113 assert(copy.begin() == copy.end());
114
115 // Check that orig is still what is expected
116 LIBCPP_ASSERT(orig.bucket_count() == 0);
117 assert(orig.size() == 0);
118 assert(orig.begin() == orig.end());
119 }
120 check_alloc_invariant();
121 { // assign from an empty container into a non-empty one
122 using V = std::pair<const int, int>;
123 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
124
125 V arr[] = {V(1, 1), V(2, 3), V(4, 4), V(5, 2)};
126 const Map orig(rhs_alloc);
127 Map copy(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), lhs_alloc);
128 copy = orig;
129 // Depending on whether the allocator is propagated the bucked count can change
130 LIBCPP_ASSERT(copy.bucket_count() == 5 || copy.bucket_count() == 0);
131 assert(copy.size() == 0);
132 assert(copy.begin() == copy.end());
133
134 // Check that orig is still what is expected
135 LIBCPP_ASSERT(orig.bucket_count() == 0);
136 assert(orig.size() == 0);
137 assert(orig.begin() == orig.end());
138 }
139 check_alloc_invariant();
140 }
141 { // Test empty/one-element copies. In our implementation that's a special case.
142 { // assign from a single-element container into an empty one
143 using V = std::pair<const int, int>;
144 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
145
146 V arr[] = {V(1, 1)};
147 const Map orig(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
148 Map copy(lhs_alloc);
149 copy = orig;
150 LIBCPP_ASSERT(copy.bucket_count() == 2);
151 assert(copy.size() == 1);
152 assert(copy.at(1) == 1);
153
154 // Check that orig is still what is expected
155 LIBCPP_ASSERT(orig.bucket_count() == 2);
156 assert(orig.size() == 1);
157 assert(orig.at(1) == 1);
158 }
159 { // assign from an empty container into a single-element one
160 using V = std::pair<const int, int>;
161 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
162
163 V arr[] = {V(1, 1)};
164 const Map orig(rhs_alloc);
165 Map copy(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), lhs_alloc);
166 copy = orig;
167 // Depending on whether the allocator is propagated the bucked count can change
168 LIBCPP_ASSERT(copy.bucket_count() == 2 || copy.bucket_count() == 0);
169 assert(copy.size() == 0);
170
171 // Check that orig is still what is expected
172 LIBCPP_ASSERT(orig.bucket_count() == 0);
173 assert(orig.size() == 0);
174 }
175 }
176 { // Ensure that self-assignment works correctly
177 { // with a non-empty map
178 using V = std::pair<const int, int>;
179 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
180
181 V arr[] = {V(1, 1), V(2, 3), V(4, 4), V(5, 2)};
182 Map orig(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
183 orig = static_cast<const Map&>(orig);
184 LIBCPP_ASSERT(orig.bucket_count() == 5);
185 assert(orig.size() == 4);
186 assert(orig.at(1) == 1);
187 assert(orig.at(2) == 3);
188 assert(orig.at(4) == 4);
189 assert(orig.at(5) == 2);
190 assert(std::next(orig.begin(), 4) == orig.end());
191 }
192 check_alloc_invariant();
193 { // with an empty map
194 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
195
196 Map orig(rhs_alloc);
197 orig = static_cast<const Map&>(orig);
198 LIBCPP_ASSERT(orig.bucket_count() == 0);
199 assert(orig.size() == 0);
200 assert(orig.begin() == orig.end());
201 }
202 check_alloc_invariant();
203 }
204 { // check assignment into a non-empty map
205 { // LHS already contains elements, but fewer than the RHS
206 using V = std::pair<const int, int>;
207 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
208
209 V lhs_arr[] = {V(1, 1), V(2, 3), V(4, 4), V(5, 2)};
210 const Map orig(begin(lhs_arr), end(lhs_arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
211
212 V rhs_arr[] = {V(10, 4), V(13, 5)};
213 Map copy(begin(rhs_arr), end(rhs_arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
214 copy = orig;
215 LIBCPP_ASSERT(copy.bucket_count() == 5);
216 assert(copy.size() == 4);
217 assert(copy.at(1) == 1);
218 assert(copy.at(2) == 3);
219 assert(copy.at(4) == 4);
220 assert(copy.at(5) == 2);
221 assert(std::next(copy.begin(), 4) == copy.end());
222
223 // Check that orig is still what is expected
224 LIBCPP_ASSERT(orig.bucket_count() == 5);
225 assert(orig.size() == 4);
226 assert(orig.at(1) == 1);
227 assert(orig.at(2) == 3);
228 assert(orig.at(4) == 4);
229 assert(orig.at(5) == 2);
230 assert(std::next(orig.begin(), 4) == orig.end());
231 }
232 check_alloc_invariant();
233 { // LHS contains the same number of elements as the RHS
234 using V = std::pair<const int, int>;
235 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
236
237 V lhs_arr[] = {V(1, 1), V(2, 3), V(4, 4), V(5, 2)};
238 const Map orig(begin(lhs_arr), end(lhs_arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
239
240 V rhs_arr[] = {V(10, 4), V(13, 5), V(12, 324), V(0, 54)};
241 Map copy(begin(rhs_arr), end(rhs_arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
242 copy = orig;
243 LIBCPP_ASSERT(copy.bucket_count() == 5);
244 assert(copy.size() == 4);
245 assert(copy.at(1) == 1);
246 assert(copy.at(2) == 3);
247 assert(copy.at(4) == 4);
248 assert(copy.at(5) == 2);
249 assert(std::next(copy.begin(), 4) == copy.end());
250
251 // Check that orig is still what is expected
252 LIBCPP_ASSERT(orig.bucket_count() == 5);
253 assert(orig.size() == 4);
254 assert(orig.at(1) == 1);
255 assert(orig.at(2) == 3);
256 assert(orig.at(4) == 4);
257 assert(orig.at(5) == 2);
258 assert(std::next(orig.begin(), 4) == orig.end());
259 }
260 check_alloc_invariant();
261 { // LHS already contains more elements than the RHS
262 using V = std::pair<const int, int>;
263 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
264
265 V lhs_arr[] = {V(1, 1), V(2, 3), V(4, 4), V(5, 2)};
266 const Map orig(begin(lhs_arr), end(lhs_arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
267
268 V rhs_arr[] = {V(10, 4), V(13, 5), V(12, 324), V(0, 54), V(50, 5), V(2, 5)};
269 Map copy(begin(rhs_arr), end(rhs_arr), 0, std::hash<int>(), std::equal_to<int>(), rhs_alloc);
270 copy = orig;
271 LIBCPP_NON_FROZEN_ASSERT(copy.bucket_count() == 5);
272 assert(copy.size() == 4);
273 assert(copy.at(1) == 1);
274 assert(copy.at(2) == 3);
275 assert(copy.at(4) == 4);
276 assert(copy.at(5) == 2);
277 assert(std::next(copy.begin(), 4) == copy.end());
278
279 // Check that orig is still what is expected
280 LIBCPP_ASSERT(orig.bucket_count() == 5);
281 assert(orig.size() == 4);
282 assert(orig.at(1) == 1);
283 assert(orig.at(2) == 3);
284 assert(orig.at(4) == 4);
285 assert(orig.at(5) == 2);
286 assert(std::next(orig.begin(), 4) == orig.end());
287 }
288 check_alloc_invariant();
289 }
290 }
291
292 void test() {
293 test_alloc<std::allocator<std::pair<const int, int> > >();
294 test_alloc<min_allocator<std::pair<const int, int> > >();
295
296 { // Make sure we're allocating/deallocating nodes with the correct allocator
297 // See https://llvm.org/PR29001 (report is for std::map, but the unordered containers have the same optimization)
298 class AssertEmpty {
299 std::vector<void*>* lhs_allocs_;
300 std::vector<void*>* rhs_allocs_;
301
302 public:
303 AssertEmpty(std::vector<void*>& lhs_allocs, std::vector<void*>& rhs_allocs)
304 : lhs_allocs_(&lhs_allocs), rhs_allocs_(&rhs_allocs) {}
305
306 void operator()() {
307 assert(lhs_allocs_->empty());
308 assert(rhs_allocs_->empty());
309 }
310 };
311
312 std::vector<void*> lhs_allocs;
313 std::vector<void*> rhs_allocs;
314 test_alloc<tracking_allocator<std::pair<const int, int> > >(
315 lhs_allocs, rhs_allocs, AssertEmpty(lhs_allocs, rhs_allocs));
316 }
317
318 { // Ensure that the hasher is copied
319 { // when the container is non-empty
320 using V = std::pair<const int, int>;
321 using Map = std::unordered_map<int, int, test_hash<int> >;
322
323 V arr[] = {V(1, 1), V(2, 2), V(3, 3)};
324 const Map orig(begin(arr), end(arr), 0, test_hash<int>(5));
325 Map copy;
326 copy = orig;
327 assert(copy.size() == 3);
328 assert(copy.hash_function() == test_hash<int>(5));
329
330 // Check that orig is still what is expected
331 assert(orig.size() == 3);
332 assert(orig.hash_function() == test_hash<int>(5));
333 }
334 { // when the container is empty
335 using Map = std::unordered_map<int, int, test_hash<int> >;
336
337 const Map orig(0, test_hash<int>(5));
338 Map copy;
339 copy = orig;
340 assert(copy.empty());
341 assert(copy.hash_function() == test_hash<int>(5));
342
343 // Check that orig is still what is expected
344 assert(orig.empty());
345 assert(orig.hash_function() == test_hash<int>(5));
346 }
347 }
348
349 { // Ensure that the equality comparator is copied
350 { // when the container is non-empty
351 using V = std::pair<const int, int>;
352 using Map = std::unordered_map<int, int, std::hash<int>, test_equal_to<int> >;
353
354 V arr[] = {V(1, 1), V(2, 2), V(3, 3)};
355 const Map orig(begin(arr), end(arr), 0, std::hash<int>(), test_equal_to<int>(23));
356 Map copy;
357 copy = orig;
358 assert(copy.size() == 3);
359 assert(copy.key_eq() == test_equal_to<int>(23));
360
361 // Check that orig is still what is expected
362 assert(orig.size() == 3);
363 assert(copy.key_eq() == test_equal_to<int>(23));
364 }
365 { // when the container is empty
366 using Map = std::unordered_map<int, int, std::hash<int>, test_equal_to<int> >;
367
368 const Map orig(0, std::hash<int>(), test_equal_to<int>(23));
369 Map copy;
370 copy = orig;
371 assert(copy.size() == 0);
372 assert(copy.key_eq() == test_equal_to<int>(23));
373
374 // Check that orig is still what is expected
375 assert(orig.size() == 0);
376 assert(copy.key_eq() == test_equal_to<int>(23));
377 }
378 }
379
380 { // Ensure that the max load factor is copied
381 { // when the container is non-empty
382 using V = std::pair<const int, int>;
383 using Map = std::unordered_map<int, int>;
384
385 V arr[] = {V(1, 1), V(2, 2), V(3, 3)};
386 Map orig(begin(arr), end(arr));
387 orig.max_load_factor(33.f);
388 Map copy;
389 copy = orig;
390 assert(copy.size() == 3);
391 assert(copy.max_load_factor() == 33.f);
392
393 // Check that orig is still what is expected
394 assert(orig.size() == 3);
395 assert(orig.max_load_factor() == 33.f);
396 }
397 { // when the container is empty
398 using Map = std::unordered_map<int, int>;
399
400 Map orig;
401 orig.max_load_factor(17.f);
402 Map copy;
403 copy = orig;
404 assert(copy.size() == 0);
405 assert(copy.max_load_factor() == 17.f);
406
407 // Check that orig is still what is expected
408 assert(orig.size() == 0);
409 assert(orig.max_load_factor() == 17.f);
410 }
411 }
412
413 { // Check that pocca is handled properly
414 { // pocca == true_type
415 { // when the container is non-empty
416 using V = std::pair<const int, int>;
417 using Alloc = other_allocator<V>;
418 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
419
420 V arr[] = {V(1, 1), V(2, 2), V(3, 3)};
421 const Map orig(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), Alloc(3));
422 Map copy(Alloc(1));
423 copy = orig;
424 assert(copy.get_allocator() == Alloc(3));
425 }
426 { // when the container is empty
427 using Alloc = other_allocator<std::pair<const int, int> >;
428 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
429
430 const Map orig(Alloc(3));
431 Map copy(Alloc(1));
432 copy = orig;
433 assert(copy.get_allocator() == Alloc(3));
434 }
435 }
436 { // pocca == false_type
437 { // when the container is non-empty
438 using V = std::pair<const int, int>;
439 using Alloc = test_allocator<V>;
440 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
441
442 V arr[] = {V(1, 1), V(2, 2), V(3, 3)};
443 const Map orig(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), Alloc(3));
444 Map copy(Alloc(1));
445 copy = orig;
446 assert(copy.get_allocator() == Alloc(1));
447 }
448 { // when the container is empty
449 using Alloc = test_allocator<std::pair<const int, int> >;
450 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
451
452 const Map orig(Alloc(3));
453 Map copy(Alloc(1));
454 copy = orig;
455 assert(copy.get_allocator() == Alloc(1));
456 }
457 }
458 }
459 }
460
461 int main(int, char**) {
462 test();
463
464 return 0;
465 }