1
2// Copyright 2008-2009 Daniel James.
3// Copyright 2022-2023 Christian Mazakas.
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or move at http://www.boost.org/LICENSE_1_0.txt)
6
7#include "../helpers/unordered.hpp"
8
9#include "../helpers/equivalent.hpp"
10#include "../helpers/invariants.hpp"
11#include "../helpers/random_values.hpp"
12#include "../helpers/test.hpp"
13#include "../helpers/tracker.hpp"
14#include "../objects/cxx11_allocator.hpp"
15#include "../objects/test.hpp"
16
17#include <boost/core/ignore_unused.hpp>
18#include <iterator>
19
20#if defined(BOOST_MSVC)
21#pragma warning(disable : 4127) // conditional expression is constant
22#endif
23
24namespace move_tests {
25 test::seed_t initialize_seed(98624);
26#define BOOST_UNORDERED_TEST_MOVING 1
27
28 template <class T> T empty(T*) { return T(); }
29
30 template <class T>
31 T create(test::random_values<T> const& v, test::object_count& count)
32 {
33 T x(v.begin(), v.end());
34 count = test::global_object_count;
35 return x;
36 }
37
38 template <class T>
39 T create(test::random_values<T> const& v, test::object_count& count,
40 typename T::hasher hf, typename T::key_equal eq,
41 typename T::allocator_type al, float mlf)
42 {
43 T x(0, hf, eq, al);
44 x.max_load_factor(mlf);
45 x.insert(v.begin(), v.end());
46 count = test::global_object_count;
47 return x;
48 }
49
50 template <class T>
51 void move_construct_tests1(T* ptr, test::random_generator const& generator)
52 {
53 typename T::hasher hf;
54 typename T::key_equal eq;
55 typename T::allocator_type al;
56
57 {
58 test::check_instances check_;
59
60 T y(empty(ptr));
61 BOOST_TEST(y.empty());
62 BOOST_TEST(test::equivalent(y.hash_function(), hf));
63 BOOST_TEST(test::equivalent(y.key_eq(), eq));
64 BOOST_TEST(test::equivalent(y.get_allocator(), al));
65#ifdef BOOST_UNORDERED_FOA_TESTS
66 BOOST_TEST(y.max_load_factor() == 0.875);
67#else
68 BOOST_TEST(y.max_load_factor() == 1.0);
69#endif
70 test::check_equivalent_keys(y);
71
72#ifdef BOOST_UNORDERED_FOA_TESTS
73 using allocator_type = typename T::allocator_type;
74 using value_type =
75 typename boost::allocator_value_type<allocator_type>::type;
76 using pointer = typename boost::allocator_pointer<allocator_type>::type;
77 if (std::is_same<pointer, value_type*>::value) {
78 BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u);
79 }
80#else
81 BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u);
82#endif
83 }
84
85 {
86 test::check_instances check_;
87
88 test::random_values<T> v(1000, generator);
89 test::object_count count;
90 T y(create(v, count));
91#if defined(BOOST_HAS_NRVO)
92 BOOST_TEST(count == test::global_object_count);
93#endif
94 test::check_container(y, v);
95 test::check_equivalent_keys(y);
96 }
97 }
98
99 template <class T>
100 void move_assign_tests1(T* p, test::random_generator const& generator)
101 {
102 {
103 test::check_instances check_;
104
105 test::random_values<T> v(500, generator);
106 test::object_count count;
107 T y;
108 y = create(v, count);
109#if BOOST_UNORDERED_TEST_MOVING && defined(BOOST_HAS_NRVO)
110 BOOST_TEST(count == test::global_object_count);
111#endif
112 test::check_container(y, v);
113 test::check_equivalent_keys(y);
114 }
115
116 {
117 test::random_values<T> v;
118
119 T y;
120 y = empty(p);
121
122#ifdef BOOST_UNORDERED_FOA_TESTS
123 using allocator_type = typename T::allocator_type;
124 using value_type =
125 typename boost::allocator_value_type<allocator_type>::type;
126 using pointer = typename boost::allocator_pointer<allocator_type>::type;
127 if (std::is_same<pointer, value_type*>::value) {
128 BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u);
129 }
130#else
131 BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u);
132#endif
133 test::check_container(y, v);
134 test::check_equivalent_keys(y);
135 }
136 }
137
138 template <class T>
139 void move_construct_tests2(T*, test::random_generator const& generator)
140 {
141 typename T::hasher hf(1);
142 typename T::key_equal eq(1);
143 typename T::allocator_type al(1);
144 typename T::allocator_type al2(2);
145
146 test::object_count count;
147
148 {
149 test::check_instances check_;
150
151 test::random_values<T> v(500, generator);
152 T y(create(v, count, hf, eq, al, 0.5));
153#if defined(BOOST_HAS_NRVO)
154 BOOST_TEST(count == test::global_object_count);
155#endif
156 test::check_container(y, v);
157 BOOST_TEST(test::equivalent(y.hash_function(), hf));
158 BOOST_TEST(test::equivalent(y.key_eq(), eq));
159 BOOST_TEST(test::equivalent(y.get_allocator(), al));
160#ifdef BOOST_UNORDERED_FOA_TESTS
161 BOOST_TEST(y.max_load_factor() == 0.875);
162#else
163 BOOST_TEST(y.max_load_factor() == 0.5); // Not necessarily required.
164#endif
165 test::check_equivalent_keys(y);
166 }
167
168 {
169 test::check_instances check_;
170
171 // TODO: To do this correctly requires the fancy new allocator
172 // stuff.
173 test::random_values<T> v(500, generator);
174 T y(create(v, count, hf, eq, al, 2.0), al2);
175 BOOST_TEST(count != test::global_object_count);
176 test::check_container(y, v);
177 BOOST_TEST(test::equivalent(y.hash_function(), hf));
178 BOOST_TEST(test::equivalent(y.key_eq(), eq));
179 BOOST_TEST(test::equivalent(y.get_allocator(), al2));
180#ifdef BOOST_UNORDERED_FOA_TESTS
181 BOOST_TEST(y.max_load_factor() == 0.875);
182#else
183 BOOST_TEST(y.max_load_factor() == 2.0); // Not necessarily required.
184#endif
185 test::check_equivalent_keys(y);
186 }
187
188 {
189 test::check_instances check_;
190
191 test::random_values<T> v(25, generator);
192 T y(create(v, count, hf, eq, al, 1.0), al);
193 BOOST_TEST(count == test::global_object_count);
194
195 test::check_container(y, v);
196 BOOST_TEST(test::equivalent(y.hash_function(), hf));
197 BOOST_TEST(test::equivalent(y.key_eq(), eq));
198 BOOST_TEST(test::equivalent(y.get_allocator(), al));
199#ifdef BOOST_UNORDERED_FOA_TESTS
200 BOOST_TEST(y.max_load_factor() == 0.875);
201#else
202 BOOST_TEST(y.max_load_factor() == 1.0); // Not necessarily required.
203#endif
204 test::check_equivalent_keys(y);
205 }
206 }
207
208 template <class T>
209 void move_assign_tests2(T*, test::random_generator const& generator)
210 {
211 typename T::hasher hf(1);
212 typename T::key_equal eq(1);
213 typename T::allocator_type al1(1);
214 typename T::allocator_type al2(2);
215 typedef typename T::allocator_type allocator_type;
216
217 {
218 test::random_values<T> v(500, generator);
219 test::random_values<T> v2(0, generator);
220 T y(v.begin(), v.end(), 0, hf, eq, al1);
221 test::object_count count;
222 y = create(v2, count, hf, eq, al2, 2.0);
223 BOOST_TEST(y.empty());
224 test::check_container(y, v2);
225 test::check_equivalent_keys(y);
226#ifdef BOOST_UNORDERED_FOA_TESTS
227 BOOST_TEST(y.max_load_factor() == 0.875);
228#else
229 BOOST_TEST(y.max_load_factor() == 2.0);
230#endif
231
232#if defined(BOOST_HAS_NRVO)
233 if (BOOST_UNORDERED_TEST_MOVING
234 ? (bool)allocator_type::is_propagate_on_move
235 : (bool)allocator_type::is_propagate_on_assign) {
236 BOOST_TEST(test::equivalent(y.get_allocator(), al2));
237 } else {
238 BOOST_TEST(test::equivalent(y.get_allocator(), al1));
239 }
240#endif
241 }
242
243 {
244 test::random_values<T> v(500, generator);
245 test::object_count count;
246 T y(0, hf, eq, al1);
247 y = create(v, count, hf, eq, al2, 0.5);
248#if defined(BOOST_HAS_NRVO)
249 if (BOOST_UNORDERED_TEST_MOVING && allocator_type::is_propagate_on_move) {
250 BOOST_TEST(count == test::global_object_count);
251 }
252#endif
253 test::check_container(y, v);
254 test::check_equivalent_keys(y);
255#ifdef BOOST_UNORDERED_FOA_TESTS
256 BOOST_TEST(y.max_load_factor() == 0.875);
257#else
258 BOOST_TEST(y.max_load_factor() == 0.5);
259#endif
260
261#if defined(BOOST_HAS_NRVO)
262 if (BOOST_UNORDERED_TEST_MOVING
263 ? (bool)allocator_type::is_propagate_on_move
264 : (bool)allocator_type::is_propagate_on_assign) {
265 BOOST_TEST(test::equivalent(y.get_allocator(), al2));
266 } else {
267 BOOST_TEST(test::equivalent(y.get_allocator(), al1));
268 }
269#endif
270 }
271
272 {
273 test::random_values<T> v;
274 T y(0, hf, eq, al1);
275 T x(0, hf, eq, al2);
276 x.max_load_factor(0.25);
277
278#ifdef BOOST_UNORDERED_FOA_TESTS
279 {
280 using value_type =
281 typename boost::allocator_value_type<allocator_type>::type;
282 using pointer = typename boost::allocator_pointer<allocator_type>::type;
283 if (std::is_same<pointer, value_type*>::value) {
284 BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u);
285 }
286 }
287#else
288 BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u);
289#endif
290
291 y = std::move(x);
292
293#ifdef BOOST_UNORDERED_FOA_TESTS
294 {
295 using value_type =
296 typename boost::allocator_value_type<allocator_type>::type;
297 using pointer = typename boost::allocator_pointer<allocator_type>::type;
298 if (std::is_same<pointer, value_type*>::value) {
299 BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u);
300 }
301 }
302#else
303 BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u);
304#endif
305 test::check_container(y, v);
306 test::check_equivalent_keys(y);
307#ifdef BOOST_UNORDERED_FOA_TESTS
308 BOOST_TEST(y.max_load_factor() == 0.875);
309#else
310 BOOST_TEST(y.max_load_factor() == 0.25);
311#endif
312
313 if (BOOST_UNORDERED_TEST_MOVING
314 ? (bool)allocator_type::is_propagate_on_move
315 : (bool)allocator_type::is_propagate_on_assign) {
316 BOOST_TEST(test::equivalent(y.get_allocator(), al2));
317 } else {
318 BOOST_TEST(test::equivalent(y.get_allocator(), al1));
319 }
320 }
321
322 {
323 test::check_instances check_;
324
325 test::random_values<T> v(500, generator);
326 T y(0, hf, eq, al1);
327
328 T x(0, hf, eq, al2);
329 x.max_load_factor(0.25);
330 x.insert(v.begin(), v.end());
331
332 test::object_count count = test::global_object_count;
333 y = std::move(x);
334 if (BOOST_UNORDERED_TEST_MOVING && allocator_type::is_propagate_on_move) {
335 BOOST_TEST(count == test::global_object_count);
336 }
337 test::check_container(y, v);
338 test::check_equivalent_keys(y);
339#ifdef BOOST_UNORDERED_FOA_TESTS
340 BOOST_TEST(y.max_load_factor() == 0.875);
341#else
342 BOOST_TEST(y.max_load_factor() == 0.25);
343#endif
344
345 if (BOOST_UNORDERED_TEST_MOVING
346 ? (bool)allocator_type::is_propagate_on_move
347 : (bool)allocator_type::is_propagate_on_assign) {
348 BOOST_TEST(test::equivalent(y.get_allocator(), al2));
349 } else {
350 BOOST_TEST(test::equivalent(y.get_allocator(), al1));
351 }
352 }
353
354 {
355 test::check_instances check_;
356
357 test::random_values<T> v1(1000, generator);
358 test::random_values<T> v2(200, generator);
359
360 T x(0, hf, eq, al2);
361 x.max_load_factor(0.5);
362 x.insert(v2.begin(), v2.end());
363
364 test::object_count count1 = test::global_object_count;
365
366 T y(v1.begin(), v1.end(), 0, hf, eq, al1);
367 y = std::move(x);
368
369 test::object_count count2 = test::global_object_count;
370
371 if (BOOST_UNORDERED_TEST_MOVING && allocator_type::is_propagate_on_move) {
372 BOOST_TEST(count1.instances == test::global_object_count.instances);
373 BOOST_TEST(
374 count2.constructions == test::global_object_count.constructions);
375 }
376
377 test::check_container(y, v2);
378 test::check_equivalent_keys(y);
379#ifdef BOOST_UNORDERED_FOA_TESTS
380 BOOST_TEST(y.max_load_factor() == 0.875);
381#else
382 BOOST_TEST(y.max_load_factor() == 0.5);
383#endif
384
385 if (BOOST_UNORDERED_TEST_MOVING
386 ? (bool)allocator_type::is_propagate_on_move
387 : (bool)allocator_type::is_propagate_on_assign) {
388 BOOST_TEST(test::equivalent(y.get_allocator(), al2));
389 } else {
390 BOOST_TEST(test::equivalent(y.get_allocator(), al1));
391 }
392 }
393 }
394
395 using test::default_generator;
396 using test::generate_collisions;
397 using test::limited_range;
398
399#ifdef BOOST_UNORDERED_FOA_TESTS
400 boost::unordered_flat_map<test::object, test::object, test::hash,
401 test::equal_to,
402 std::allocator<std::pair<test::object const, test::object> > >*
403 test_map_std_alloc;
404
405 boost::unordered_flat_set<test::object, test::hash, test::equal_to,
406 test::allocator2<test::object> >* test_set;
407 boost::unordered_flat_map<test::object, test::object, test::hash,
408 test::equal_to,
409 test::allocator1<std::pair<test::object const, test::object> > >* test_map;
410
411 boost::unordered_flat_set<test::object, test::hash, test::equal_to,
412 test::cxx11_allocator<test::object, test::propagate_move> >*
413 test_set_prop_move;
414 boost::unordered_flat_map<test::object, test::object, test::hash,
415 test::equal_to,
416 test::cxx11_allocator<std::pair<test::object const, test::object>,
417 test::propagate_move> >* test_map_prop_move;
418
419 boost::unordered_flat_set<test::object, test::hash, test::equal_to,
420 test::cxx11_allocator<test::object, test::no_propagate_move> >*
421 test_set_no_prop_move;
422 boost::unordered_flat_map<test::object, test::object, test::hash,
423 test::equal_to,
424 test::cxx11_allocator<std::pair<test::object const, test::object>,
425 test::no_propagate_move> >* test_map_no_prop_move;
426
427 boost::unordered_node_map<test::object, test::object, test::hash,
428 test::equal_to,
429 std::allocator<std::pair<test::object const, test::object> > >*
430 test_node_map_std_alloc;
431
432 boost::unordered_node_set<test::object, test::hash, test::equal_to,
433 test::allocator2<test::object> >* test_node_set;
434 boost::unordered_node_map<test::object, test::object, test::hash,
435 test::equal_to,
436 test::allocator1<std::pair<test::object const, test::object> > >*
437 test_node_map;
438
439 boost::unordered_node_set<test::object, test::hash, test::equal_to,
440 test::cxx11_allocator<test::object, test::propagate_move> >*
441 test_node_set_prop_move;
442 boost::unordered_node_map<test::object, test::object, test::hash,
443 test::equal_to,
444 test::cxx11_allocator<std::pair<test::object const, test::object>,
445 test::propagate_move> >* test_node_map_prop_move;
446
447 boost::unordered_node_set<test::object, test::hash, test::equal_to,
448 test::cxx11_allocator<test::object, test::no_propagate_move> >*
449 test_node_set_no_prop_move;
450 boost::unordered_node_map<test::object, test::object, test::hash,
451 test::equal_to,
452 test::cxx11_allocator<std::pair<test::object const, test::object>,
453 test::no_propagate_move> >* test_node_map_no_prop_move;
454
455 // clang-format off
456 UNORDERED_TEST(move_construct_tests1,
457 ((test_map_std_alloc)(test_set)(test_map)
458 (test_set_prop_move)(test_map_prop_move)
459 (test_set_no_prop_move)(test_map_no_prop_move)
460 (test_node_map_std_alloc)(test_node_set)(test_node_map)
461 (test_node_set_prop_move)(test_node_map_prop_move)
462 (test_node_set_no_prop_move)(test_node_map_no_prop_move))(
463 (default_generator)(generate_collisions)(limited_range)))
464 UNORDERED_TEST(move_assign_tests1,
465 ((test_map_std_alloc)(test_set)(test_map)
466 (test_set_prop_move)(test_map_prop_move)
467 (test_set_no_prop_move)(test_map_no_prop_move)
468 (test_node_map_std_alloc)(test_node_set)(test_node_map)
469 (test_node_set_prop_move)(test_node_map_prop_move)
470 (test_node_set_no_prop_move)(test_node_map_no_prop_move))(
471 (default_generator)(generate_collisions)(limited_range)))
472 UNORDERED_TEST(move_construct_tests2,
473 ((test_set)(test_map)
474 (test_set_prop_move)(test_map_prop_move)
475 (test_set_no_prop_move)(test_map_no_prop_move)
476 (test_node_set)(test_node_map)
477 (test_node_set_prop_move)(test_node_map_prop_move)
478 (test_node_set_no_prop_move)(test_node_map_no_prop_move))(
479 (default_generator)(generate_collisions)(limited_range)))
480 UNORDERED_TEST(move_assign_tests2,
481 ((test_set)(test_map)
482 (test_set_prop_move)(test_map_prop_move)
483 (test_set_no_prop_move)(test_map_no_prop_move)
484 (test_node_set)(test_node_map)
485 (test_node_set_prop_move)(test_node_map_prop_move)
486 (test_node_set_no_prop_move)(test_node_map_no_prop_move))(
487 (default_generator)(generate_collisions)(limited_range)))
488 // clang-format on
489#else
490 boost::unordered_map<test::object, test::object, test::hash, test::equal_to,
491 std::allocator<std::pair<test::object const, test::object> > >*
492 test_map_std_alloc;
493
494 boost::unordered_set<test::object, test::hash, test::equal_to,
495 test::allocator2<test::object> >* test_set;
496 boost::unordered_multiset<test::object, test::hash, test::equal_to,
497 test::allocator1<test::object> >* test_multiset;
498 boost::unordered_map<test::object, test::object, test::hash, test::equal_to,
499 test::allocator1<std::pair<test::object const, test::object> > >* test_map;
500 boost::unordered_multimap<test::object, test::object, test::hash,
501 test::equal_to,
502 test::allocator2<std::pair<test::object const, test::object> > >*
503 test_multimap;
504
505 boost::unordered_set<test::object, test::hash, test::equal_to,
506 test::cxx11_allocator<test::object, test::propagate_move> >*
507 test_set_prop_move;
508 boost::unordered_multiset<test::object, test::hash, test::equal_to,
509 test::cxx11_allocator<test::object, test::propagate_move> >*
510 test_multiset_prop_move;
511 boost::unordered_map<test::object, test::object, test::hash, test::equal_to,
512 test::cxx11_allocator<std::pair<test::object const, test::object>,
513 test::propagate_move> >* test_map_prop_move;
514 boost::unordered_multimap<test::object, test::object, test::hash,
515 test::equal_to,
516 test::cxx11_allocator<std::pair<test::object const, test::object>,
517 test::propagate_move> >* test_multimap_prop_move;
518
519 boost::unordered_set<test::object, test::hash, test::equal_to,
520 test::cxx11_allocator<test::object, test::no_propagate_move> >*
521 test_set_no_prop_move;
522 boost::unordered_multiset<test::object, test::hash, test::equal_to,
523 test::cxx11_allocator<test::object, test::no_propagate_move> >*
524 test_multiset_no_prop_move;
525 boost::unordered_map<test::object, test::object, test::hash, test::equal_to,
526 test::cxx11_allocator<std::pair<test::object const, test::object>,
527 test::no_propagate_move> >* test_map_no_prop_move;
528 boost::unordered_multimap<test::object, test::object, test::hash,
529 test::equal_to,
530 test::cxx11_allocator<std::pair<test::object const, test::object>,
531 test::no_propagate_move> >* test_multimap_no_prop_move;
532
533 UNORDERED_TEST(move_construct_tests1,
534 ((test_map_std_alloc)(test_set)(test_multiset)(test_map)(test_multimap)(test_set_prop_move)(test_multiset_prop_move)(test_map_prop_move)(test_multimap_prop_move)(test_set_no_prop_move)(test_multiset_no_prop_move)(test_map_no_prop_move)(test_multimap_no_prop_move))(
535 (default_generator)(generate_collisions)(limited_range)))
536 UNORDERED_TEST(move_assign_tests1,
537 ((test_map_std_alloc)(test_set)(test_multiset)(test_map)(test_multimap)(test_set_prop_move)(test_multiset_prop_move)(test_map_prop_move)(test_multimap_prop_move)(test_set_no_prop_move)(test_multiset_no_prop_move)(test_map_no_prop_move)(test_multimap_no_prop_move))(
538 (default_generator)(generate_collisions)(limited_range)))
539 UNORDERED_TEST(move_construct_tests2,
540 ((test_set)(test_multiset)(test_map)(test_multimap)(test_set_prop_move)(test_multiset_prop_move)(test_map_prop_move)(test_multimap_prop_move)(test_set_no_prop_move)(test_multiset_no_prop_move)(test_map_no_prop_move)(test_multimap_no_prop_move))(
541 (default_generator)(generate_collisions)(limited_range)))
542 UNORDERED_TEST(move_assign_tests2,
543 ((test_set)(test_multiset)(test_map)(test_multimap)(test_set_prop_move)(test_multiset_prop_move)(test_map_prop_move)(test_multimap_prop_move)(test_set_no_prop_move)(test_multiset_no_prop_move)(test_map_no_prop_move)(test_multimap_no_prop_move))(
544 (default_generator)(generate_collisions)(limited_range)))
545#endif
546} // namespace move_tests
547
548RUN_TESTS()
549

source code of boost/libs/unordered/test/unordered/move_tests.cpp