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()) - PARTIALLY BROKEN 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()) - BROKEN 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 #if 0 // FIXME: This doesn't work 110 const std::string sin("abc"); 111 std::basic_string s(sin, (size_t)1, (size_t)3); 112 ASSERT_SAME_TYPE(decltype(s), std::string); 113 assert(s == "bc"); 114 115 using WStr = std::basic_string<wchar_t, 116 constexpr_char_traits<wchar_t>, 117 test_allocator<wchar_t>>; 118 const WStr win(L"abcdef"); 119 std::basic_string w(win, (TestSizeT)2, (TestSizeT)3); 120 ASSERT_SAME_TYPE(decltype(w), WStr); 121 assert(w == L"cde"); 122 #endif 123 } 124 { // Testing (5) w/ allocator 125 const std::string sin("abc"); 126 std::basic_string s(sin, (size_t)1, (size_t)3, std::allocator<char>{}); 127 ASSERT_SAME_TYPE(decltype(s), std::string); 128 assert(s == "bc"); 129 130 using WStr = std::basic_string<wchar_t, 131 constexpr_char_traits<wchar_t>, 132 test_allocator<wchar_t>>; 133 const WStr win(L"abcdef"); 134 std::basic_string w(win, (TestSizeT)2, (TestSizeT)3, test_allocator<wchar_t>{}); 135 ASSERT_SAME_TYPE(decltype(w), WStr); 136 assert(w == L"cde"); 137 } 138 { // Testing (6) w/o allocator 139 std::basic_string s("abc", (size_t)2); 140 ASSERT_SAME_TYPE(decltype(s), std::string); 141 assert(s == "ab"); 142 143 std::basic_string w(L"abcdef", (size_t)3); 144 ASSERT_SAME_TYPE(decltype(w), std::wstring); 145 assert(w == L"abc"); 146 } 147 { // Testing (6) w/ allocator 148 std::basic_string s("abc", (size_t)2, std::allocator<char>{}); 149 ASSERT_SAME_TYPE(decltype(s), std::string); 150 assert(s == "ab"); 151 152 using WStr = std::basic_string<wchar_t, 153 std::char_traits<wchar_t>, 154 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 } 159 { // Testing (7) w/o allocator 160 std::basic_string s("abc"); 161 ASSERT_SAME_TYPE(decltype(s), std::string); 162 assert(s == "abc"); 163 164 std::basic_string w(L"abcdef"); 165 ASSERT_SAME_TYPE(decltype(w), std::wstring); 166 assert(w == L"abcdef"); 167 } 168 { // Testing (7) w/ allocator 169 std::basic_string s("abc", std::allocator<char>{}); 170 ASSERT_SAME_TYPE(decltype(s), std::string); 171 assert(s == "abc"); 172 173 using WStr = std::basic_string<wchar_t, 174 std::char_traits<wchar_t>, 175 test_allocator<wchar_t>>; 176 std::basic_string w(L"abcdef", test_allocator<wchar_t>{}); 177 ASSERT_SAME_TYPE(decltype(w), WStr); 178 assert(w == L"abcdef"); 179 } 180 { // (8) w/o allocator 181 // This overload isn't compatible with implicit deduction guides as 182 // specified in the standard. 183 // FIXME: Propose adding an explicit guide to the standard? 184 } 185 { // (8) w/ allocator 186 // This overload isn't compatible with implicit deduction guides as 187 // specified in the standard. 188 // FIXME: Propose adding an explicit guide to the standard? 189 #if 0 190 using It = input_iterator<const char*>; 191 const char* input = "abcdef"; 192 std::basic_string s(It(input), It(input + 3), std::allocator<char>{}); 193 ASSERT_SAME_TYPE(decltype(s), std::string); 194 #endif 195 } 196 { // Testing (9) 197 const std::string sin("abc"); 198 std::basic_string s(sin); 199 ASSERT_SAME_TYPE(decltype(s), std::string); 200 assert(s == "abc"); 201 202 using WStr = std::basic_string<wchar_t, 203 constexpr_char_traits<wchar_t>, 204 test_allocator<wchar_t>>; 205 const WStr win(L"abcdef"); 206 std::basic_string w(win); 207 ASSERT_SAME_TYPE(decltype(w), WStr); 208 assert(w == L"abcdef"); 209 } 210 { // Testing (10) 211 const std::string sin("abc"); 212 std::basic_string s(sin, std::allocator<char>{}); 213 ASSERT_SAME_TYPE(decltype(s), std::string); 214 assert(s == "abc"); 215 216 using WStr = std::basic_string<wchar_t, 217 constexpr_char_traits<wchar_t>, 218 test_allocator<wchar_t>>; 219 const WStr win(L"abcdef"); 220 std::basic_string w(win, test_allocator<wchar_t>{}); 221 ASSERT_SAME_TYPE(decltype(w), WStr); 222 assert(w == L"abcdef"); 223 } 224 { // Testing (11) 225 std::string sin("abc"); 226 std::basic_string s(std::move(sin)); 227 ASSERT_SAME_TYPE(decltype(s), std::string); 228 assert(s == "abc"); 229 230 using WStr = std::basic_string<wchar_t, 231 constexpr_char_traits<wchar_t>, 232 test_allocator<wchar_t>>; 233 WStr win(L"abcdef"); 234 std::basic_string w(std::move(win)); 235 ASSERT_SAME_TYPE(decltype(w), WStr); 236 assert(w == L"abcdef"); 237 } 238 { // Testing (12) 239 std::string sin("abc"); 240 std::basic_string s(std::move(sin), std::allocator<char>{}); 241 ASSERT_SAME_TYPE(decltype(s), std::string); 242 assert(s == "abc"); 243 244 using WStr = std::basic_string<wchar_t, 245 constexpr_char_traits<wchar_t>, 246 test_allocator<wchar_t>>; 247 WStr win(L"abcdef"); 248 std::basic_string w(std::move(win), test_allocator<wchar_t>{}); 249 ASSERT_SAME_TYPE(decltype(w), WStr); 250 assert(w == L"abcdef"); 251 } 252 { // Testing (13) w/o allocator 253 std::basic_string s({'a', 'b', 'c'}); 254 ASSERT_SAME_TYPE(decltype(s), std::string); 255 assert(s == "abc"); 256 257 std::basic_string w({L'a', L'b', L'c'}); 258 ASSERT_SAME_TYPE(decltype(w), std::wstring); 259 assert(w == L"abc"); 260 } 261 { // Testing (13) w/ allocator 262 std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{}); 263 ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>); 264 assert(s == "abc"); 265 266 std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{}); 267 ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>); 268 assert(w == L"abc"); 269 } 270 { // Testing (14) w/o allocator 271 std::string_view sv("abc"); 272 std::basic_string s(sv); 273 ASSERT_SAME_TYPE(decltype(s), std::string); 274 assert(s == "abc"); 275 276 using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>; 277 std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef"); 278 std::basic_string w(BSV); 279 ASSERT_SAME_TYPE(decltype(w), Expect); 280 assert(w == L"abcdef"); 281 } 282 { // Testing (14) w/ allocator 283 using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>; 284 std::string_view sv("abc"); 285 std::basic_string s(sv, test_allocator<char>{}); 286 ASSERT_SAME_TYPE(decltype(s), ExpectS); 287 assert(s == "abc"); 288 289 using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, 290 test_allocator<wchar_t>>; 291 std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef"); 292 std::basic_string w(BSV, test_allocator<wchar_t>{}); 293 ASSERT_SAME_TYPE(decltype(w), ExpectW); 294 assert(w == L"abcdef"); 295 } 296 { // Testing (15) 297 // This overload isn't compatible with implicit deduction guides as 298 // specified in the standard. 299 } 300 } 301