]>
git.ipfire.org Git - thirdparty/gcc.git/blob - 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 }
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>"
12 #include <testsuite_hooks.h>
15 using T
= std::char_traits
<C
>;
18 struct Alloc
: std::allocator
<T
>
20 using std::allocator
<T
>::allocator
;
22 constexpr explicit Alloc(int p
) : personality(p
) { }
25 constexpr Alloc(const Alloc
<U
>& a
) : personality(a
.personality
) { }
29 constexpr Alloc
select_on_container_copy_construction() const
32 constexpr bool operator==(const Alloc
& a
) const noexcept
33 { return personality
== a
.personality
; }
39 std::basic_string
<C
> s0
;
42 std::basic_string
<C
> s1(std::allocator
<C
>{});
45 std::basic_string
<C
, T
, Alloc
<C
>> s2
;
48 std::basic_string
<C
, T
, Alloc
<C
>> s3(Alloc
<C
>(3));
50 VERIFY( s3
.get_allocator().personality
== 3 );
55 static_assert( test_default_ctor() );
60 const C cs
[] = "This has an embedded \0 null";
61 const auto len
= (sizeof(cs
) - 1)/sizeof(C
);
63 std::basic_string
<C
> s1(cs
);
64 VERIFY( s1
.length() == 21 );
65 std::basic_string
<C
> s2(cs
, std::allocator
<C
>{});
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
>{});
74 std::basic_string
<C
, T
, Alloc
<C
>> s5(cs
);
75 VERIFY( s5
== std::basic_string_view
<C
>(s1
) );
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 );
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 );
88 static_assert( test_cstr() );
93 const std::basic_string
<C
> short_string
= "sh";
94 const std::basic_string
<C
> long_string
= "string longer than the SSO buffer";
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
);
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
);
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();
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 );
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 );
128 static_assert( test_copy() );
133 const std::basic_string
<C
> short_string
= "sh";
134 const std::basic_string
<C
> long_string
= "string longer than the SSO buffer";
136 std::basic_string
<C
> s0
= short_string
;
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
);
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
);
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();
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 );
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 );
173 static_assert( test_move() );