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