|
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" |
| 22 | + |
| 23 | +TEST_CONSTEXPR_CXX20 void test_move_assignment(unsigned N) { |
| 24 | + // |
| 25 | + // Testing for O(1) ownership move |
| 26 | + // |
| 27 | + { // Test with pocma = true_type, thus performing an ownership move. |
| 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)); |
| 31 | + l2 = std::move(l); |
| 32 | + assert(l2 == lo); |
| 33 | + LIBCPP_ASSERT(l.empty()); // After move, source vector is in a vliad but unspecified state. libc++ leaves it empty. |
| 34 | + assert(l2.get_allocator() == lo.get_allocator()); |
| 35 | + } |
| 36 | + { // Test with pocma = false_type but equal allocators, thus performing an ownership move. |
| 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)); |
| 40 | + l2 = std::move(l); |
| 41 | + assert(l2 == lo); |
| 42 | + LIBCPP_ASSERT(l.empty()); |
| 43 | + assert(l2.get_allocator() == lo.get_allocator()); |
| 44 | + } |
| 45 | + { // Test with pocma = false_type but equal allocators, thus performing an ownership move. |
| 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>{}); |
| 49 | + l2 = std::move(l); |
| 50 | + assert(l2 == lo); |
| 51 | + LIBCPP_ASSERT(l.empty()); |
| 52 | + assert(l2.get_allocator() == lo.get_allocator()); |
| 53 | + } |
| 54 | + |
| 55 | + // |
| 56 | + // Testing for O(n) element-wise move |
| 57 | + // |
| 58 | + { // Test with pocma = false_type and unequal allocators, thus performing an element-wise move. |
| 59 | + // Reallocation occurs during the element-wise move due to empty destination vector. |
| 60 | + std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5)); |
| 61 | + std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5)); |
| 62 | + std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(42)); |
| 63 | + l2 = std::move(l); |
| 64 | + assert(l2 == lo); |
| 65 | + LIBCPP_ASSERT(!l.empty()); |
| 66 | + assert(l2.get_allocator() == test_allocator<bool>(42)); |
| 67 | + } |
| 68 | + { // Test with pocma = false_type and unequal allocators, thus performing an element-wise move. |
| 69 | + // Reallocation occurs during the element-wise move due to insufficient destination space. |
| 70 | + std::vector<bool, test_allocator<bool> > l(N + 64, true, test_allocator<bool>(5)); |
| 71 | + std::vector<bool, test_allocator<bool> > lo(N + 64, true, test_allocator<bool>(5)); |
| 72 | + std::vector<bool, test_allocator<bool> > l2(10, false, test_allocator<bool>(42)); |
| 73 | + l2 = std::move(l); |
| 74 | + assert(l2 == lo); |
| 75 | + LIBCPP_ASSERT(!l.empty()); |
| 76 | + assert(l2.get_allocator() == test_allocator<bool>(42)); |
| 77 | + } |
| 78 | + { // Test with pocma = false_type and unequal allocators, thus performing an element-wise move. |
| 79 | + // No reallocation occurs since source data fits within destination size. |
| 80 | + std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5)); |
| 81 | + std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5)); |
| 82 | + std::vector<bool, test_allocator<bool> > l2(N * 2, false, test_allocator<bool>(42)); |
| 83 | + l2 = std::move(l); |
| 84 | + assert(l2 == lo); |
| 85 | + LIBCPP_ASSERT(!l.empty()); |
| 86 | + assert(l2.get_allocator() == test_allocator<bool>(42)); |
| 87 | + } |
| 88 | +} |
20 | 89 |
|
21 | | -TEST_CONSTEXPR_CXX20 bool tests() |
22 | | -{ |
23 | | - { |
24 | | - std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5)); |
25 | | - std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5)); |
26 | | - for (int i = 1; i <= 3; ++i) |
27 | | - { |
28 | | - l.push_back(i); |
29 | | - lo.push_back(i); |
30 | | - } |
31 | | - std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(5)); |
32 | | - l2 = std::move(l); |
33 | | - assert(l2 == lo); |
34 | | - LIBCPP_ASSERT(l.empty()); |
35 | | - assert(l2.get_allocator() == lo.get_allocator()); |
36 | | - } |
37 | | - { |
38 | | - std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5)); |
39 | | - std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5)); |
40 | | - for (int i = 1; i <= 3; ++i) |
41 | | - { |
42 | | - l.push_back(i); |
43 | | - lo.push_back(i); |
44 | | - } |
45 | | - std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(6)); |
46 | | - l2 = std::move(l); |
47 | | - assert(l2 == lo); |
48 | | - assert(!l.empty()); |
49 | | - assert(l2.get_allocator() == test_allocator<bool>(6)); |
50 | | - } |
51 | | - { |
52 | | - std::vector<bool, other_allocator<bool> > l(other_allocator<bool>(5)); |
53 | | - std::vector<bool, other_allocator<bool> > lo(other_allocator<bool>(5)); |
54 | | - for (int i = 1; i <= 3; ++i) |
55 | | - { |
56 | | - l.push_back(i); |
57 | | - lo.push_back(i); |
58 | | - } |
59 | | - std::vector<bool, other_allocator<bool> > l2(other_allocator<bool>(6)); |
60 | | - l2 = std::move(l); |
61 | | - assert(l2 == lo); |
62 | | - assert(l.empty()); |
63 | | - assert(l2.get_allocator() == lo.get_allocator()); |
64 | | - } |
65 | | - { |
66 | | - std::vector<bool, min_allocator<bool> > l(min_allocator<bool>{}); |
67 | | - std::vector<bool, min_allocator<bool> > lo(min_allocator<bool>{}); |
68 | | - for (int i = 1; i <= 3; ++i) |
69 | | - { |
70 | | - l.push_back(i); |
71 | | - lo.push_back(i); |
72 | | - } |
73 | | - std::vector<bool, min_allocator<bool> > l2(min_allocator<bool>{}); |
74 | | - l2 = std::move(l); |
75 | | - assert(l2 == lo); |
76 | | - assert(l.empty()); |
77 | | - assert(l2.get_allocator() == lo.get_allocator()); |
78 | | - } |
| 90 | +TEST_CONSTEXPR_CXX20 bool tests() { |
| 91 | + test_move_assignment(9); |
| 92 | + test_move_assignment(33); |
| 93 | + test_move_assignment(65); |
| 94 | + test_move_assignment(257); |
| 95 | + test_move_assignment(1000); |
79 | 96 |
|
80 | | - return true; |
| 97 | + return true; |
81 | 98 | } |
82 | 99 |
|
83 | | -int main(int, char**) |
84 | | -{ |
85 | | - tests(); |
86 | | -#if TEST_STD_VER > 17 |
87 | | - static_assert(tests()); |
| 100 | +int main(int, char**) { |
| 101 | + tests(); |
| 102 | +#if TEST_STD_VER >= 20 |
| 103 | + static_assert(tests()); |
88 | 104 | #endif |
89 | | - return 0; |
| 105 | + return 0; |
90 | 106 | } |
0 commit comments