| 1 | // { dg-do compile { target c++20 } } |
| 2 | |
| 3 | #include <vector> |
| 4 | #include <testsuite_hooks.h> |
| 5 | #include <testsuite_iterators.h> |
| 6 | |
| 7 | template<typename T> |
| 8 | struct Alloc : std::allocator<T> |
| 9 | { |
| 10 | using std::allocator<T>::allocator; |
| 11 | |
| 12 | constexpr explicit Alloc(int p) : personality(p) { } |
| 13 | |
| 14 | template<typename U> |
| 15 | constexpr Alloc(const Alloc<U>& a) : personality(a.personality) { } |
| 16 | |
| 17 | #if __cplusplus <= 202302L |
| 18 | using is_always_equal = std::false_type; |
| 19 | #endif |
| 20 | |
| 21 | int personality = 0; |
| 22 | |
| 23 | constexpr Alloc select_on_container_copy_construction() const |
| 24 | { return Alloc(-1); } |
| 25 | |
| 26 | constexpr bool operator==(const Alloc& a) const noexcept |
| 27 | { return personality == a.personality; } |
| 28 | }; |
| 29 | |
| 30 | namespace default_constructor_global_scope |
| 31 | { |
| 32 | constexpr std::vector<bool> v1; |
| 33 | static_assert(v1.size() == 0); |
| 34 | static_assert(v1.capacity() == 0); |
| 35 | |
| 36 | constexpr std::allocator<bool> a; |
| 37 | constexpr std::vector<bool> v2(a); |
| 38 | static_assert(v2.size() == 0); |
| 39 | static_assert(v2.capacity() == 0); |
| 40 | |
| 41 | constexpr Alloc<bool> aa(10); |
| 42 | constexpr std::vector<bool, Alloc<bool>> v3(aa); |
| 43 | static_assert(v3.size() == 0); |
| 44 | static_assert(v3.capacity() == 0); |
| 45 | static_assert(v3.get_allocator() == aa); |
| 46 | } |
| 47 | |
| 48 | constexpr bool |
| 49 | default_constructor_function_scope() |
| 50 | { |
| 51 | // vector() |
| 52 | |
| 53 | std::vector<bool> v1; |
| 54 | VERIFY(v1.size() == 0); |
| 55 | VERIFY(v1.capacity() == 0); |
| 56 | |
| 57 | // vector(const Allocator&) |
| 58 | |
| 59 | const std::allocator<bool> a; |
| 60 | std::vector<bool> v2(a); |
| 61 | VERIFY(v2.size() == 0); |
| 62 | VERIFY(v2.capacity() == 0); |
| 63 | |
| 64 | const Alloc<bool> aa(10); |
| 65 | std::vector<bool, Alloc<bool>> v3(aa); |
| 66 | VERIFY(v3.size() == 0); |
| 67 | VERIFY(v3.capacity() == 0); |
| 68 | VERIFY(v3.get_allocator() == aa); |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | static_assert( default_constructor_function_scope() ); |
| 74 | |
| 75 | constexpr bool |
| 76 | sequence_constructors() |
| 77 | { |
| 78 | // vector(size_type, const Allocator& = Allocator()) |
| 79 | |
| 80 | std::vector<bool> v0(0); |
| 81 | VERIFY(v0.size() == 0); |
| 82 | |
| 83 | std::vector<bool> v1(1); |
| 84 | VERIFY(v1.size() == 1); |
| 85 | |
| 86 | std::vector<bool> v2(2); |
| 87 | VERIFY(v2.size() == 2); |
| 88 | |
| 89 | std::vector<bool> v50(50); |
| 90 | VERIFY(v50.size() == 50); |
| 91 | |
| 92 | const std::allocator<bool> a; |
| 93 | std::vector<bool> a0(0, a); |
| 94 | VERIFY(a0.size() == 0); |
| 95 | |
| 96 | std::vector<bool> a1(1, a); |
| 97 | VERIFY(a1.size() == 1); |
| 98 | |
| 99 | std::vector<bool> a2(2, a); |
| 100 | VERIFY(a2.size() == 2); |
| 101 | |
| 102 | std::vector<bool> a50(50, a); |
| 103 | VERIFY(a50.size() == 50); |
| 104 | |
| 105 | const Alloc<bool> la(10); |
| 106 | std::vector<bool, Alloc<bool>> l0(0, la); |
| 107 | VERIFY(l0.size() == 0); |
| 108 | VERIFY(l0.get_allocator() == la); |
| 109 | |
| 110 | std::vector<bool, Alloc<bool>> l1(1, la); |
| 111 | VERIFY(l1.size() == 1); |
| 112 | VERIFY(l1.get_allocator() == la); |
| 113 | |
| 114 | std::vector<bool, Alloc<bool>> l2(2, la); |
| 115 | VERIFY(l2.size() == 2); |
| 116 | VERIFY(l2.get_allocator() == la); |
| 117 | |
| 118 | std::vector<bool, Alloc<bool>> l50(50, la); |
| 119 | VERIFY(l50.size() == 50); |
| 120 | VERIFY(l50.get_allocator() == la); |
| 121 | |
| 122 | // vector(size_type, const T&, const Allocator& = Allocator()) |
| 123 | |
| 124 | std::vector<bool> v3(3, true); |
| 125 | VERIFY(v3.size() == 3); |
| 126 | VERIFY(v3[0] == true && v3[2] == true); |
| 127 | |
| 128 | std::vector<bool> a3(3, false, a); |
| 129 | VERIFY(a3.size() == 3); |
| 130 | VERIFY(a3[0] == false && a3[2] == false); |
| 131 | |
| 132 | std::vector<bool, Alloc<bool>> l3(3, true, la); |
| 133 | VERIFY(l3.size() == 3); |
| 134 | VERIFY(l3[0] == true && l3[2] == true); |
| 135 | VERIFY(l3.get_allocator() == la); |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | static_assert(sequence_constructors()); |
| 141 | |
| 142 | constexpr bool |
| 143 | iterator_range_constructor() |
| 144 | { |
| 145 | // vector(InputIterator, InputIterator, const Allocator& = Allocator()) |
| 146 | |
| 147 | short range[3] = { true, false, true }; |
| 148 | |
| 149 | std::vector<bool> v0(std::begin(range), std::end(range)); |
| 150 | VERIFY(v0.size() == std::size(range)); |
| 151 | VERIFY(v0[0] == true && v0[1] == false && v0[2] == true); |
| 152 | |
| 153 | const Alloc<bool> a(5); |
| 154 | std::vector<bool, Alloc<bool>> l0(std::begin(range), std::end(range), a); |
| 155 | VERIFY(l0.size() == std::size(range)); |
| 156 | VERIFY(l0.get_allocator() == a); |
| 157 | VERIFY(l0[0] == true && l0[1] == false && l0[2] == true); |
| 158 | |
| 159 | struct input_iterator |
| 160 | { |
| 161 | using iterator_category = std::input_iterator_tag; |
| 162 | using value_type = bool; |
| 163 | using pointer = const bool*; |
| 164 | using reference = bool; |
| 165 | using difference_type = int; |
| 166 | |
| 167 | constexpr input_iterator() : val(0) { } |
| 168 | constexpr input_iterator(int i) : val(i) { } |
| 169 | |
| 170 | constexpr input_iterator& operator++() { --val; return *this; } |
| 171 | constexpr input_iterator operator++(int) { return {val--}; } |
| 172 | |
| 173 | constexpr bool operator*() const { return val % 2; } |
| 174 | constexpr const bool* operator->() const { return nullptr; } |
| 175 | |
| 176 | constexpr bool operator==(const input_iterator&) const = default; |
| 177 | |
| 178 | int val; |
| 179 | }; |
| 180 | |
| 181 | std::vector<bool> v1(input_iterator(3), input_iterator()); |
| 182 | VERIFY(v1.size() == 3); |
| 183 | VERIFY(v1[0] == true && v1[1] == false && v1[2] == true); |
| 184 | |
| 185 | std::vector<bool, Alloc<bool>> l1(input_iterator(2), input_iterator(), a); |
| 186 | VERIFY(l1.size() == 2); |
| 187 | VERIFY(l1.get_allocator() == a); |
| 188 | VERIFY(l1[0] == false && l1[1] == true); |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | static_assert(iterator_range_constructor()); |
| 194 | |
| 195 | constexpr bool |
| 196 | initializer_list_constructor() |
| 197 | { |
| 198 | // vector(initializer_list<T>, const Allocator& = Allocator()) |
| 199 | |
| 200 | std::vector<bool> v0({ false, true, false }); |
| 201 | VERIFY(v0.size() == 3); |
| 202 | VERIFY(v0[0] == false && v0[1] == true && v0[2] == false); |
| 203 | |
| 204 | const Alloc<bool> a(5); |
| 205 | std::vector<bool, Alloc<bool>> l0({ true, false, false }, a); |
| 206 | VERIFY(l0.size() == 3); |
| 207 | VERIFY(l0.get_allocator() == a); |
| 208 | VERIFY(l0[0] == true && l0[1] == false && l0[2] == false); |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | static_assert(initializer_list_constructor()); |
| 214 | |
| 215 | constexpr bool |
| 216 | copy_constructor() |
| 217 | { |
| 218 | const std::vector<bool> v0({ 1, 0, 0, 1, 0, 1, 1, 0 }); |
| 219 | const std::vector<bool, Alloc<bool>> l0({ 0, 0, 1, 1, 0, 1, 0, 1, 1, 1 }); |
| 220 | |
| 221 | // vector(const vector&) |
| 222 | |
| 223 | std::vector<bool> v1(v0); |
| 224 | VERIFY( v1.size() == v0.size() ); |
| 225 | VERIFY( v1[0] == v0[0] && v1[1] == v0[1] && v1[2] == v0[2] ); |
| 226 | VERIFY( v1.get_allocator() == v0.get_allocator() ); |
| 227 | |
| 228 | const Alloc<bool> as(6); |
| 229 | std::vector<bool, Alloc<bool>> s1(3, true, as); |
| 230 | std::vector<bool, Alloc<bool>> s2(s1); |
| 231 | VERIFY( s2.size() == s1.size() ); |
| 232 | VERIFY( s2.get_allocator().personality == -1 ); |
| 233 | |
| 234 | // vector(const vector&, const Allocator&) |
| 235 | |
| 236 | const Alloc<bool> a(6); |
| 237 | std::vector<bool, Alloc<bool>> l1(l0, a); |
| 238 | VERIFY( l1.size() == l0.size() ); |
| 239 | VERIFY( l1[0] == l0[0] && l1[1] == l0[1] && l1[2] == l0[2] ); |
| 240 | VERIFY( l1.get_allocator() == a ); |
| 241 | VERIFY( l1.get_allocator() != l0.get_allocator() ); |
| 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | static_assert(copy_constructor()); |
| 247 | |
| 248 | constexpr bool |
| 249 | move_constructor() |
| 250 | { |
| 251 | const std::vector<bool> v0({ 1, 0, 0, 1, 0, 1, 1, 0 }); |
| 252 | const std::vector<bool, Alloc<bool>> l0({ 0, 0, 1, 1, 0, 1, 0, 1, 1, 1 }); |
| 253 | |
| 254 | // vector(const vector&) |
| 255 | |
| 256 | std::vector<bool> v1(v0); |
| 257 | std::vector<bool> v2(std::move(v1)); |
| 258 | VERIFY( v2.size() == v0.size() ); |
| 259 | VERIFY( v1.empty() ); |
| 260 | VERIFY( v2[0] == v0[0] && v2[1] == v0[1] && v2[2] == v0[2] ); |
| 261 | VERIFY( v2.get_allocator() == v0.get_allocator() ); |
| 262 | |
| 263 | // vector(const vector&, const Allocator&) |
| 264 | |
| 265 | const Alloc<bool> a(6); |
| 266 | std::vector<bool, Alloc<bool>> l1(l0); |
| 267 | std::vector<bool, Alloc<bool>> l2(std::move(l1), a); |
| 268 | VERIFY( l2.size() == l0.size() ); |
| 269 | VERIFY( l2[0] == l0[0] && l2[1] == l0[1] && l2[2] == l0[2] ); |
| 270 | VERIFY( l2.get_allocator() == a ); |
| 271 | VERIFY( l2.get_allocator() != l0.get_allocator() ); |
| 272 | |
| 273 | std::vector<bool, Alloc<bool>> l3(std::move(l2), a); |
| 274 | VERIFY( l3.size() == l0.size() ); |
| 275 | VERIFY( l3[0] == l0[0] && l3[1] == l0[1] && l3[2] == l0[2] ); |
| 276 | VERIFY( l3.get_allocator() == a ); |
| 277 | VERIFY( l3.get_allocator() == l2.get_allocator() ); |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | static_assert(move_constructor()); |