| 1 |
// { dg-do compile { target c++20 } } |
| 2 |
|
| 3 |
#include <vector> |
| 4 |
#include <testsuite_hooks.h> |
| 5 |
|
| 6 |
constexpr std::size_t |
| 7 |
capacity_for(std::size_t n) |
| 8 |
{ |
| 9 |
std::size_t N = std::vector<bool>(1).capacity(); |
| 10 |
if (auto r = n % N) |
| 11 |
return n - r + N; |
| 12 |
return n; |
| 13 |
} |
| 14 |
|
| 15 |
constexpr bool |
| 16 |
test_empty() |
| 17 |
{ |
| 18 |
std::vector<bool> v; |
| 19 |
VERIFY( v.empty() ); |
| 20 |
v = {1}; |
| 21 |
VERIFY( !v.empty() ); |
| 22 |
|
| 23 |
return true; |
| 24 |
} |
| 25 |
|
| 26 |
static_assert( test_empty() ); |
| 27 |
|
| 28 |
constexpr bool |
| 29 |
test_size() |
| 30 |
{ |
| 31 |
std::vector<bool> v; |
| 32 |
VERIFY( v.size() == 0 ); |
| 33 |
v = {1}; |
| 34 |
VERIFY( v.size() == 1 ); |
| 35 |
|
| 36 |
VERIFY( v.max_size() != 0 ); |
| 37 |
|
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
static_assert( test_size() ); |
| 42 |
|
| 43 |
constexpr bool |
| 44 |
test_capacity() |
| 45 |
{ |
| 46 |
std::vector<bool> v; |
| 47 |
VERIFY( v.size() == 0 ); |
| 48 |
VERIFY( v.capacity() == v.size() ); |
| 49 |
v = {false, false, false}; |
| 50 |
VERIFY( v.size() == 3 ); |
| 51 |
VERIFY( v.capacity() >= v.size() ); |
| 52 |
|
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
static_assert( test_capacity() ); |
| 57 |
|
| 58 |
constexpr bool |
| 59 |
test_resize() |
| 60 |
{ |
| 61 |
std::vector<bool> v; |
| 62 |
v.reserve(9); |
| 63 |
VERIFY( v.size() == 0 ); |
| 64 |
VERIFY( v.capacity() == capacity_for(9) ); |
| 65 |
v.resize(5); |
| 66 |
VERIFY( v.size() == 5 ); |
| 67 |
VERIFY( v.capacity() == capacity_for(9) ); |
| 68 |
v.resize(900, true); |
| 69 |
VERIFY( v.size() == 900 ); |
| 70 |
VERIFY( v.capacity() == capacity_for(900) ); |
| 71 |
VERIFY( v[10] == true ); |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
static_assert( test_resize() ); |
| 77 |
|
| 78 |
constexpr bool |
| 79 |
test_reserve() |
| 80 |
{ |
| 81 |
std::vector<bool> v; |
| 82 |
v.reserve(9); |
| 83 |
VERIFY( v.size() == 0 ); |
| 84 |
VERIFY( v.capacity() == capacity_for(9) ); |
| 85 |
v.resize(2); |
| 86 |
VERIFY( v.size() == 2 ); |
| 87 |
VERIFY( v.capacity() == capacity_for(9) ); |
| 88 |
v.resize(300); |
| 89 |
v.resize(100); |
| 90 |
VERIFY( v.size() == 100 ); |
| 91 |
VERIFY( v.capacity() == capacity_for(300) ); |
| 92 |
|
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
static_assert( test_reserve() ); |
| 97 |
|
| 98 |
constexpr bool |
| 99 |
test_shrink_to_fit() |
| 100 |
{ |
| 101 |
std::vector<bool> v; |
| 102 |
v.reserve(9); |
| 103 |
v.shrink_to_fit(); |
| 104 |
VERIFY( v.capacity() == 0 ); |
| 105 |
v.reserve(9); |
| 106 |
v.resize(5); |
| 107 |
v.shrink_to_fit(); |
| 108 |
VERIFY( v.capacity() == capacity_for(v.size()) ); |
| 109 |
|
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
static_assert( test_shrink_to_fit() ); |