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 using TestSizeT = test_allocator<char>::size_type; 50 { 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 } { // Testing (3) w/o allocator 60 std::basic_string s(6ull, 'a'); 61 ASSERT_SAME_TYPE(decltype(s), std::string); 62 assert(s == "aaaaaa"); 63 64 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 65 std::basic_string w(2ull, L'b'); 66 ASSERT_SAME_TYPE(decltype(w), std::wstring); 67 assert(w == L"bb"); 68 #endif 69 } 70 { // Testing (3) w/ allocator 71 std::basic_string s(6ull, 'a', test_allocator<char>{}); 72 ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>); 73 assert(s == "aaaaaa"); 74 75 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 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 #endif 80 } 81 { // Testing (4) w/o allocator 82 const std::string sin("abc"); 83 std::basic_string s(sin, (std::size_t)1); 84 ASSERT_SAME_TYPE(decltype(s), std::string); 85 assert(s == "bc"); 86 87 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 88 using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, 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 #endif 94 } 95 { // Testing (4) w/ allocator 96 const std::string sin("abc"); 97 std::basic_string s(sin, (std::size_t)1, std::allocator<char>{}); 98 ASSERT_SAME_TYPE(decltype(s), std::string); 99 assert(s == "bc"); 100 101 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 102 using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, 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 #endif 108 } 109 { // Testing (5) w/o allocator 110 const std::string sin("abc"); 111 std::basic_string s(sin, (std::size_t)1, (size_t)3); 112 ASSERT_SAME_TYPE(decltype(s), std::string); 113 assert(s == "bc"); 114 115 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 116 using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, 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 #endif 122 } 123 { // Testing (5) w/ allocator 124 const std::string sin("abc"); 125 std::basic_string s(sin, (std::size_t)1, (size_t)3, std::allocator<char>{}); 126 ASSERT_SAME_TYPE(decltype(s), std::string); 127 assert(s == "bc"); 128 129 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 130 using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, 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 #endif 136 } 137 { // Testing (6) w/o allocator 138 std::basic_string s("abc", (std::size_t)2); 139 ASSERT_SAME_TYPE(decltype(s), std::string); 140 assert(s == "ab"); 141 142 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 143 std::basic_string w(L"abcdef", (std::size_t)3); 144 ASSERT_SAME_TYPE(decltype(w), std::wstring); 145 assert(w == L"abc"); 146 #endif 147 } 148 { // Testing (6) w/ allocator 149 std::basic_string s("abc", (std::size_t)2, std::allocator<char>{}); 150 ASSERT_SAME_TYPE(decltype(s), std::string); 151 assert(s == "ab"); 152 153 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 154 using WStr = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>; 155 std::basic_string w(L"abcdef", (TestSizeT)3, test_allocator<wchar_t>{}); 156 ASSERT_SAME_TYPE(decltype(w), WStr); 157 assert(w == L"abc"); 158 #endif 159 } 160 { // Testing (7) w/o allocator 161 std::basic_string s("abc"); 162 ASSERT_SAME_TYPE(decltype(s), std::string); 163 assert(s == "abc"); 164 165 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 166 std::basic_string w(L"abcdef"); 167 ASSERT_SAME_TYPE(decltype(w), std::wstring); 168 assert(w == L"abcdef"); 169 #endif 170 } 171 { // Testing (7) w/ allocator 172 std::basic_string s("abc", std::allocator<char>{}); 173 ASSERT_SAME_TYPE(decltype(s), std::string); 174 assert(s == "abc"); 175 176 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 177 using WStr = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>; 178 std::basic_string w(L"abcdef", test_allocator<wchar_t>{}); 179 ASSERT_SAME_TYPE(decltype(w), WStr); 180 assert(w == L"abcdef"); 181 #endif 182 } 183 { // (8) w/o allocator 184 using It = cpp17_input_iterator<const char*>; 185 const char* input = "abcdef"; 186 std::basic_string s(It(input), It(input + 3), std::allocator<char>{}); 187 ASSERT_SAME_TYPE(decltype(s), std::string); 188 assert(s == "abc"); 189 } 190 {// (8) w/ allocator 191 {using Expect = std::basic_string<char, std::char_traits<char>, test_allocator<char>>; 192 using It = cpp17_input_iterator<const char*>; 193 const char* input = "abcdef"; 194 std::basic_string s(It(input), It(input + 3), test_allocator<char>{}); 195 ASSERT_SAME_TYPE(decltype(s), Expect); 196 assert(s == "abc"); 197 } 198 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 199 { 200 using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>; 201 using It = cpp17_input_iterator<const wchar_t*>; 202 const wchar_t* input = L"abcdef"; 203 std::basic_string s(It(input), It(input + 3), test_allocator<wchar_t>{}); 204 ASSERT_SAME_TYPE(decltype(s), ExpectW); 205 assert(s == L"abc"); 206 } 207 #endif 208 } 209 { // Testing (9) 210 const std::string sin("abc"); 211 std::basic_string s(sin); 212 ASSERT_SAME_TYPE(decltype(s), std::string); 213 assert(s == "abc"); 214 215 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 216 using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, test_allocator<wchar_t>>; 217 const WStr win(L"abcdef"); 218 std::basic_string w(win); 219 ASSERT_SAME_TYPE(decltype(w), WStr); 220 assert(w == L"abcdef"); 221 #endif 222 } 223 { // Testing (10) 224 const std::string sin("abc"); 225 std::basic_string s(sin, std::allocator<char>{}); 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, constexpr_char_traits<wchar_t>, test_allocator<wchar_t>>; 231 const WStr win(L"abcdef"); 232 std::basic_string w(win, test_allocator<wchar_t>{}); 233 ASSERT_SAME_TYPE(decltype(w), WStr); 234 assert(w == L"abcdef"); 235 #endif 236 } 237 { // Testing (11) 238 std::string sin("abc"); 239 std::basic_string s(std::move(sin)); 240 ASSERT_SAME_TYPE(decltype(s), std::string); 241 assert(s == "abc"); 242 243 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 244 using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, test_allocator<wchar_t>>; 245 WStr win(L"abcdef"); 246 std::basic_string w(std::move(win)); 247 ASSERT_SAME_TYPE(decltype(w), WStr); 248 assert(w == L"abcdef"); 249 #endif 250 } 251 { // Testing (12) 252 std::string sin("abc"); 253 std::basic_string s(std::move(sin), std::allocator<char>{}); 254 ASSERT_SAME_TYPE(decltype(s), std::string); 255 assert(s == "abc"); 256 257 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 258 using WStr = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, test_allocator<wchar_t>>; 259 WStr win(L"abcdef"); 260 std::basic_string w(std::move(win), test_allocator<wchar_t>{}); 261 ASSERT_SAME_TYPE(decltype(w), WStr); 262 assert(w == L"abcdef"); 263 #endif 264 } 265 { // Testing (13) w/o allocator 266 std::basic_string s({'a', 'b', 'c'}); 267 ASSERT_SAME_TYPE(decltype(s), std::string); 268 assert(s == "abc"); 269 270 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 271 std::basic_string w({L'a', L'b', L'c'}); 272 ASSERT_SAME_TYPE(decltype(w), std::wstring); 273 assert(w == L"abc"); 274 #endif 275 } 276 { // Testing (13) w/ allocator 277 std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{}); 278 ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>); 279 assert(s == "abc"); 280 281 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 282 std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{}); 283 ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>); 284 assert(w == L"abc"); 285 #endif 286 } 287 { // Testing (14) w/o allocator 288 std::string_view sv("abc"); 289 std::basic_string s(sv); 290 ASSERT_SAME_TYPE(decltype(s), std::string); 291 assert(s == "abc"); 292 293 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 294 using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>; 295 std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef"); 296 std::basic_string w(BSV); 297 ASSERT_SAME_TYPE(decltype(w), Expect); 298 assert(w == L"abcdef"); 299 #endif 300 } 301 { // Testing (14) w/ allocator 302 using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>; 303 std::string_view sv("abc"); 304 std::basic_string s(sv, test_allocator<char>{}); 305 ASSERT_SAME_TYPE(decltype(s), ExpectS); 306 assert(s == "abc"); 307 308 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 309 using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, test_allocator<wchar_t>>; 310 std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef"); 311 std::basic_string w(BSV, test_allocator<wchar_t>{}); 312 ASSERT_SAME_TYPE(decltype(w), ExpectW); 313 assert(w == L"abcdef"); 314 #endif 315 } 316 { // Testing (15) w/o allocator 317 std::string s0("abc"); 318 std::basic_string s(s0, 1, 1); 319 ASSERT_SAME_TYPE(decltype(s), std::string); 320 assert(s == "b"); 321 322 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 323 std::wstring w0(L"abcdef"); 324 std::basic_string w(w0, 2, 2); 325 ASSERT_SAME_TYPE(decltype(w), std::wstring); 326 assert(w == L"cd"); 327 #endif 328 } 329 { // Testing (15) w/ allocator 330 using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>; 331 ExpectS s0("abc"); 332 std::basic_string s(s0, 1, 1, test_allocator<char>{4}); 333 ASSERT_SAME_TYPE(decltype(s), ExpectS); 334 assert(s == "b"); 335 336 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 337 using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>; 338 ExpectW w0(L"abcdef"); 339 std::basic_string w(w0, 2, 2, test_allocator<wchar_t>{6}); 340 ASSERT_SAME_TYPE(decltype(w), ExpectW); 341 assert(w == L"cd"); 342 #endif 343 } 344 345 return true; 346 } 347 348 int main(int, char**) { 349 test(); 350 #if TEST_STD_VER > 17 351 static_assert(test()); 352 #endif 353 354 return 0; 355 } 356