| 1 |
// { dg-do compile { target c++20 } } |
| 2 |
// { dg-require-effective-target cxx11_abi } |
| 3 |
// { dg-add-options no_pch } |
| 4 |
|
| 5 |
#include <string> |
| 6 |
|
| 7 |
#ifndef __cpp_lib_constexpr_string |
| 8 |
# error "Feature-test macro for constexpr std::string missing in <string>" |
| 9 |
#elif __cpp_lib_constexpr_string != 201907L |
| 10 |
# error "Feature-test macro for constexpr std::string has wrong value in <string>" |
| 11 |
#endif |
| 12 |
|
| 13 |
#include <testsuite_hooks.h> |
| 14 |
|
| 15 |
using C = wchar_t; |
| 16 |
using T = std::char_traits<C>; |
| 17 |
|
| 18 |
template<typename T> |
| 19 |
struct Alloc : std::allocator<T> |
| 20 |
{ |
| 21 |
using std::allocator<T>::allocator; |
| 22 |
|
| 23 |
constexpr explicit Alloc(int p) : personality(p) { } |
| 24 |
|
| 25 |
template<typename U> |
| 26 |
constexpr Alloc(const Alloc<U>& a) : personality(a.personality) { } |
| 27 |
|
| 28 |
int personality = 0; |
| 29 |
|
| 30 |
constexpr Alloc select_on_container_copy_construction() const |
| 31 |
{ return Alloc(-1); } |
| 32 |
|
| 33 |
constexpr bool operator==(const Alloc& a) const noexcept |
| 34 |
{ return personality == a.personality; } |
| 35 |
}; |
| 36 |
|
| 37 |
constexpr bool |
| 38 |
test_default_ctor() |
| 39 |
{ |
| 40 |
std::basic_string<C> s0; |
| 41 |
VERIFY( s0.empty() ); |
| 42 |
|
| 43 |
std::basic_string<C> s1(std::allocator<C>{}); |
| 44 |
VERIFY( s1.empty() ); |
| 45 |
|
| 46 |
std::basic_string<C, T, Alloc<C>> s2; |
| 47 |
VERIFY( s2.empty() ); |
| 48 |
|
| 49 |
std::basic_string<C, T, Alloc<C>> s3(Alloc<C>(3)); |
| 50 |
VERIFY( s3.empty() ); |
| 51 |
VERIFY( s3.get_allocator().personality == 3 ); |
| 52 |
|
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
static_assert( test_default_ctor() ); |
| 57 |
|
| 58 |
constexpr bool |
| 59 |
test_cstr() |
| 60 |
{ |
| 61 |
const C cs[] = L"This has an embedded \0 null"; |
| 62 |
const auto len = (sizeof(cs) - 1)/sizeof(C); |
| 63 |
|
| 64 |
std::basic_string<C> s1(cs); |
| 65 |
VERIFY( s1.length() == 21 ); |
| 66 |
std::basic_string<C> s2(cs, std::allocator<C>{}); |
| 67 |
VERIFY( s2 == s1 ); |
| 68 |
|
| 69 |
std::basic_string<C> s3(cs, len); |
| 70 |
VERIFY( s3.length() == len ); |
| 71 |
VERIFY( s3[len] == L'\0' ); |
| 72 |
std::basic_string<C> s4(cs, len, std::allocator<C>{}); |
| 73 |
VERIFY( s4 == s3 ); |
| 74 |
|
| 75 |
std::basic_string<C, T, Alloc<C>> s5(cs); |
| 76 |
VERIFY( s5 == std::basic_string_view<C>(s1) ); |
| 77 |
|
| 78 |
std::basic_string<C, T, Alloc<C>> s6(cs, Alloc<C>(6)); |
| 79 |
VERIFY( s6 == std::basic_string_view<C>(s1) ); |
| 80 |
VERIFY( s6.get_allocator().personality == 6 ); |
| 81 |
|
| 82 |
std::basic_string<C, T, Alloc<C>> s7(cs, len, Alloc<C>(7)); |
| 83 |
VERIFY( s7 == std::basic_string_view<C>(s3) ); |
| 84 |
VERIFY( s7.get_allocator().personality == 7 ); |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
static_assert( test_cstr() ); |
| 90 |
|
| 91 |
constexpr bool |
| 92 |
test_copy() |
| 93 |
{ |
| 94 |
const std::basic_string<C> short_string = L"sh"; |
| 95 |
const std::basic_string<C> long_string = L"string longer than the SSO buffer"; |
| 96 |
|
| 97 |
std::basic_string<C> s1 = short_string; |
| 98 |
VERIFY( s1 == short_string ); |
| 99 |
std::basic_string<C> s2(short_string, s1.get_allocator()); |
| 100 |
VERIFY( s2 == short_string ); |
| 101 |
|
| 102 |
std::basic_string<C> s3 = long_string; |
| 103 |
VERIFY( s3 == long_string ); |
| 104 |
std::basic_string<C> s4(long_string, s1.get_allocator()); |
| 105 |
VERIFY( s4 == long_string ); |
| 106 |
|
| 107 |
std::basic_string<C, T, Alloc<C>> a_short_string = short_string.c_str(); |
| 108 |
std::basic_string<C, T, Alloc<C>> a_long_string = long_string.c_str(); |
| 109 |
|
| 110 |
std::basic_string<C, T, Alloc<C>> s5(a_short_string); |
| 111 |
VERIFY( s5 == a_short_string ); |
| 112 |
std::basic_string<C, T, Alloc<C>> s6(a_short_string, s5.get_allocator()); |
| 113 |
VERIFY( s6 == a_short_string ); |
| 114 |
std::basic_string<C, T, Alloc<C>> s7(a_short_string, Alloc<C>(7)); |
| 115 |
VERIFY( s7 == a_short_string ); |
| 116 |
VERIFY( s7.get_allocator().personality == 7 ); |
| 117 |
|
| 118 |
std::basic_string<C, T, Alloc<C>> s8 = a_long_string; |
| 119 |
VERIFY( s8 == a_long_string ); |
| 120 |
std::basic_string<C, T, Alloc<C>> s9(a_long_string, s5.get_allocator()); |
| 121 |
VERIFY( s9 == a_long_string ); |
| 122 |
std::basic_string<C, T, Alloc<C>> s10(a_long_string, Alloc<C>(10)); |
| 123 |
VERIFY( s10 == a_long_string ); |
| 124 |
VERIFY( s10.get_allocator().personality == 10 ); |
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
static_assert( test_copy() ); |
| 130 |
|
| 131 |
constexpr bool |
| 132 |
test_move() |
| 133 |
{ |
| 134 |
const std::basic_string<C> short_string = L"sh"; |
| 135 |
const std::basic_string<C> long_string = L"string longer than the SSO buffer"; |
| 136 |
|
| 137 |
std::basic_string<C> s0 = short_string; |
| 138 |
|
| 139 |
std::basic_string<C> s1 = std::move(s0); |
| 140 |
VERIFY( s1 == short_string ); |
| 141 |
std::basic_string<C> s2(std::move(s1), std::allocator<C>()); |
| 142 |
VERIFY( s2 == short_string ); |
| 143 |
|
| 144 |
s0 = long_string; |
| 145 |
std::basic_string<C> s3 = std::move(s0); |
| 146 |
VERIFY( s3 == long_string ); |
| 147 |
std::basic_string<C> s4(std::move(s3), s1.get_allocator()); |
| 148 |
VERIFY( s4 == long_string ); |
| 149 |
|
| 150 |
std::basic_string<C, T, Alloc<C>> a_short_string = short_string.c_str(); |
| 151 |
std::basic_string<C, T, Alloc<C>> a_long_string = long_string.c_str(); |
| 152 |
|
| 153 |
auto sa0 = a_short_string; |
| 154 |
std::basic_string<C, T, Alloc<C>> s5 = std::move(sa0); |
| 155 |
VERIFY( s5 == a_short_string ); |
| 156 |
std::basic_string<C, T, Alloc<C>> s6(std::move(s5), sa0.get_allocator()); |
| 157 |
VERIFY( s6 == a_short_string ); |
| 158 |
std::basic_string<C, T, Alloc<C>> s7(std::move(s6), Alloc<C>(7)); |
| 159 |
VERIFY( s7 == a_short_string ); |
| 160 |
VERIFY( s7.get_allocator().personality == 7 ); |
| 161 |
|
| 162 |
sa0 = a_long_string; |
| 163 |
std::basic_string<C, T, Alloc<C>> s8 = std::move(sa0); |
| 164 |
VERIFY( s8 == a_long_string ); |
| 165 |
std::basic_string<C, T, Alloc<C>> s9(std::move(s8), s5.get_allocator()); |
| 166 |
VERIFY( s9 == a_long_string ); |
| 167 |
std::basic_string<C, T, Alloc<C>> s10(std::move(s9), Alloc<C>(10)); |
| 168 |
VERIFY( s10 == a_long_string ); |
| 169 |
VERIFY( s10.get_allocator().personality == 10 ); |
| 170 |
|
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
static_assert( test_move() ); |