xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/implicit_deduction_guides.pass.cpp (revision fb855eb941b6d740cc6560297d0b4d3201dcaf9f)
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // <string>
12 
13 // Test that the constructors offered by std::basic_string are formulated
14 // so they're compatible with implicit deduction guides.
15 
16 #include <string>
17 #include <string_view>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "test_allocator.h"
22 #include "test_iterators.h"
23 #include "constexpr_char_traits.h"
24 
25 template <class T, class Alloc = std::allocator<T>>
26 using BStr = std::basic_string<T, std::char_traits<T>, Alloc>;
27 
28 // Overloads
29 //  using A = Allocator;
30 //  using BS = basic_string
31 //  using BSV = basic_string_view
32 // ---------------
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 
50   using TestSizeT = test_allocator<char>::size_type;
51   { // Testing (1)
52     // Nothing to do. Cannot deduce without any arguments.
53   }
54   { // Testing (2)
55     // This overload isn't compatible with implicit deduction guides as
56     // specified in the standard.
57     // const test_allocator<char> alloc{};
58     // std::basic_string s(alloc);
59   }
60   { // Testing (3) w/o allocator
61     std::basic_string s(6ull, 'a');
62     ASSERT_SAME_TYPE(decltype(s), std::string);
63     assert(s == "aaaaaa");
64 
65 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
66     std::basic_string w(2ull, L'b');
67     ASSERT_SAME_TYPE(decltype(w), std::wstring);
68     assert(w == L"bb");
69 #endif
70   }
71   { // Testing (3) w/ allocator
72     std::basic_string s(6ull, 'a', test_allocator<char>{});
73     ASSERT_SAME_TYPE(decltype(s), BStr<char,test_allocator<char>>);
74     assert(s == "aaaaaa");
75 
76 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
77     std::basic_string w(2ull, L'b', test_allocator<wchar_t>{});
78     ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
79     assert(w == L"bb");
80 #endif
81   }
82   { // Testing (4) w/o allocator
83     const std::string sin("abc");
84     std::basic_string s(sin, (std::size_t)1);
85     ASSERT_SAME_TYPE(decltype(s), std::string);
86     assert(s == "bc");
87 
88 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
89     using WStr = std::basic_string<wchar_t,
90                                   constexpr_char_traits<wchar_t>,
91                                   test_allocator<wchar_t>>;
92     const WStr win(L"abcdef");
93     std::basic_string w(win, (TestSizeT)3);
94     ASSERT_SAME_TYPE(decltype(w), WStr);
95     assert(w == L"def");
96 #endif
97   }
98   { // Testing (4) w/ allocator
99     const std::string sin("abc");
100     std::basic_string s(sin, (std::size_t)1, std::allocator<char>{});
101     ASSERT_SAME_TYPE(decltype(s), std::string);
102     assert(s == "bc");
103 
104 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
105     using WStr = std::basic_string<wchar_t,
106                                   constexpr_char_traits<wchar_t>,
107                                   test_allocator<wchar_t>>;
108     const WStr win(L"abcdef");
109     std::basic_string w(win, (TestSizeT)3, test_allocator<wchar_t>{});
110     ASSERT_SAME_TYPE(decltype(w), WStr);
111     assert(w == L"def");
112 #endif
113   }
114   { // Testing (5) w/o allocator
115     const std::string sin("abc");
116     std::basic_string s(sin, (std::size_t)1, (size_t)3);
117     ASSERT_SAME_TYPE(decltype(s), std::string);
118     assert(s == "bc");
119 
120 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
121     using WStr = std::basic_string<wchar_t,
122                                   constexpr_char_traits<wchar_t>,
123                                   test_allocator<wchar_t>>;
124     const WStr win(L"abcdef");
125     std::basic_string w(win, (TestSizeT)2, (TestSizeT)3);
126     ASSERT_SAME_TYPE(decltype(w), WStr);
127     assert(w == L"cde");
128 #endif
129   }
130   { // Testing (5) w/ allocator
131     const std::string sin("abc");
132     std::basic_string s(sin, (std::size_t)1, (size_t)3, std::allocator<char>{});
133     ASSERT_SAME_TYPE(decltype(s), std::string);
134     assert(s == "bc");
135 
136 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
137     using WStr = std::basic_string<wchar_t,
138                                   constexpr_char_traits<wchar_t>,
139                                   test_allocator<wchar_t>>;
140     const WStr win(L"abcdef");
141     std::basic_string w(win, (TestSizeT)2, (TestSizeT)3, test_allocator<wchar_t>{});
142     ASSERT_SAME_TYPE(decltype(w), WStr);
143     assert(w == L"cde");
144 #endif
145   }
146   { // Testing (6) w/o allocator
147     std::basic_string s("abc", (std::size_t)2);
148     ASSERT_SAME_TYPE(decltype(s), std::string);
149     assert(s == "ab");
150 
151 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
152     std::basic_string w(L"abcdef", (std::size_t)3);
153     ASSERT_SAME_TYPE(decltype(w), std::wstring);
154     assert(w == L"abc");
155 #endif
156   }
157   { // Testing (6) w/ allocator
158     std::basic_string s("abc", (std::size_t)2, std::allocator<char>{});
159     ASSERT_SAME_TYPE(decltype(s), std::string);
160     assert(s == "ab");
161 
162 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
163     using WStr = std::basic_string<wchar_t,
164                                   std::char_traits<wchar_t>,
165                                   test_allocator<wchar_t>>;
166     std::basic_string w(L"abcdef", (TestSizeT)3, test_allocator<wchar_t>{});
167     ASSERT_SAME_TYPE(decltype(w), WStr);
168     assert(w == L"abc");
169 #endif
170   }
171   { // Testing (7) w/o allocator
172     std::basic_string s("abc");
173     ASSERT_SAME_TYPE(decltype(s), std::string);
174     assert(s == "abc");
175 
176 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
177     std::basic_string w(L"abcdef");
178     ASSERT_SAME_TYPE(decltype(w), std::wstring);
179     assert(w == L"abcdef");
180 #endif
181   }
182   { // Testing (7) w/ allocator
183     std::basic_string s("abc", std::allocator<char>{});
184     ASSERT_SAME_TYPE(decltype(s), std::string);
185     assert(s == "abc");
186 
187 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
188     using WStr = std::basic_string<wchar_t,
189                                   std::char_traits<wchar_t>,
190                                   test_allocator<wchar_t>>;
191     std::basic_string w(L"abcdef", test_allocator<wchar_t>{});
192     ASSERT_SAME_TYPE(decltype(w), WStr);
193     assert(w == L"abcdef");
194 #endif
195   }
196   { // (8) w/o allocator
197     using It = cpp17_input_iterator<const char*>;
198     const char* input = "abcdef";
199     std::basic_string s(It(input), It(input + 3), std::allocator<char>{});
200     ASSERT_SAME_TYPE(decltype(s), std::string);
201     assert(s == "abc");
202   }
203   { // (8) w/ allocator
204     {
205       using Expect = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
206       using It = cpp17_input_iterator<const char*>;
207       const char* input = "abcdef";
208       std::basic_string s(It(input), It(input + 3), test_allocator<char>{});
209       ASSERT_SAME_TYPE(decltype(s), Expect);
210       assert(s == "abc");
211     }
212 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
213     {
214       using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>;
215       using It = cpp17_input_iterator<const wchar_t*>;
216       const wchar_t* input = L"abcdef";
217       std::basic_string s(It(input), It(input + 3), test_allocator<wchar_t>{});
218       ASSERT_SAME_TYPE(decltype(s), ExpectW);
219       assert(s == L"abc");
220     }
221 #endif
222   }
223   { // Testing (9)
224     const std::string sin("abc");
225     std::basic_string s(sin);
226     ASSERT_SAME_TYPE(decltype(s), std::string);
227     assert(s == "abc");
228 
229 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
230     using WStr = std::basic_string<wchar_t,
231                                   constexpr_char_traits<wchar_t>,
232                                   test_allocator<wchar_t>>;
233     const WStr win(L"abcdef");
234     std::basic_string w(win);
235     ASSERT_SAME_TYPE(decltype(w), WStr);
236     assert(w == L"abcdef");
237 #endif
238   }
239   { // Testing (10)
240     const std::string sin("abc");
241     std::basic_string s(sin, std::allocator<char>{});
242     ASSERT_SAME_TYPE(decltype(s), std::string);
243     assert(s == "abc");
244 
245 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
246     using WStr = std::basic_string<wchar_t,
247                                   constexpr_char_traits<wchar_t>,
248                                   test_allocator<wchar_t>>;
249     const WStr win(L"abcdef");
250     std::basic_string w(win, test_allocator<wchar_t>{});
251     ASSERT_SAME_TYPE(decltype(w), WStr);
252     assert(w == L"abcdef");
253 #endif
254   }
255   { // Testing (11)
256     std::string sin("abc");
257     std::basic_string s(std::move(sin));
258     ASSERT_SAME_TYPE(decltype(s), std::string);
259     assert(s == "abc");
260 
261 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
262     using WStr = std::basic_string<wchar_t,
263                                   constexpr_char_traits<wchar_t>,
264                                   test_allocator<wchar_t>>;
265     WStr win(L"abcdef");
266     std::basic_string w(std::move(win));
267     ASSERT_SAME_TYPE(decltype(w), WStr);
268     assert(w == L"abcdef");
269 #endif
270   }
271   { // Testing (12)
272     std::string sin("abc");
273     std::basic_string s(std::move(sin), std::allocator<char>{});
274     ASSERT_SAME_TYPE(decltype(s), std::string);
275     assert(s == "abc");
276 
277 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
278     using WStr = std::basic_string<wchar_t,
279                                   constexpr_char_traits<wchar_t>,
280                                   test_allocator<wchar_t>>;
281     WStr win(L"abcdef");
282     std::basic_string w(std::move(win), test_allocator<wchar_t>{});
283     ASSERT_SAME_TYPE(decltype(w), WStr);
284     assert(w == L"abcdef");
285 #endif
286   }
287   { // Testing (13) w/o allocator
288     std::basic_string s({'a', 'b', 'c'});
289     ASSERT_SAME_TYPE(decltype(s), std::string);
290     assert(s == "abc");
291 
292 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
293     std::basic_string w({L'a', L'b', L'c'});
294     ASSERT_SAME_TYPE(decltype(w), std::wstring);
295     assert(w == L"abc");
296 #endif
297   }
298   { // Testing (13) w/ allocator
299     std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{});
300     ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>);
301     assert(s == "abc");
302 
303 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
304     std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{});
305     ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
306     assert(w == L"abc");
307 #endif
308   }
309   { // Testing (14) w/o allocator
310     std::string_view sv("abc");
311     std::basic_string s(sv);
312     ASSERT_SAME_TYPE(decltype(s), std::string);
313     assert(s == "abc");
314 
315 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
316     using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>;
317     std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
318     std::basic_string w(BSV);
319     ASSERT_SAME_TYPE(decltype(w), Expect);
320     assert(w == L"abcdef");
321 #endif
322   }
323   { // Testing (14) w/ allocator
324     using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
325     std::string_view sv("abc");
326     std::basic_string s(sv, test_allocator<char>{});
327     ASSERT_SAME_TYPE(decltype(s), ExpectS);
328     assert(s == "abc");
329 
330 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
331     using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>,
332                                       test_allocator<wchar_t>>;
333     std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
334     std::basic_string w(BSV, test_allocator<wchar_t>{});
335     ASSERT_SAME_TYPE(decltype(w), ExpectW);
336     assert(w == L"abcdef");
337 #endif
338   }
339   { // Testing (15) w/o allocator
340     std::string s0("abc");
341     std::basic_string s(s0, 1, 1);
342     ASSERT_SAME_TYPE(decltype(s), std::string);
343     assert(s == "b");
344 
345 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
346     std::wstring w0(L"abcdef");
347     std::basic_string w(w0, 2, 2);
348     ASSERT_SAME_TYPE(decltype(w), std::wstring);
349     assert(w == L"cd");
350 #endif
351   }
352   { // Testing (15) w/ allocator
353     using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
354     ExpectS s0("abc");
355     std::basic_string s(s0, 1, 1, test_allocator<char>{4});
356     ASSERT_SAME_TYPE(decltype(s), ExpectS);
357     assert(s == "b");
358 
359 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
360     using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>;
361     ExpectW w0(L"abcdef");
362     std::basic_string w(w0, 2, 2, test_allocator<wchar_t>{6});
363     ASSERT_SAME_TYPE(decltype(w), ExpectW);
364     assert(w == L"cd");
365 #endif
366   }
367 
368   return true;
369 }
370 
371 int main(int, char**)
372 {
373   test();
374 #if TEST_STD_VER > 17
375   static_assert(test());
376 #endif
377 
378   return 0;
379 }
380