xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/implicit_deduction_guides.pass.cpp (revision 76b26852b6be6e54c86741c7c80ca6b5d74eab2e)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 // UNSUPPORTED: libcpp-no-deduction-guides
12 
13 // <string>
14 
15 // Test that the constructors offered by std::basic_string are formulated
16 // so they're compatible with implicit deduction guides.
17 
18 #include <string>
19 #include <string_view>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "test_allocator.h"
24 #include "test_iterators.h"
25 #include "constexpr_char_traits.hpp"
26 
27 template <class T, class Alloc = std::allocator<T>>
28 using BStr = std::basic_string<T, std::char_traits<T>, Alloc>;
29 
30 // Overloads
31 //  using A = Allocator;
32 //  using BS = basic_string
33 //  using BSV = basic_string_view
34 // ---------------
35 // (1)  basic_string() - NOT TESTED
36 // (2)  basic_string(A const&) - BROKEN
37 // (3)  basic_string(size_type, CharT, const A& = A())
38 // (4)  basic_string(BS const&, size_type, A const& = A())
39 // (5)  basic_string(BS const&, size_type, size_type, A const& = A())
40 // (6)  basic_string(const CharT*, size_type, A const& = A())
41 // (7)  basic_string(const CharT*, A const& = A())
42 // (8)  basic_string(InputIt, InputIt, A const& = A()) - BROKEN
43 // (9)  basic_string(BS const&)
44 // (10) basic_string(BS const&, A const&)
45 // (11) basic_string(BS&&)
46 // (12) basic_string(BS&&, A const&)
47 // (13) basic_string(initializer_list<CharT>, A const& = A())
48 // (14) basic_string(BSV, A const& = A())
49 // (15) basic_string(const T&, size_type, size_type, A const& = A())
50 int main()
51 {
52   using TestSizeT = test_allocator<char>::size_type;
53   { // Testing (1)
54     // Nothing TODO. Cannot deduce without any arguments.
55   }
56   { // Testing (2)
57     // This overload isn't compatible with implicit deduction guides as
58     // specified in the standard.
59     // const test_allocator<char> alloc{};
60     // std::basic_string s(alloc);
61   }
62   { // Testing (3) w/o allocator
63     std::basic_string s(6ull, 'a');
64     ASSERT_SAME_TYPE(decltype(s), std::string);
65     assert(s == "aaaaaa");
66 
67     std::basic_string w(2ull, L'b');
68     ASSERT_SAME_TYPE(decltype(w), std::wstring);
69     assert(w == L"bb");
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     std::basic_string w(2ull, L'b', test_allocator<wchar_t>{});
77     ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
78     assert(w == L"bb");
79   }
80   { // Testing (4) w/o allocator
81     const std::string sin("abc");
82     std::basic_string s(sin, (size_t)1);
83     ASSERT_SAME_TYPE(decltype(s), std::string);
84     assert(s == "bc");
85 
86     using WStr = std::basic_string<wchar_t,
87                                   constexpr_char_traits<wchar_t>,
88                                   test_allocator<wchar_t>>;
89     const WStr win(L"abcdef");
90     std::basic_string w(win, (TestSizeT)3);
91     ASSERT_SAME_TYPE(decltype(w), WStr);
92     assert(w == L"def");
93   }
94   { // Testing (4) w/ allocator
95     const std::string sin("abc");
96     std::basic_string s(sin, (size_t)1, std::allocator<char>{});
97     ASSERT_SAME_TYPE(decltype(s), std::string);
98     assert(s == "bc");
99 
100     using WStr = std::basic_string<wchar_t,
101                                   constexpr_char_traits<wchar_t>,
102                                   test_allocator<wchar_t>>;
103     const WStr win(L"abcdef");
104     std::basic_string w(win, (TestSizeT)3, test_allocator<wchar_t>{});
105     ASSERT_SAME_TYPE(decltype(w), WStr);
106     assert(w == L"def");
107   }
108   { // Testing (5) w/o allocator
109     const std::string sin("abc");
110     std::basic_string s(sin, (size_t)1, (size_t)3);
111     ASSERT_SAME_TYPE(decltype(s), std::string);
112     assert(s == "bc");
113 
114     using WStr = std::basic_string<wchar_t,
115                                   constexpr_char_traits<wchar_t>,
116                                   test_allocator<wchar_t>>;
117     const WStr win(L"abcdef");
118     std::basic_string w(win, (TestSizeT)2, (TestSizeT)3);
119     ASSERT_SAME_TYPE(decltype(w), WStr);
120     assert(w == L"cde");
121   }
122   { // Testing (5) w/ allocator
123     const std::string sin("abc");
124     std::basic_string s(sin, (size_t)1, (size_t)3, std::allocator<char>{});
125     ASSERT_SAME_TYPE(decltype(s), std::string);
126     assert(s == "bc");
127 
128     using WStr = std::basic_string<wchar_t,
129                                   constexpr_char_traits<wchar_t>,
130                                   test_allocator<wchar_t>>;
131     const WStr win(L"abcdef");
132     std::basic_string w(win, (TestSizeT)2, (TestSizeT)3, test_allocator<wchar_t>{});
133     ASSERT_SAME_TYPE(decltype(w), WStr);
134     assert(w == L"cde");
135   }
136   { // Testing (6) w/o allocator
137     std::basic_string s("abc", (size_t)2);
138     ASSERT_SAME_TYPE(decltype(s), std::string);
139     assert(s == "ab");
140 
141     std::basic_string w(L"abcdef", (size_t)3);
142     ASSERT_SAME_TYPE(decltype(w), std::wstring);
143     assert(w == L"abc");
144   }
145   { // Testing (6) w/ allocator
146     std::basic_string s("abc", (size_t)2, std::allocator<char>{});
147     ASSERT_SAME_TYPE(decltype(s), std::string);
148     assert(s == "ab");
149 
150     using WStr = std::basic_string<wchar_t,
151                                   std::char_traits<wchar_t>,
152                                   test_allocator<wchar_t>>;
153     std::basic_string w(L"abcdef", (TestSizeT)3, test_allocator<wchar_t>{});
154     ASSERT_SAME_TYPE(decltype(w), WStr);
155     assert(w == L"abc");
156   }
157   { // Testing (7) w/o allocator
158     std::basic_string s("abc");
159     ASSERT_SAME_TYPE(decltype(s), std::string);
160     assert(s == "abc");
161 
162     std::basic_string w(L"abcdef");
163     ASSERT_SAME_TYPE(decltype(w), std::wstring);
164     assert(w == L"abcdef");
165   }
166   { // Testing (7) w/ allocator
167     std::basic_string s("abc", std::allocator<char>{});
168     ASSERT_SAME_TYPE(decltype(s), std::string);
169     assert(s == "abc");
170 
171     using WStr = std::basic_string<wchar_t,
172                                   std::char_traits<wchar_t>,
173                                   test_allocator<wchar_t>>;
174     std::basic_string w(L"abcdef", test_allocator<wchar_t>{});
175     ASSERT_SAME_TYPE(decltype(w), WStr);
176     assert(w == L"abcdef");
177   }
178   { // (8) w/o allocator
179     using It = input_iterator<const char*>;
180     const char* input = "abcdef";
181     std::basic_string s(It(input), It(input + 3), std::allocator<char>{});
182     ASSERT_SAME_TYPE(decltype(s), std::string);
183     assert(s == "abc");
184   }
185   { // (8) w/ allocator
186     using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>;
187     using It = input_iterator<const wchar_t*>;
188     const wchar_t* input = L"abcdef";
189     std::basic_string s(It(input), It(input + 3), test_allocator<wchar_t>{});
190     ASSERT_SAME_TYPE(decltype(s), ExpectW);
191     assert(s == L"abc");
192   }
193   { // Testing (9)
194     const std::string sin("abc");
195     std::basic_string s(sin);
196     ASSERT_SAME_TYPE(decltype(s), std::string);
197     assert(s == "abc");
198 
199     using WStr = std::basic_string<wchar_t,
200                                   constexpr_char_traits<wchar_t>,
201                                   test_allocator<wchar_t>>;
202     const WStr win(L"abcdef");
203     std::basic_string w(win);
204     ASSERT_SAME_TYPE(decltype(w), WStr);
205     assert(w == L"abcdef");
206   }
207   { // Testing (10)
208     const std::string sin("abc");
209     std::basic_string s(sin, std::allocator<char>{});
210     ASSERT_SAME_TYPE(decltype(s), std::string);
211     assert(s == "abc");
212 
213     using WStr = std::basic_string<wchar_t,
214                                   constexpr_char_traits<wchar_t>,
215                                   test_allocator<wchar_t>>;
216     const WStr win(L"abcdef");
217     std::basic_string w(win, test_allocator<wchar_t>{});
218     ASSERT_SAME_TYPE(decltype(w), WStr);
219     assert(w == L"abcdef");
220   }
221   { // Testing (11)
222     std::string sin("abc");
223     std::basic_string s(std::move(sin));
224     ASSERT_SAME_TYPE(decltype(s), std::string);
225     assert(s == "abc");
226 
227     using WStr = std::basic_string<wchar_t,
228                                   constexpr_char_traits<wchar_t>,
229                                   test_allocator<wchar_t>>;
230     WStr win(L"abcdef");
231     std::basic_string w(std::move(win));
232     ASSERT_SAME_TYPE(decltype(w), WStr);
233     assert(w == L"abcdef");
234   }
235   { // Testing (12)
236     std::string sin("abc");
237     std::basic_string s(std::move(sin), std::allocator<char>{});
238     ASSERT_SAME_TYPE(decltype(s), std::string);
239     assert(s == "abc");
240 
241     using WStr = std::basic_string<wchar_t,
242                                   constexpr_char_traits<wchar_t>,
243                                   test_allocator<wchar_t>>;
244     WStr win(L"abcdef");
245     std::basic_string w(std::move(win), test_allocator<wchar_t>{});
246     ASSERT_SAME_TYPE(decltype(w), WStr);
247     assert(w == L"abcdef");
248   }
249   { // Testing (13) w/o allocator
250     std::basic_string s({'a', 'b', 'c'});
251     ASSERT_SAME_TYPE(decltype(s), std::string);
252     assert(s == "abc");
253 
254     std::basic_string w({L'a', L'b', L'c'});
255     ASSERT_SAME_TYPE(decltype(w), std::wstring);
256     assert(w == L"abc");
257   }
258   { // Testing (13) w/ allocator
259     std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{});
260     ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>);
261     assert(s == "abc");
262 
263     std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{});
264     ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
265     assert(w == L"abc");
266   }
267   { // Testing (14) w/o allocator
268     std::string_view sv("abc");
269     std::basic_string s(sv);
270     ASSERT_SAME_TYPE(decltype(s), std::string);
271     assert(s == "abc");
272 
273     using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>;
274     std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
275     std::basic_string w(BSV);
276     ASSERT_SAME_TYPE(decltype(w), Expect);
277     assert(w == L"abcdef");
278   }
279   { // Testing (14) w/ allocator
280     using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
281     std::string_view sv("abc");
282     std::basic_string s(sv, test_allocator<char>{});
283     ASSERT_SAME_TYPE(decltype(s), ExpectS);
284     assert(s == "abc");
285 
286     using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>,
287                                       test_allocator<wchar_t>>;
288     std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
289     std::basic_string w(BSV, test_allocator<wchar_t>{});
290     ASSERT_SAME_TYPE(decltype(w), ExpectW);
291     assert(w == L"abcdef");
292   }
293   { // Testing (15) w/o allocator
294     std::string s0("abc");
295     std::basic_string s(s0, 1, 1);
296     ASSERT_SAME_TYPE(decltype(s), std::string);
297     assert(s == "b");
298 
299     std::wstring w0(L"abcdef");
300     std::basic_string w(w0, 2, 2);
301     ASSERT_SAME_TYPE(decltype(w), std::wstring);
302     assert(w == L"cd");
303   }
304   { // Testing (15) w/ allocator
305     using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
306     ExpectS s0("abc");
307     std::basic_string s(s0, 1, 1, test_allocator<char>{4});
308     ASSERT_SAME_TYPE(decltype(s), ExpectS);
309     assert(s == "b");
310 
311     using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>;
312     ExpectW w0(L"abcdef");
313     std::basic_string w(w0, 2, 2, test_allocator<wchar_t>{6});
314     ASSERT_SAME_TYPE(decltype(w), ExpectW);
315     assert(w == L"cd");
316   }
317 }
318