|
12 | 12 |
|
13 | 13 | // vector& operator=(vector&& c); |
14 | 14 |
|
15 | | -#include <vector> |
16 | 15 | #include <cassert> |
17 | | -#include "test_macros.h" |
18 | | -#include "test_allocator.h" |
| 16 | +#include <vector> |
| 17 | + |
19 | 18 | #include "min_allocator.h" |
| 19 | +#include "test_allocator.h" |
| 20 | +#include "test_macros.h" |
20 | 21 |
|
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()); |
| 22 | +TEST_CONSTEXPR_CXX20 bool tests() { |
| 23 | + // |
| 24 | + // Testing for O(1) ownership move |
| 25 | + // |
| 26 | + { // Test with pocma = true_type, thus performing an ownership move. |
| 27 | + std::vector<bool, other_allocator<bool> > l(other_allocator<bool>(5)); |
| 28 | + std::vector<bool, other_allocator<bool> > lo(other_allocator<bool>(5)); |
| 29 | + std::vector<bool, other_allocator<bool> > l2(other_allocator<bool>(6)); |
| 30 | + for (int i = 1; i <= 3; ++i) { |
| 31 | + l.push_back(i); |
| 32 | + lo.push_back(i); |
| 33 | + } |
| 34 | + l2 = std::move(l); |
| 35 | + assert(l2 == lo); |
| 36 | + assert(l.empty()); |
| 37 | + assert(l2.get_allocator() == lo.get_allocator()); |
| 38 | + } |
| 39 | + { // Test with pocma = false_type and equal allocators, thus performing an ownership move. |
| 40 | + std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5)); |
| 41 | + std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5)); |
| 42 | + std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(5)); |
| 43 | + for (int i = 1; i <= 3; ++i) { |
| 44 | + l.push_back(i); |
| 45 | + lo.push_back(i); |
36 | 46 | } |
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)); |
| 47 | + l2 = std::move(l); |
| 48 | + assert(l2 == lo); |
| 49 | + LIBCPP_ASSERT(l.empty()); |
| 50 | + assert(l2.get_allocator() == lo.get_allocator()); |
| 51 | + } |
| 52 | + { // Test with pocma = false_type and equal allocators, thus performing an ownership move. |
| 53 | + std::vector<bool, min_allocator<bool> > l(min_allocator<bool>{}); |
| 54 | + std::vector<bool, min_allocator<bool> > lo(min_allocator<bool>{}); |
| 55 | + std::vector<bool, min_allocator<bool> > l2(min_allocator<bool>{}); |
| 56 | + for (int i = 1; i <= 3; ++i) { |
| 57 | + l.push_back(i); |
| 58 | + lo.push_back(i); |
50 | 59 | } |
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()); |
| 60 | + l2 = std::move(l); |
| 61 | + assert(l2 == lo); |
| 62 | + assert(l.empty()); |
| 63 | + assert(l2.get_allocator() == lo.get_allocator()); |
| 64 | + } |
| 65 | + |
| 66 | + // |
| 67 | + // Testing for O(n) element-wise move |
| 68 | + // |
| 69 | + { // Test with pocma = false_type and unequal allocators, thus performing an element-wise move. |
| 70 | + // Reallocation occurs during the element-wise move due to insufficient space. |
| 71 | + std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5)); |
| 72 | + std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5)); |
| 73 | + std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(6)); |
| 74 | + for (int i = 1; i <= 3; ++i) { |
| 75 | + l.push_back(i); |
| 76 | + lo.push_back(i); |
| 77 | + } |
| 78 | + l2 = std::move(l); |
| 79 | + assert(l2 == lo); |
| 80 | + assert(!l.empty()); |
| 81 | + assert(l2.get_allocator() == test_allocator<bool>(6)); |
| 82 | + } |
| 83 | + |
| 84 | + { // Test with pocma = false_type and unequal allocators, thus performing an element-wise move. |
| 85 | + // No reallocation occurs since source data fits within current size. |
| 86 | + std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5)); |
| 87 | + std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5)); |
| 88 | + std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(6)); |
| 89 | + for (int i = 1; i <= 3; ++i) { |
| 90 | + l.push_back(i); |
| 91 | + lo.push_back(i); |
64 | 92 | } |
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()); |
| 93 | + for (int i = 1; i <= 5; ++i) |
| 94 | + l2.push_back(i); |
| 95 | + l2 = std::move(l); |
| 96 | + assert(l2 == lo); |
| 97 | + assert(!l.empty()); |
| 98 | + assert(l2.get_allocator() == test_allocator<bool>(6)); |
| 99 | + } |
| 100 | + { // Test with pocma = false_type and unequal allocators, thus performing an element-wise move. |
| 101 | + // No reallocation occurs since source data fits within current spare space. |
| 102 | + std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5)); |
| 103 | + std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5)); |
| 104 | + std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(6)); |
| 105 | + for (int i = 1; i <= 3; ++i) { |
| 106 | + l.push_back(i); |
| 107 | + lo.push_back(i); |
78 | 108 | } |
| 109 | + for (int i = 1; i <= 2; ++i) |
| 110 | + l2.push_back(i); |
| 111 | + l2.reserve(5); |
| 112 | + l2 = std::move(l); |
| 113 | + assert(l2 == lo); |
| 114 | + assert(!l.empty()); |
| 115 | + assert(l2.get_allocator() == test_allocator<bool>(6)); |
| 116 | + } |
79 | 117 |
|
80 | | - return true; |
| 118 | + return true; |
81 | 119 | } |
82 | 120 |
|
83 | | -int main(int, char**) |
84 | | -{ |
85 | | - tests(); |
| 121 | +int main(int, char**) { |
| 122 | + tests(); |
86 | 123 | #if TEST_STD_VER > 17 |
87 | | - static_assert(tests()); |
| 124 | + static_assert(tests()); |
88 | 125 | #endif |
89 | | - return 0; |
| 126 | + return 0; |
90 | 127 | } |
0 commit comments