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