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 int main(int, char**) 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 std::basic_string w(2ull, L'b'); 66 ASSERT_SAME_TYPE(decltype(w), std::wstring); 67 assert(w == L"bb"); 68 } 69 { // Testing (3) w/ allocator 70 std::basic_string s(6ull, 'a', test_allocator<char>{}); 71 ASSERT_SAME_TYPE(decltype(s), BStr<char,test_allocator<char>>); 72 assert(s == "aaaaaa"); 73 74 std::basic_string w(2ull, L'b', test_allocator<wchar_t>{}); 75 ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>); 76 assert(w == L"bb"); 77 } 78 { // Testing (4) w/o allocator 79 const std::string sin("abc"); 80 std::basic_string s(sin, (size_t)1); 81 ASSERT_SAME_TYPE(decltype(s), std::string); 82 assert(s == "bc"); 83 84 using WStr = std::basic_string<wchar_t, 85 constexpr_char_traits<wchar_t>, 86 test_allocator<wchar_t>>; 87 const WStr win(L"abcdef"); 88 std::basic_string w(win, (TestSizeT)3); 89 ASSERT_SAME_TYPE(decltype(w), WStr); 90 assert(w == L"def"); 91 } 92 { // Testing (4) w/ allocator 93 const std::string sin("abc"); 94 std::basic_string s(sin, (size_t)1, std::allocator<char>{}); 95 ASSERT_SAME_TYPE(decltype(s), std::string); 96 assert(s == "bc"); 97 98 using WStr = std::basic_string<wchar_t, 99 constexpr_char_traits<wchar_t>, 100 test_allocator<wchar_t>>; 101 const WStr win(L"abcdef"); 102 std::basic_string w(win, (TestSizeT)3, test_allocator<wchar_t>{}); 103 ASSERT_SAME_TYPE(decltype(w), WStr); 104 assert(w == L"def"); 105 } 106 { // Testing (5) w/o allocator 107 const std::string sin("abc"); 108 std::basic_string s(sin, (size_t)1, (size_t)3); 109 ASSERT_SAME_TYPE(decltype(s), std::string); 110 assert(s == "bc"); 111 112 using WStr = std::basic_string<wchar_t, 113 constexpr_char_traits<wchar_t>, 114 test_allocator<wchar_t>>; 115 const WStr win(L"abcdef"); 116 std::basic_string w(win, (TestSizeT)2, (TestSizeT)3); 117 ASSERT_SAME_TYPE(decltype(w), WStr); 118 assert(w == L"cde"); 119 } 120 { // Testing (5) w/ allocator 121 const std::string sin("abc"); 122 std::basic_string s(sin, (size_t)1, (size_t)3, std::allocator<char>{}); 123 ASSERT_SAME_TYPE(decltype(s), std::string); 124 assert(s == "bc"); 125 126 using WStr = std::basic_string<wchar_t, 127 constexpr_char_traits<wchar_t>, 128 test_allocator<wchar_t>>; 129 const WStr win(L"abcdef"); 130 std::basic_string w(win, (TestSizeT)2, (TestSizeT)3, test_allocator<wchar_t>{}); 131 ASSERT_SAME_TYPE(decltype(w), WStr); 132 assert(w == L"cde"); 133 } 134 { // Testing (6) w/o allocator 135 std::basic_string s("abc", (size_t)2); 136 ASSERT_SAME_TYPE(decltype(s), std::string); 137 assert(s == "ab"); 138 139 std::basic_string w(L"abcdef", (size_t)3); 140 ASSERT_SAME_TYPE(decltype(w), std::wstring); 141 assert(w == L"abc"); 142 } 143 { // Testing (6) w/ allocator 144 std::basic_string s("abc", (size_t)2, std::allocator<char>{}); 145 ASSERT_SAME_TYPE(decltype(s), std::string); 146 assert(s == "ab"); 147 148 using WStr = std::basic_string<wchar_t, 149 std::char_traits<wchar_t>, 150 test_allocator<wchar_t>>; 151 std::basic_string w(L"abcdef", (TestSizeT)3, test_allocator<wchar_t>{}); 152 ASSERT_SAME_TYPE(decltype(w), WStr); 153 assert(w == L"abc"); 154 } 155 { // Testing (7) w/o allocator 156 std::basic_string s("abc"); 157 ASSERT_SAME_TYPE(decltype(s), std::string); 158 assert(s == "abc"); 159 160 std::basic_string w(L"abcdef"); 161 ASSERT_SAME_TYPE(decltype(w), std::wstring); 162 assert(w == L"abcdef"); 163 } 164 { // Testing (7) w/ allocator 165 std::basic_string s("abc", std::allocator<char>{}); 166 ASSERT_SAME_TYPE(decltype(s), std::string); 167 assert(s == "abc"); 168 169 using WStr = std::basic_string<wchar_t, 170 std::char_traits<wchar_t>, 171 test_allocator<wchar_t>>; 172 std::basic_string w(L"abcdef", test_allocator<wchar_t>{}); 173 ASSERT_SAME_TYPE(decltype(w), WStr); 174 assert(w == L"abcdef"); 175 } 176 { // (8) w/o allocator 177 using It = cpp17_input_iterator<const char*>; 178 const char* input = "abcdef"; 179 std::basic_string s(It(input), It(input + 3), std::allocator<char>{}); 180 ASSERT_SAME_TYPE(decltype(s), std::string); 181 assert(s == "abc"); 182 } 183 { // (8) w/ allocator 184 using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>; 185 using It = cpp17_input_iterator<const wchar_t*>; 186 const wchar_t* input = L"abcdef"; 187 std::basic_string s(It(input), It(input + 3), test_allocator<wchar_t>{}); 188 ASSERT_SAME_TYPE(decltype(s), ExpectW); 189 assert(s == L"abc"); 190 } 191 { // Testing (9) 192 const std::string sin("abc"); 193 std::basic_string s(sin); 194 ASSERT_SAME_TYPE(decltype(s), std::string); 195 assert(s == "abc"); 196 197 using WStr = std::basic_string<wchar_t, 198 constexpr_char_traits<wchar_t>, 199 test_allocator<wchar_t>>; 200 const WStr win(L"abcdef"); 201 std::basic_string w(win); 202 ASSERT_SAME_TYPE(decltype(w), WStr); 203 assert(w == L"abcdef"); 204 } 205 { // Testing (10) 206 const std::string sin("abc"); 207 std::basic_string s(sin, std::allocator<char>{}); 208 ASSERT_SAME_TYPE(decltype(s), std::string); 209 assert(s == "abc"); 210 211 using WStr = std::basic_string<wchar_t, 212 constexpr_char_traits<wchar_t>, 213 test_allocator<wchar_t>>; 214 const WStr win(L"abcdef"); 215 std::basic_string w(win, test_allocator<wchar_t>{}); 216 ASSERT_SAME_TYPE(decltype(w), WStr); 217 assert(w == L"abcdef"); 218 } 219 { // Testing (11) 220 std::string sin("abc"); 221 std::basic_string s(std::move(sin)); 222 ASSERT_SAME_TYPE(decltype(s), std::string); 223 assert(s == "abc"); 224 225 using WStr = std::basic_string<wchar_t, 226 constexpr_char_traits<wchar_t>, 227 test_allocator<wchar_t>>; 228 WStr win(L"abcdef"); 229 std::basic_string w(std::move(win)); 230 ASSERT_SAME_TYPE(decltype(w), WStr); 231 assert(w == L"abcdef"); 232 } 233 { // Testing (12) 234 std::string sin("abc"); 235 std::basic_string s(std::move(sin), std::allocator<char>{}); 236 ASSERT_SAME_TYPE(decltype(s), std::string); 237 assert(s == "abc"); 238 239 using WStr = std::basic_string<wchar_t, 240 constexpr_char_traits<wchar_t>, 241 test_allocator<wchar_t>>; 242 WStr win(L"abcdef"); 243 std::basic_string w(std::move(win), test_allocator<wchar_t>{}); 244 ASSERT_SAME_TYPE(decltype(w), WStr); 245 assert(w == L"abcdef"); 246 } 247 { // Testing (13) w/o allocator 248 std::basic_string s({'a', 'b', 'c'}); 249 ASSERT_SAME_TYPE(decltype(s), std::string); 250 assert(s == "abc"); 251 252 std::basic_string w({L'a', L'b', L'c'}); 253 ASSERT_SAME_TYPE(decltype(w), std::wstring); 254 assert(w == L"abc"); 255 } 256 { // Testing (13) w/ allocator 257 std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{}); 258 ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>); 259 assert(s == "abc"); 260 261 std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{}); 262 ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>); 263 assert(w == L"abc"); 264 } 265 { // Testing (14) w/o allocator 266 std::string_view sv("abc"); 267 std::basic_string s(sv); 268 ASSERT_SAME_TYPE(decltype(s), std::string); 269 assert(s == "abc"); 270 271 using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>; 272 std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef"); 273 std::basic_string w(BSV); 274 ASSERT_SAME_TYPE(decltype(w), Expect); 275 assert(w == L"abcdef"); 276 } 277 { // Testing (14) w/ allocator 278 using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>; 279 std::string_view sv("abc"); 280 std::basic_string s(sv, test_allocator<char>{}); 281 ASSERT_SAME_TYPE(decltype(s), ExpectS); 282 assert(s == "abc"); 283 284 using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, 285 test_allocator<wchar_t>>; 286 std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef"); 287 std::basic_string w(BSV, test_allocator<wchar_t>{}); 288 ASSERT_SAME_TYPE(decltype(w), ExpectW); 289 assert(w == L"abcdef"); 290 } 291 { // Testing (15) w/o allocator 292 std::string s0("abc"); 293 std::basic_string s(s0, 1, 1); 294 ASSERT_SAME_TYPE(decltype(s), std::string); 295 assert(s == "b"); 296 297 std::wstring w0(L"abcdef"); 298 std::basic_string w(w0, 2, 2); 299 ASSERT_SAME_TYPE(decltype(w), std::wstring); 300 assert(w == L"cd"); 301 } 302 { // Testing (15) w/ allocator 303 using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>; 304 ExpectS s0("abc"); 305 std::basic_string s(s0, 1, 1, test_allocator<char>{4}); 306 ASSERT_SAME_TYPE(decltype(s), ExpectS); 307 assert(s == "b"); 308 309 using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>; 310 ExpectW w0(L"abcdef"); 311 std::basic_string w(w0, 2, 2, test_allocator<wchar_t>{6}); 312 ASSERT_SAME_TYPE(decltype(w), ExpectW); 313 assert(w == L"cd"); 314 } 315 316 return 0; 317 } 318