|
9 | 9 | // UNSUPPORTED: c++03
|
10 | 10 |
|
11 | 11 | // <vector>
|
| 12 | +// vector<bool> |
12 | 13 |
|
13 | 14 | // vector& operator=(vector&& c);
|
14 | 15 |
|
15 |
| -#include <vector> |
16 | 16 | #include <cassert>
|
17 |
| -#include "test_macros.h" |
18 |
| -#include "test_allocator.h" |
| 17 | +#include <vector> |
| 18 | + |
19 | 19 | #include "min_allocator.h"
|
| 20 | +#include "test_allocator.h" |
| 21 | +#include "test_macros.h" |
20 | 22 |
|
21 |
| -TEST_CONSTEXPR_CXX20 bool tests() { |
22 |
| - { |
23 |
| - std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5)); |
24 |
| - std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5)); |
25 |
| - for (int i = 1; i <= 3; ++i) { |
26 |
| - l.push_back(i); |
27 |
| - lo.push_back(i); |
28 |
| - } |
29 |
| - std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(5)); |
| 23 | +TEST_CONSTEXPR_CXX20 void test_move_assignment(unsigned N) { |
| 24 | + // |
| 25 | + // Testing for container move where either POCMA = true_type or the allocators compare equal |
| 26 | + // |
| 27 | + { // Test with POCMA = true_type |
| 28 | + std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5)); |
| 29 | + std::vector<bool, other_allocator<bool> > lo(N, true, other_allocator<bool>(5)); |
| 30 | + std::vector<bool, other_allocator<bool> > l2(N + 10, false, other_allocator<bool>(42)); |
30 | 31 | l2 = std::move(l);
|
31 | 32 | assert(l2 == lo);
|
32 |
| - LIBCPP_ASSERT(l.empty()); |
| 33 | + LIBCPP_ASSERT(l.empty()); // After move, source vector is in a vliad but unspecified state. libc++ leaves it empty. |
33 | 34 | assert(l2.get_allocator() == lo.get_allocator());
|
34 | 35 | }
|
35 |
| - { |
36 |
| - std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5)); |
37 |
| - std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5)); |
38 |
| - for (int i = 1; i <= 3; ++i) { |
39 |
| - l.push_back(i); |
40 |
| - lo.push_back(i); |
41 |
| - } |
42 |
| - std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(6)); |
| 36 | + { // Test with POCMA = false_type and allocators compare equal |
| 37 | + std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5)); |
| 38 | + std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5)); |
| 39 | + std::vector<bool, test_allocator<bool> > l2(N + 10, false, test_allocator<bool>(5)); |
43 | 40 | l2 = std::move(l);
|
44 | 41 | assert(l2 == lo);
|
45 |
| - assert(!l.empty()); |
46 |
| - assert(l2.get_allocator() == test_allocator<bool>(6)); |
| 42 | + LIBCPP_ASSERT(l.empty()); |
| 43 | + assert(l2.get_allocator() == lo.get_allocator()); |
47 | 44 | }
|
48 |
| - { |
49 |
| - std::vector<bool, other_allocator<bool> > l(other_allocator<bool>(5)); |
50 |
| - std::vector<bool, other_allocator<bool> > lo(other_allocator<bool>(5)); |
51 |
| - for (int i = 1; i <= 3; ++i) { |
52 |
| - l.push_back(i); |
53 |
| - lo.push_back(i); |
54 |
| - } |
55 |
| - std::vector<bool, other_allocator<bool> > l2(other_allocator<bool>(6)); |
| 45 | + { // Test with POCMA = false_type and allocators compare equal |
| 46 | + std::vector<bool, min_allocator<bool> > l(N, true, min_allocator<bool>{}); |
| 47 | + std::vector<bool, min_allocator<bool> > lo(N, true, min_allocator<bool>{}); |
| 48 | + std::vector<bool, min_allocator<bool> > l2(N + 10, false, min_allocator<bool>{}); |
56 | 49 | l2 = std::move(l);
|
57 | 50 | assert(l2 == lo);
|
58 |
| - assert(l.empty()); |
| 51 | + LIBCPP_ASSERT(l.empty()); |
59 | 52 | assert(l2.get_allocator() == lo.get_allocator());
|
60 | 53 | }
|
61 |
| - { |
62 |
| - std::vector<bool, min_allocator<bool> > l(min_allocator<bool>{}); |
63 |
| - std::vector<bool, min_allocator<bool> > lo(min_allocator<bool>{}); |
64 |
| - for (int i = 1; i <= 3; ++i) { |
65 |
| - l.push_back(i); |
66 |
| - lo.push_back(i); |
67 |
| - } |
68 |
| - std::vector<bool, min_allocator<bool> > l2(min_allocator<bool>{}); |
| 54 | + |
| 55 | + // |
| 56 | + // Testing for element-wise move where POCMA = false_type and allocators compare unequal |
| 57 | + // |
| 58 | + { // Test with reallocation during the element-wise move due to empty destination vector. |
| 59 | + std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5)); |
| 60 | + std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5)); |
| 61 | + std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(42)); |
69 | 62 | l2 = std::move(l);
|
70 | 63 | assert(l2 == lo);
|
71 |
| - assert(l.empty()); |
72 |
| - assert(l2.get_allocator() == lo.get_allocator()); |
| 64 | + LIBCPP_ASSERT(!l.empty()); |
| 65 | + assert(l2.get_allocator() == test_allocator<bool>(42)); |
| 66 | + } |
| 67 | + { // Test with reallocation occurs during the element-wise move due to insufficient destination space. |
| 68 | + std::vector<bool, test_allocator<bool> > l(N + 64, true, test_allocator<bool>(5)); |
| 69 | + std::vector<bool, test_allocator<bool> > lo(N + 64, true, test_allocator<bool>(5)); |
| 70 | + std::vector<bool, test_allocator<bool> > l2(10, false, test_allocator<bool>(42)); |
| 71 | + l2 = std::move(l); |
| 72 | + assert(l2 == lo); |
| 73 | + LIBCPP_ASSERT(!l.empty()); |
| 74 | + assert(l2.get_allocator() == test_allocator<bool>(42)); |
73 | 75 | }
|
| 76 | + { // Test without reallocation where source vector elements fit within destination size. |
| 77 | + std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5)); |
| 78 | + std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5)); |
| 79 | + std::vector<bool, test_allocator<bool> > l2(N * 2, false, test_allocator<bool>(42)); |
| 80 | + l2 = std::move(l); |
| 81 | + assert(l2 == lo); |
| 82 | + LIBCPP_ASSERT(!l.empty()); |
| 83 | + assert(l2.get_allocator() == test_allocator<bool>(42)); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +TEST_CONSTEXPR_CXX20 bool tests() { |
| 88 | + test_move_assignment(3); |
| 89 | + test_move_assignment(18); |
| 90 | + test_move_assignment(33); |
| 91 | + test_move_assignment(65); |
| 92 | + test_move_assignment(299); |
74 | 93 |
|
75 | 94 | return true;
|
76 | 95 | }
|
|
0 commit comments