xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/implicit_deduction_guides.pass.cpp (revision b3a39a9bdb6b3300c872e0229fadbaac430649c1)
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())
test()48 TEST_CONSTEXPR_CXX20 bool test() {
49   using TestSizeT = test_allocator<char>::size_type;
50   {
51     // Testing (1)
52     // Nothing to do. Cannot deduce without any arguments.
53   }
54   {
55     // Testing (2)
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);
60   }
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");
65 
66 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
67     std::basic_string w(2ull, L'b');
68     ASSERT_SAME_TYPE(decltype(w), std::wstring);
69     assert(w == L"bb");
70 #endif
71   }
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");
76 
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>>);
80     assert(w == L"bb");
81 #endif
82   }
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);
87     assert(s == "bc");
88 
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);
94     assert(w == L"def");
95 #endif
96   }
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);
101     assert(s == "bc");
102 
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);
108     assert(w == L"def");
109 #endif
110   }
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);
115     assert(s == "bc");
116 
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);
122     assert(w == L"cde");
123 #endif
124   }
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);
129     assert(s == "bc");
130 
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);
136     assert(w == L"cde");
137 #endif
138   }
139   { // Testing (6) w/o allocator
140     std::basic_string s("abc", (std::size_t)2);
141     ASSERT_SAME_TYPE(decltype(s), std::string);
142     assert(s == "ab");
143 
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);
147     assert(w == L"abc");
148 #endif
149   }
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);
153     assert(s == "ab");
154 
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);
159     assert(w == L"abc");
160 #endif
161   }
162   { // Testing (7) w/o allocator
163     std::basic_string s("abc");
164     ASSERT_SAME_TYPE(decltype(s), std::string);
165     assert(s == "abc");
166 
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");
171 #endif
172   }
173   { // Testing (7) w/ allocator
174     std::basic_string s("abc", std::allocator<char>{});
175     ASSERT_SAME_TYPE(decltype(s), std::string);
176     assert(s == "abc");
177 
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");
183 #endif
184   }
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);
190     assert(s == "abc");
191   }
192   { // (8) w/ allocator
193     {
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);
199       assert(s == "abc");
200     }
201 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
202     {
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);
208       assert(s == L"abc");
209     }
210 #endif
211   }
212   { // Testing (9)
213     const std::string sin("abc");
214     std::basic_string s(sin);
215     ASSERT_SAME_TYPE(decltype(s), std::string);
216     assert(s == "abc");
217 
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");
224 #endif
225   }
226   { // Testing (10)
227     const std::string sin("abc");
228     std::basic_string s(sin, std::allocator<char>{});
229     ASSERT_SAME_TYPE(decltype(s), std::string);
230     assert(s == "abc");
231 
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");
238 #endif
239   }
240   { // Testing (11)
241     std::string sin("abc");
242     std::basic_string s(std::move(sin));
243     ASSERT_SAME_TYPE(decltype(s), std::string);
244     assert(s == "abc");
245 
246 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
247     using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, test_allocator<wchar_t>>;
248     WStr win(L"abcdef");
249     std::basic_string w(std::move(win));
250     ASSERT_SAME_TYPE(decltype(w), WStr);
251     assert(w == L"abcdef");
252 #endif
253   }
254   { // Testing (12)
255     std::string sin("abc");
256     std::basic_string s(std::move(sin), std::allocator<char>{});
257     ASSERT_SAME_TYPE(decltype(s), std::string);
258     assert(s == "abc");
259 
260 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
261     using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, test_allocator<wchar_t>>;
262     WStr win(L"abcdef");
263     std::basic_string w(std::move(win), test_allocator<wchar_t>{});
264     ASSERT_SAME_TYPE(decltype(w), WStr);
265     assert(w == L"abcdef");
266 #endif
267   }
268   { // Testing (13) w/o allocator
269     std::basic_string s({'a', 'b', 'c'});
270     ASSERT_SAME_TYPE(decltype(s), std::string);
271     assert(s == "abc");
272 
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);
276     assert(w == L"abc");
277 #endif
278   }
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>>);
282     assert(s == "abc");
283 
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>>);
287     assert(w == L"abc");
288 #endif
289   }
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);
294     assert(s == "abc");
295 
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");
302 #endif
303   }
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);
309     assert(s == "abc");
310 
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");
317 #endif
318   }
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);
323     assert(s == "b");
324 
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);
329     assert(w == L"cd");
330 #endif
331   }
332   { // Testing (15) w/ allocator
333     using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
334     ExpectS s0("abc");
335     std::basic_string s(s0, 1, 1, test_allocator<char>{4});
336     ASSERT_SAME_TYPE(decltype(s), ExpectS);
337     assert(s == "b");
338 
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);
344     assert(w == L"cd");
345 #endif
346   }
347 
348   return true;
349 }
350 
main(int,char **)351 int main(int, char**) {
352   test();
353 #if TEST_STD_VER > 17
354   static_assert(test());
355 #endif
356 
357   return 0;
358 }
359