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