1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
13 // Test that the constructors offered by std::basic_string are formulated
14 // so they're compatible with implicit deduction guides.
17 #include <string_view>
20 #include "test_macros.h"
21 #include "test_allocator.h"
22 #include "test_iterators.h"
23 #include "constexpr_char_traits.h"
25 template <class T
, class Alloc
= std::allocator
<T
>>
26 using BStr
= std::basic_string
<T
, std::char_traits
<T
>, Alloc
>;
29 // using A = Allocator;
30 // using BS = basic_string
31 // using BSV = basic_string_view
33 // (1) basic_string() - NOT TESTED
34 // (2) basic_string(A const&) - BROKEN
35 // (3) basic_string(size_type, CharT, const A& = A())
36 // (4) basic_string(BS const&, size_type, A const& = A())
37 // (5) basic_string(BS const&, size_type, size_type, A const& = A())
38 // (6) basic_string(const CharT*, size_type, A const& = A())
39 // (7) basic_string(const CharT*, A const& = A())
40 // (8) basic_string(InputIt, InputIt, A const& = A()) - BROKEN
41 // (9) basic_string(BS const&)
42 // (10) basic_string(BS const&, A const&)
43 // (11) basic_string(BS&&)
44 // (12) basic_string(BS&&, A const&)
45 // (13) basic_string(initializer_list<CharT>, A const& = A())
46 // (14) basic_string(BSV, A const& = A())
47 // (15) basic_string(const T&, size_type, size_type, A const& = A())
48 TEST_CONSTEXPR_CXX20
bool test() {
49 using TestSizeT
= test_allocator
<char>::size_type
;
52 // Nothing to do. Cannot deduce without any arguments.
56 // This overload isn't compatible with implicit deduction guides as
57 // specified in the standard.
58 // const test_allocator<char> alloc{};
59 // std::basic_string s(alloc);
61 { // Testing (3) w/o allocator
62 std::basic_string
s(6ull, 'a');
63 ASSERT_SAME_TYPE(decltype(s
), std::string
);
64 assert(s
== "aaaaaa");
66 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
67 std::basic_string
w(2ull, L
'b');
68 ASSERT_SAME_TYPE(decltype(w
), std::wstring
);
72 { // Testing (3) w/ allocator
73 std::basic_string
s(6ull, 'a', test_allocator
<char>{});
74 ASSERT_SAME_TYPE(decltype(s
), BStr
<char, test_allocator
<char>>);
75 assert(s
== "aaaaaa");
77 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
78 std::basic_string
w(2ull, L
'b', test_allocator
<wchar_t>{});
79 ASSERT_SAME_TYPE(decltype(w
), BStr
<wchar_t, test_allocator
<wchar_t>>);
83 { // Testing (4) w/o allocator
84 const std::string
sin("abc");
85 std::basic_string
s(sin
, (std::size_t)1);
86 ASSERT_SAME_TYPE(decltype(s
), std::string
);
89 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
90 using WStr
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
91 const WStr
win(L
"abcdef");
92 std::basic_string
w(win
, (TestSizeT
)3);
93 ASSERT_SAME_TYPE(decltype(w
), WStr
);
97 { // Testing (4) w/ allocator
98 const std::string
sin("abc");
99 std::basic_string
s(sin
, (std::size_t)1, std::allocator
<char>{});
100 ASSERT_SAME_TYPE(decltype(s
), std::string
);
103 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
104 using WStr
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
105 const WStr
win(L
"abcdef");
106 std::basic_string
w(win
, (TestSizeT
)3, test_allocator
<wchar_t>{});
107 ASSERT_SAME_TYPE(decltype(w
), WStr
);
111 { // Testing (5) w/o allocator
112 const std::string
sin("abc");
113 std::basic_string
s(sin
, (std::size_t)1, (size_t)3);
114 ASSERT_SAME_TYPE(decltype(s
), std::string
);
117 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
118 using WStr
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
119 const WStr
win(L
"abcdef");
120 std::basic_string
w(win
, (TestSizeT
)2, (TestSizeT
)3);
121 ASSERT_SAME_TYPE(decltype(w
), WStr
);
125 { // Testing (5) w/ allocator
126 const std::string
sin("abc");
127 std::basic_string
s(sin
, (std::size_t)1, (size_t)3, std::allocator
<char>{});
128 ASSERT_SAME_TYPE(decltype(s
), std::string
);
131 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
132 using WStr
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
133 const WStr
win(L
"abcdef");
134 std::basic_string
w(win
, (TestSizeT
)2, (TestSizeT
)3, test_allocator
<wchar_t>{});
135 ASSERT_SAME_TYPE(decltype(w
), WStr
);
139 { // Testing (6) w/o allocator
140 std::basic_string
s("abc", (std::size_t)2);
141 ASSERT_SAME_TYPE(decltype(s
), std::string
);
144 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
145 std::basic_string
w(L
"abcdef", (std::size_t)3);
146 ASSERT_SAME_TYPE(decltype(w
), std::wstring
);
150 { // Testing (6) w/ allocator
151 std::basic_string
s("abc", (std::size_t)2, std::allocator
<char>{});
152 ASSERT_SAME_TYPE(decltype(s
), std::string
);
155 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
156 using WStr
= std::basic_string
<wchar_t, std::char_traits
<wchar_t>, test_allocator
<wchar_t>>;
157 std::basic_string
w(L
"abcdef", (TestSizeT
)3, test_allocator
<wchar_t>{});
158 ASSERT_SAME_TYPE(decltype(w
), WStr
);
162 { // Testing (7) w/o allocator
163 std::basic_string
s("abc");
164 ASSERT_SAME_TYPE(decltype(s
), std::string
);
167 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
168 std::basic_string
w(L
"abcdef");
169 ASSERT_SAME_TYPE(decltype(w
), std::wstring
);
170 assert(w
== L
"abcdef");
173 { // Testing (7) w/ allocator
174 std::basic_string
s("abc", std::allocator
<char>{});
175 ASSERT_SAME_TYPE(decltype(s
), std::string
);
178 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
179 using WStr
= std::basic_string
<wchar_t, std::char_traits
<wchar_t>, test_allocator
<wchar_t>>;
180 std::basic_string
w(L
"abcdef", test_allocator
<wchar_t>{});
181 ASSERT_SAME_TYPE(decltype(w
), WStr
);
182 assert(w
== L
"abcdef");
185 { // (8) w/o allocator
186 using It
= cpp17_input_iterator
<const char*>;
187 const char* input
= "abcdef";
188 std::basic_string
s(It(input
), It(input
+ 3), std::allocator
<char>{});
189 ASSERT_SAME_TYPE(decltype(s
), std::string
);
192 { // (8) w/ allocator
194 using Expect
= std::basic_string
<char, std::char_traits
<char>, test_allocator
<char>>;
195 using It
= cpp17_input_iterator
<const char*>;
196 const char* input
= "abcdef";
197 std::basic_string
s(It(input
), It(input
+ 3), test_allocator
<char>{});
198 ASSERT_SAME_TYPE(decltype(s
), Expect
);
201 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
203 using ExpectW
= std::basic_string
<wchar_t, std::char_traits
<wchar_t>, test_allocator
<wchar_t>>;
204 using It
= cpp17_input_iterator
<const wchar_t*>;
205 const wchar_t* input
= L
"abcdef";
206 std::basic_string
s(It(input
), It(input
+ 3), test_allocator
<wchar_t>{});
207 ASSERT_SAME_TYPE(decltype(s
), ExpectW
);
213 const std::string
sin("abc");
214 std::basic_string
s(sin
);
215 ASSERT_SAME_TYPE(decltype(s
), std::string
);
218 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
219 using WStr
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
220 const WStr
win(L
"abcdef");
221 std::basic_string
w(win
);
222 ASSERT_SAME_TYPE(decltype(w
), WStr
);
223 assert(w
== L
"abcdef");
227 const std::string
sin("abc");
228 std::basic_string
s(sin
, std::allocator
<char>{});
229 ASSERT_SAME_TYPE(decltype(s
), std::string
);
232 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
233 using WStr
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
234 const WStr
win(L
"abcdef");
235 std::basic_string
w(win
, test_allocator
<wchar_t>{});
236 ASSERT_SAME_TYPE(decltype(w
), WStr
);
237 assert(w
== L
"abcdef");
241 std::string
sin("abc");
242 std::basic_string
s(std::move(sin
));
243 ASSERT_SAME_TYPE(decltype(s
), std::string
);
246 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
247 using WStr
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
249 std::basic_string
w(std::move(win
));
250 ASSERT_SAME_TYPE(decltype(w
), WStr
);
251 assert(w
== L
"abcdef");
255 std::string
sin("abc");
256 std::basic_string
s(std::move(sin
), std::allocator
<char>{});
257 ASSERT_SAME_TYPE(decltype(s
), std::string
);
260 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
261 using WStr
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
263 std::basic_string
w(std::move(win
), test_allocator
<wchar_t>{});
264 ASSERT_SAME_TYPE(decltype(w
), WStr
);
265 assert(w
== L
"abcdef");
268 { // Testing (13) w/o allocator
269 std::basic_string
s({'a', 'b', 'c'});
270 ASSERT_SAME_TYPE(decltype(s
), std::string
);
273 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
274 std::basic_string
w({L
'a', L
'b', L
'c'});
275 ASSERT_SAME_TYPE(decltype(w
), std::wstring
);
279 { // Testing (13) w/ allocator
280 std::basic_string
s({'a', 'b', 'c'}, test_allocator
<char>{});
281 ASSERT_SAME_TYPE(decltype(s
), BStr
<char, test_allocator
<char>>);
284 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
285 std::basic_string
w({L
'a', L
'b', L
'c'}, test_allocator
<wchar_t>{});
286 ASSERT_SAME_TYPE(decltype(w
), BStr
<wchar_t, test_allocator
<wchar_t>>);
290 { // Testing (14) w/o allocator
291 std::string_view
sv("abc");
292 std::basic_string
s(sv
);
293 ASSERT_SAME_TYPE(decltype(s
), std::string
);
296 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
297 using Expect
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>>;
298 std::basic_string_view
<wchar_t, constexpr_char_traits
<wchar_t>> BSV(L
"abcdef");
299 std::basic_string
w(BSV
);
300 ASSERT_SAME_TYPE(decltype(w
), Expect
);
301 assert(w
== L
"abcdef");
304 { // Testing (14) w/ allocator
305 using ExpectS
= std::basic_string
<char, std::char_traits
<char>, test_allocator
<char>>;
306 std::string_view
sv("abc");
307 std::basic_string
s(sv
, test_allocator
<char>{});
308 ASSERT_SAME_TYPE(decltype(s
), ExpectS
);
311 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
312 using ExpectW
= std::basic_string
<wchar_t, constexpr_char_traits
<wchar_t>, test_allocator
<wchar_t>>;
313 std::basic_string_view
<wchar_t, constexpr_char_traits
<wchar_t>> BSV(L
"abcdef");
314 std::basic_string
w(BSV
, test_allocator
<wchar_t>{});
315 ASSERT_SAME_TYPE(decltype(w
), ExpectW
);
316 assert(w
== L
"abcdef");
319 { // Testing (15) w/o allocator
320 std::string
s0("abc");
321 std::basic_string
s(s0
, 1, 1);
322 ASSERT_SAME_TYPE(decltype(s
), std::string
);
325 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
326 std::wstring
w0(L
"abcdef");
327 std::basic_string
w(w0
, 2, 2);
328 ASSERT_SAME_TYPE(decltype(w
), std::wstring
);
332 { // Testing (15) w/ allocator
333 using ExpectS
= std::basic_string
<char, std::char_traits
<char>, test_allocator
<char>>;
335 std::basic_string
s(s0
, 1, 1, test_allocator
<char>{4});
336 ASSERT_SAME_TYPE(decltype(s
), ExpectS
);
339 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
340 using ExpectW
= std::basic_string
<wchar_t, std::char_traits
<wchar_t>, test_allocator
<wchar_t>>;
341 ExpectW
w0(L
"abcdef");
342 std::basic_string
w(w0
, 2, 2, test_allocator
<wchar_t>{6});
343 ASSERT_SAME_TYPE(decltype(w
), ExpectW
);
351 int main(int, char**) {
353 #if TEST_STD_VER > 17
354 static_assert(test());