summaryrefslogtreecommitdiffstats
path: root/test/std/strings/basic.string/string.cons/implicit_deduction_guides.pass.cpp
blob: 443e88e43c7ca869bdc1ce9417d00f3417444f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11, c++14
// UNSUPPORTED: libcpp-no-deduction-guides
//
// This feature is not yet ready in clang
// http://b/36401676
// XFAIL: *

// <string>

// Test that the constructors offered by std::basic_string are formulated
// so they're compatible with implicit deduction guides.

#include <string>
#include <string_view>
#include <cassert>

#include "test_macros.h"
#include "test_allocator.h"
#include "test_iterators.h"
#include "constexpr_char_traits.hpp"

template <class T, class Alloc = std::allocator<T>>
using BStr = std::basic_string<T, std::char_traits<T>, Alloc>;

// Overloads
//  using A = Allocator;
//  using BS = basic_string
//  using BSV = basic_string_view
// ---------------
// (1)  basic_string() - NOT TESTED
// (2)  basic_string(A const&) - BROKEN
// (3)  basic_string(size_type, CharT, const A& = A())
// (4)  basic_string(BS const&, size_type, A const& = A())
// (5)  basic_string(BS const&, size_type, size_type, A const& = A()) - PARTIALLY BROKEN
// (6)  basic_string(const CharT*, size_type, A const& = A())
// (7)  basic_string(const CharT*, A const& = A())
// (8)  basic_string(InputIt, InputIt, A const& = A()) - BROKEN
// (9)  basic_string(BS const&)
// (10) basic_string(BS const&, A const&)
// (11) basic_string(BS&&)
// (12) basic_string(BS&&, A const&)
// (13) basic_string(initializer_list<CharT>, A const& = A())
// (14) basic_string(BSV, A const& = A())
// (15) basic_string(const T&, size_type, size_type, A const& = A()) - BROKEN
int main()
{
  using TestSizeT = test_allocator<char>::size_type;
  { // Testing (1)
    // Nothing TODO. Cannot deduce without any arguments.
  }
  { // Testing (2)
    // This overload isn't compatible with implicit deduction guides as
    // specified in the standard.
    // const test_allocator<char> alloc{};
    // std::basic_string s(alloc);
  }
  { // Testing (3) w/o allocator
    std::basic_string s(6ull, 'a');
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "aaaaaa");

    std::basic_string w(2ull, L'b');
    ASSERT_SAME_TYPE(decltype(w), std::wstring);
    assert(w == L"bb");
  }
  { // Testing (3) w/ allocator
    std::basic_string s(6ull, 'a', test_allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), BStr<char,test_allocator<char>>);
    assert(s == "aaaaaa");

    std::basic_string w(2ull, L'b', test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
    assert(w == L"bb");
  }
  { // Testing (4) w/o allocator
    const std::string sin("abc");
    std::basic_string s(sin, (size_t)1);
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "bc");

    using WStr = std::basic_string<wchar_t,
                                  constexpr_char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    const WStr win(L"abcdef");
    std::basic_string w(win, (TestSizeT)3);
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"def");
  }
  { // Testing (4) w/ allocator
    const std::string sin("abc");
    std::basic_string s(sin, (size_t)1, std::allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "bc");

    using WStr = std::basic_string<wchar_t,
                                  constexpr_char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    const WStr win(L"abcdef");
    std::basic_string w(win, (TestSizeT)3, test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"def");
  }
  { // Testing (5) w/o allocator
#if 0 // FIXME: This doesn't work
    const std::string sin("abc");
    std::basic_string s(sin, (size_t)1, (size_t)3);
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "bc");

    using WStr = std::basic_string<wchar_t,
                                  constexpr_char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    const WStr win(L"abcdef");
    std::basic_string w(win, (TestSizeT)2, (TestSizeT)3);
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"cde");
#endif
  }
  { // Testing (5) w/ allocator
    const std::string sin("abc");
    std::basic_string s(sin, (size_t)1, (size_t)3, std::allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "bc");

    using WStr = std::basic_string<wchar_t,
                                  constexpr_char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    const WStr win(L"abcdef");
    std::basic_string w(win, (TestSizeT)2, (TestSizeT)3, test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"cde");
  }
  { // Testing (6) w/o allocator
    std::basic_string s("abc", (size_t)2);
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "ab");

    std::basic_string w(L"abcdef", (size_t)3);
    ASSERT_SAME_TYPE(decltype(w), std::wstring);
    assert(w == L"abc");
  }
  { // Testing (6) w/ allocator
    std::basic_string s("abc", (size_t)2, std::allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "ab");

    using WStr = std::basic_string<wchar_t,
                                  std::char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    std::basic_string w(L"abcdef", (TestSizeT)3, test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"abc");
  }
  { // Testing (7) w/o allocator
    std::basic_string s("abc");
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "abc");

    std::basic_string w(L"abcdef");
    ASSERT_SAME_TYPE(decltype(w), std::wstring);
    assert(w == L"abcdef");
  }
  { // Testing (7) w/ allocator
    std::basic_string s("abc", std::allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "abc");

    using WStr = std::basic_string<wchar_t,
                                  std::char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    std::basic_string w(L"abcdef", test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"abcdef");
  }
  { // (8) w/o allocator
    // This overload isn't compatible with implicit deduction guides as
    // specified in the standard.
    // FIXME: Propose adding an explicit guide to the standard?
  }
  { // (8) w/ allocator
    // This overload isn't compatible with implicit deduction guides as
    // specified in the standard.
    // FIXME: Propose adding an explicit guide to the standard?
#if 0
    using It = input_iterator<const char*>;
    const char* input = "abcdef";
    std::basic_string s(It(input), It(input + 3), std::allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), std::string);
#endif
  }
  { // Testing (9)
    const std::string sin("abc");
    std::basic_string s(sin);
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "abc");

    using WStr = std::basic_string<wchar_t,
                                  constexpr_char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    const WStr win(L"abcdef");
    std::basic_string w(win);
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"abcdef");
  }
  { // Testing (10)
    const std::string sin("abc");
    std::basic_string s(sin, std::allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "abc");

    using WStr = std::basic_string<wchar_t,
                                  constexpr_char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    const WStr win(L"abcdef");
    std::basic_string w(win, test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"abcdef");
  }
  { // Testing (11)
    std::string sin("abc");
    std::basic_string s(std::move(sin));
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "abc");

    using WStr = std::basic_string<wchar_t,
                                  constexpr_char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    WStr win(L"abcdef");
    std::basic_string w(std::move(win));
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"abcdef");
  }
  { // Testing (12)
    std::string sin("abc");
    std::basic_string s(std::move(sin), std::allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "abc");

    using WStr = std::basic_string<wchar_t,
                                  constexpr_char_traits<wchar_t>,
                                  test_allocator<wchar_t>>;
    WStr win(L"abcdef");
    std::basic_string w(std::move(win), test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), WStr);
    assert(w == L"abcdef");
  }
  { // Testing (13) w/o allocator
    std::basic_string s({'a', 'b', 'c'});
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "abc");

    std::basic_string w({L'a', L'b', L'c'});
    ASSERT_SAME_TYPE(decltype(w), std::wstring);
    assert(w == L"abc");
  }
  { // Testing (13) w/ allocator
    std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>);
    assert(s == "abc");

    std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
    assert(w == L"abc");
  }
  { // Testing (14) w/o allocator
    std::string_view sv("abc");
    std::basic_string s(sv);
    ASSERT_SAME_TYPE(decltype(s), std::string);
    assert(s == "abc");

    using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>;
    std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
    std::basic_string w(BSV);
    ASSERT_SAME_TYPE(decltype(w), Expect);
    assert(w == L"abcdef");
  }
  { // Testing (14) w/ allocator
    using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
    std::string_view sv("abc");
    std::basic_string s(sv, test_allocator<char>{});
    ASSERT_SAME_TYPE(decltype(s), ExpectS);
    assert(s == "abc");

    using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>,
                                      test_allocator<wchar_t>>;
    std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
    std::basic_string w(BSV, test_allocator<wchar_t>{});
    ASSERT_SAME_TYPE(decltype(w), ExpectW);
    assert(w == L"abcdef");
  }
  { // Testing (15)
    // This overload isn't compatible with implicit deduction guides as
    // specified in the standard.
  }
}