1 //===----------------------------------------------------------------------===// 2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 3 // See https://llvm.org/LICENSE.txt for license information. 4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5 // 6 //===----------------------------------------------------------------------===// 7 8 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 9 // UNSUPPORTED: libcpp-has-no-incomplete-format 10 11 // This version runs the test when the platform has Unicode support. 12 // UNSUPPORTED: libcpp-has-no-unicode 13 14 // TODO FMT Investigate Windows and AIX issues. 15 // UNSUPPORTED: msvc, target={{.+}}-windows-gnu 16 // UNSUPPORTED: LIBCXX-AIX-FIXME 17 18 // TODO FMT Fix this test using GCC, it currently crashes. 19 // UNSUPPORTED: gcc-12 20 21 // TODO FMT This test should not require std::to_chars(floating-point) 22 // This test requires std::to_chars(floating-point), which is in the dylib 23 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0}} 24 25 // <format> 26 27 // This test the debug string type for the formatter specializations for char 28 // and string types. This tests Unicode strings. 29 30 #include <format> 31 32 #include <cassert> 33 #include <concepts> 34 #include <iterator> 35 #include <list> 36 #include <vector> 37 38 #include "test_macros.h" 39 #include "make_string.h" 40 #include "test_format_string.h" 41 #include "assert_macros.h" 42 #include "concat_macros.h" 43 44 #ifndef TEST_HAS_NO_LOCALIZATION 45 # include <iostream> 46 #endif 47 48 #define SV(S) MAKE_STRING_VIEW(CharT, S) 49 50 auto test_format = []<class CharT, class... Args>( 51 std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) { 52 { 53 std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...); 54 TEST_REQUIRE(out == expected, 55 TEST_WRITE_CONCATENATED( 56 "\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n')); 57 } 58 #ifndef TEST_HAS_NO_LOCALIZATION 59 { 60 std::basic_string<CharT> out = std::format(std::locale(), fmt, std::forward<Args>(args)...); 61 assert(out == expected); 62 } 63 #endif // TEST_HAS_NO_LOCALIZATION 64 }; 65 66 auto test_format_to = 67 []<class CharT, class... Args>( 68 std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) { 69 { 70 std::basic_string<CharT> out(expected.size(), CharT(' ')); 71 auto it = std::format_to(out.begin(), fmt, std::forward<Args>(args)...); 72 assert(it == out.end()); 73 assert(out == expected); 74 } 75 #ifndef TEST_HAS_NO_LOCALIZATION 76 { 77 std::basic_string<CharT> out(expected.size(), CharT(' ')); 78 auto it = std::format_to(out.begin(), std::locale(), fmt, std::forward<Args>(args)...); 79 assert(it == out.end()); 80 assert(out == expected); 81 } 82 #endif // TEST_HAS_NO_LOCALIZATION 83 { 84 std::list<CharT> out; 85 std::format_to(std::back_inserter(out), fmt, std::forward<Args>(args)...); 86 assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); 87 } 88 { 89 std::vector<CharT> out; 90 std::format_to(std::back_inserter(out), fmt, std::forward<Args>(args)...); 91 assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); 92 } 93 { 94 assert(expected.size() < 4096 && "Update the size of the buffer."); 95 CharT out[4096]; 96 CharT* it = std::format_to(out, fmt, std::forward<Args>(args)...); 97 assert(std::distance(out, it) == int(expected.size())); 98 // Convert to std::string since output contains '\0' for boolean tests. 99 assert(std::basic_string<CharT>(out, it) == expected); 100 } 101 }; 102 103 auto test_formatted_size = 104 []<class CharT, class... Args>( 105 std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) { 106 { 107 std::size_t size = std::formatted_size(fmt, std::forward<Args>(args)...); 108 assert(size == expected.size()); 109 } 110 #ifndef TEST_HAS_NO_LOCALIZATION 111 { 112 std::size_t size = std::formatted_size(std::locale(), fmt, std::forward<Args>(args)...); 113 assert(size == expected.size()); 114 } 115 #endif // TEST_HAS_NO_LOCALIZATION 116 }; 117 118 auto test_format_to_n = 119 []<class CharT, class... Args>( 120 std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) { 121 { 122 std::size_t n = expected.size(); 123 std::basic_string<CharT> out(n, CharT(' ')); 124 std::format_to_n_result result = std::format_to_n(out.begin(), n, fmt, std::forward<Args>(args)...); 125 assert(result.size == static_cast<std::ptrdiff_t>(expected.size())); 126 assert(result.out == out.end()); 127 assert(out == expected); 128 } 129 #ifndef TEST_HAS_NO_LOCALIZATION 130 { 131 std::size_t n = expected.size(); 132 std::basic_string<CharT> out(n, CharT(' ')); 133 std::format_to_n_result result = 134 std::format_to_n(out.begin(), n, std::locale(), fmt, std::forward<Args>(args)...); 135 assert(result.size == static_cast<std::ptrdiff_t>(expected.size())); 136 assert(result.out == out.end()); 137 assert(out == expected); 138 } 139 #endif // TEST_HAS_NO_LOCALIZATION 140 { 141 std::ptrdiff_t n = 0; 142 std::basic_string<CharT> out; 143 std::format_to_n_result result = std::format_to_n(out.begin(), n, fmt, std::forward<Args>(args)...); 144 assert(result.size == static_cast<std::ptrdiff_t>(expected.size())); 145 assert(result.out == out.end()); 146 assert(out.empty()); 147 } 148 { 149 std::ptrdiff_t n = expected.size() / 2; 150 std::basic_string<CharT> out(n, CharT(' ')); 151 std::format_to_n_result result = std::format_to_n(out.begin(), n, fmt, std::forward<Args>(args)...); 152 assert(result.size == static_cast<std::ptrdiff_t>(expected.size())); 153 assert(result.out == out.end()); 154 assert(out == expected.substr(0, n)); 155 } 156 }; 157 158 template <class CharT> 159 void test_char() { 160 // *** P2286 examples *** 161 test_format(SV("['\\'', '\"']"), SV("[{:?}, {:?}]"), CharT('\''), CharT('"')); 162 163 // *** Specical cases *** 164 test_format(SV("'\\t'"), SV("{:?}"), CharT('\t')); 165 test_format(SV("'\\n'"), SV("{:?}"), CharT('\n')); 166 test_format(SV("'\\r'"), SV("{:?}"), CharT('\r')); 167 test_format(SV("'\\\\'"), SV("{:?}"), CharT('\\')); 168 169 test_format(SV("'\\\''"), SV("{:?}"), CharT('\'')); 170 test_format(SV("'\"'"), SV("{:?}"), CharT('"')); // only special for string 171 172 test_format(SV("' '"), SV("{:?}"), CharT(' ')); 173 174 // *** Printable *** 175 test_format(SV("'a'"), SV("{:?}"), CharT('a')); 176 test_format(SV("'b'"), SV("{:?}"), CharT('b')); 177 test_format(SV("'c'"), SV("{:?}"), CharT('c')); 178 179 // *** Non-printable *** 180 181 // Control 182 test_format(SV("'\\u{0}'"), SV("{:?}"), CharT('\0')); 183 test_format(SV("'\\u{1f}'"), SV("{:?}"), CharT('\x1f')); 184 185 // Ill-formed 186 if constexpr (sizeof(CharT) == 1) 187 test_format(SV("'\\x{80}'"), SV("{:?}"), CharT('\x80')); 188 189 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 190 if constexpr (sizeof(CharT) > 1) { 191 using V = std::basic_string_view<CharT>; 192 193 // Unicode fitting in a 16-bit wchar_t 194 195 // *** Non-printable *** 196 197 // Space_Separator 198 test_format(V{L"'\\u{a0}'"}, L"{:?}", L'\xa0'); // NO-BREAK SPACE 199 test_format(V{L"'\\u{3000}'"}, L"{:?}", L'\x3000'); // IDEOGRAPHIC SPACE 200 201 // Line_Separator 202 test_format(V{L"'\\u{2028}'"}, L"{:?}", L'\x2028'); // LINE SEPARATOR 203 204 // Paragraph_Separator 205 test_format(V{L"'\\u{2029}'"}, L"{:?}", L'\x2029'); // PARAGRAPH SEPARATOR 206 207 // Format 208 test_format(V{L"'\\u{ad}'"}, L"{:?}", L'\xad'); // SOFT HYPHEN 209 test_format(V{L"'\\u{600}'"}, L"{:?}", L'\x600'); // ARABIC NUMBER SIGN 210 test_format(V{L"'\\u{feff}'"}, L"{:?}", L'\xfeff'); // ZERO WIDTH NO-BREAK SPACE 211 212 // Incomplete surrogate pair in UTF-16 213 test_format(V{L"'\\x{d800}'"}, L"{:?}", L'\xd800'); // <surrogate-D800> 214 test_format(V{L"'\\x{dfff}'"}, L"{:?}", L'\xdfff'); // <surrogate-DFFF> 215 216 // Private_Use 217 test_format(V{L"'\\u{e000}'"}, L"{:?}", L'\xe000'); // <private-use-E000> 218 test_format(V{L"'\\u{f8ff}'"}, L"{:?}", L'\xf8ff'); // <private-use-F8FF> 219 220 // Unassigned 221 test_format(V{L"'\\u{378}'"}, L"{:?}", L'\x378'); // <reserved-0378> 222 test_format(V{L"'\\u{1774}'"}, L"{:?}", L'\x1774'); // <reserved-1774> 223 test_format(V{L"'\\u{ffff}'"}, L"{:?}", L'\xffff'); // <noncharacter-FFFF> 224 225 // Grapheme Extended 226 test_format(V{L"'\\u{300}'"}, L"{:?}", L'\x300'); // COMBINING GRAVE ACCENT 227 test_format(V{L"'\\u{fe20}'"}, L"{:?}", L'\xfe20'); // VARIATION SELECTOR-1 228 } 229 # ifndef TEST_SHORT_WCHAR 230 if constexpr (sizeof(CharT) > 2) { 231 static_assert(sizeof(CharT) == 4, "add support for unexpected size"); 232 // Unicode fitting in a 32-bit wchar_t 233 234 constexpr wchar_t x = 0x1ffff; 235 constexpr std::uint32_t y = 0x1ffff; 236 static_assert(x == y); 237 238 using V = std::basic_string_view<CharT>; 239 240 // *** Non-printable *** 241 // Format 242 test_format(V{L"'\\u{110bd}'"}, L"{:?}", L'\x110bd'); // KAITHI NUMBER SIGN 243 test_format(V{L"'\\u{e007f}'"}, L"{:?}", L'\xe007f'); // CANCEL TAG 244 245 // Private_Use 246 test_format(V{L"'\\u{f0000}'"}, L"{:?}", L'\xf0000'); // <private-use-F0000> 247 test_format(V{L"'\\u{ffffd}'"}, L"{:?}", L'\xffffd'); // <private-use-FFFFD> 248 249 test_format(V{L"'\\u{100000}'"}, L"{:?}", L'\x100000'); // <private-use-100000> 250 test_format(V{L"'\\u{10fffd}'"}, L"{:?}", L'\x10fffd'); // <private-use-10FFFD> 251 252 // Unassigned 253 test_format(V{L"'\\u{1000c}'"}, L"{:?}", L'\x1000c'); // <reserved-1000c> 254 test_format(V{L"'\\u{fffff}'"}, L"{:?}", L'\xfffff'); // <noncharacter-FFFFF> 255 test_format(V{L"'\\u{10fffe}'"}, L"{:?}", L'\x10fffe'); // <noncharacter-10FFFE> 256 257 // Grapheme Extended 258 test_format(V{L"'\\u{101fd}'"}, L"{:?}", L'\x101fd'); // COMBINING OLD PERMIC LETTER AN 259 test_format(V{L"'\\u{e0100}'"}, L"{:?}", L'\xe0100'); // VARIATION SELECTOR-17 260 261 // Ill-formed 262 test_format(V{L"'\\x{110000}'"}, L"{:?}", L'\x110000'); 263 test_format(V{L"'\\x{ffffffff}'"}, L"{:?}", L'\xffffffff'); 264 } 265 # endif // TEST_SHORT_WCHAR 266 #endif // TEST_HAS_NO_WIDE_CHARACTERS 267 } 268 269 template <class CharT> 270 void test_string() { 271 // *** P2286 examples *** 272 test_format(SV("[h\tllo]"), SV("[{}]"), SV("h\tllo")); 273 test_format(SV(R"(["h\tllo"])"), SV("[{:?}]"), SV("h\tllo")); 274 test_format(SV(R"(["Спасибо, Виктор ♥!"])"), SV("[{:?}]"), SV("Спасибо, Виктор ♥!")); 275 276 test_format(SV(R"(["\u{0} \n \t \u{2} \u{1b}"])"), SV("[{:?}]"), SV("\0 \n \t \x02 \x1b")); 277 278 if constexpr (sizeof(CharT) == 1) { 279 // Ill-formend UTF-8 280 test_format(SV(R"(["\x{c3}"])"), SV("[{:?}]"), "\xc3"); 281 test_format(SV(R"(["\x{c3}("])"), SV("[{:?}]"), "\xc3\x28"); 282 283 /* U+0000..U+0007F 1 code unit range, encoded in 2 code units. */ 284 test_format(SV(R"(["\x{c0}\x{80}"])"), SV("[{:?}]"), "\xc0\x80"); // U+0000 285 test_format(SV(R"(["\x{c1}\x{bf}"])"), SV("[{:?}]"), "\xc1\xbf"); // U+007F 286 test_format(SV(R"(["\u{80}"])"), SV("[{:?}]"), "\xc2\x80"); // U+0080 first valid (General_Category=Control) 287 288 /* U+0000..U+07FFF 1 and 2 code unit range, encoded in 3 code units. */ 289 test_format(SV(R"(["\x{e0}\x{80}\x{80}"])"), SV("[{:?}]"), "\xe0\x80\x80"); // U+0000 290 test_format(SV(R"(["\x{e0}\x{81}\x{bf}"])"), SV("[{:?}]"), "\xe0\x81\xbf"); // U+007F 291 test_format(SV(R"(["\x{e0}\x{82}\x{80}"])"), SV("[{:?}]"), "\xe0\x82\x80"); // U+0080 292 test_format(SV(R"(["\x{e0}\x{9f}\x{bf}"])"), SV("[{:?}]"), "\xe0\x9f\xbf"); // U+07FF 293 test_format(SV("[\"\u0800\"]"), SV("[{:?}]"), "\xe0\xa0\x80"); // U+0800 first valid 294 295 #if 0 296 // This code point is in the Hangul Jamo Extended-B block and at the time of writing 297 // it's unassigned. When it comes defined, this branch might become true. 298 test_format(SV("[\"\ud7ff\"]"), SV("[{:?}]"), "\xed\x9f\xbf"); // U+D7FF last valid 299 #else 300 /* U+D800..D+DFFFF surrogate range */ 301 test_format(SV(R"(["\u{d7ff}"])"), SV("[{:?}]"), "\xed\x9f\xbf"); // U+D7FF last valid 302 #endif 303 test_format(SV(R"(["\x{ed}\x{a0}\x{80}"])"), SV("[{:?}]"), "\xed\xa0\x80"); // U+D800 304 test_format(SV(R"(["\x{ed}\x{af}\x{bf}"])"), SV("[{:?}]"), "\xed\xaf\xbf"); // U+DBFF 305 test_format(SV(R"(["\x{ed}\x{bf}\x{80}"])"), SV("[{:?}]"), "\xed\xbf\x80"); // U+DC00 306 test_format(SV(R"(["\x{ed}\x{bf}\x{bf}"])"), SV("[{:?}]"), "\xed\xbf\xbf"); // U+DFFF 307 test_format(SV(R"(["\u{e000}"])"), SV("[{:?}]"), "\xee\x80\x80"); // U+E000 first valid 308 // (in the Private Use Area block) 309 310 /* U+0000..U+FFFF 1, 2, and 3 code unit range */ 311 test_format(SV(R"(["\x{f0}\x{80}\x{80}\x{80}"])"), SV("[{:?}]"), "\xf0\x80\x80\x80"); // U+0000 312 test_format(SV(R"(["\x{f0}\x{80}\x{81}\x{bf}"])"), SV("[{:?}]"), "\xf0\x80\x81\xbf"); // U+007F 313 test_format(SV(R"(["\x{f0}\x{80}\x{82}\x{80}"])"), SV("[{:?}]"), "\xf0\x80\x82\x80"); // U+0080 314 test_format(SV(R"(["\x{f0}\x{80}\x{9f}\x{bf}"])"), SV("[{:?}]"), "\xf0\x80\x9f\xbf"); // U+07FF 315 test_format(SV(R"(["\x{f0}\x{80}\x{a0}\x{80}"])"), SV("[{:?}]"), "\xf0\x80\xa0\x80"); // U+0800 316 test_format(SV(R"(["\x{f0}\x{8f}\x{bf}\x{bf}"])"), SV("[{:?}]"), "\xf0\x8f\xbf\xbf"); // U+FFFF 317 test_format(SV("[\"\U00010000\"]"), SV("[{:?}]"), "\xf0\x90\x80\x80"); // U+10000 first valid 318 319 /* U+10FFFF..U+1FFFFF invalid range */ 320 test_format(SV(R"(["\u{10ffff}"])"), SV("[{:?}]"), "\xf4\x8f\xbf\xbf"); // U+10FFFF last valid 321 // (in Supplementary Private Use Area-B) 322 test_format(SV(R"(["\x{f4}\x{90}\x{80}\x{80}"])"), SV("[{:?}]"), "\xf4\x90\x80\x80"); // U+110000 323 test_format(SV(R"(["\x{f4}\x{bf}\x{bf}\x{bf}"])"), SV("[{:?}]"), "\xf4\xbf\xbf\xbf"); // U+11FFFF 324 } else { 325 // Valid UTF-16 and UTF-32 326 test_format(SV("[\"\u00c3\"]"), SV("[{:?}]"), L"\xc3"); // LATIN CAPITAL LETTER A WITH TILDE 327 test_format(SV("[\"\u00c3(\"]"), SV("[{:?}]"), L"\xc3\x28"); 328 } 329 330 test_format(SV(R"(["\u{200d}♂\u{fe0f}"])"), SV("[{:?}]"), SV("♂️")); 331 332 // *** Specical cases *** 333 test_format(SV(R"("\t\n\r\\'\" ")"), SV("{:?}"), SV("\t\n\r\\'\" ")); 334 335 // *** Printable *** 336 test_format(SV(R"("abcdefg")"), SV("{:?}"), SV("abcdefg")); 337 338 // *** Non-printable *** 339 340 // Control 341 test_format(SV(R"("\u{0}\u{1f}")"), SV("{:?}"), SV("\0\x1f")); 342 343 // Ill-formed 344 if constexpr (sizeof(CharT) == 1) 345 test_format(SV(R"("\x{80}")"), SV("{:?}"), SV("\x80")); 346 347 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 348 if constexpr (sizeof(CharT) > 1) { 349 using V = std::basic_string_view<CharT>; 350 351 // Unicode fitting in a 16-bit wchar_t 352 353 // *** Non-printable *** 354 355 // Space_Separator 356 test_format(V{LR"("\u{a0}\u{3000}")"}, L"{:?}", L"\xa0\x3000"); 357 358 // Line_Separator 359 test_format(V{LR"("\u{2028}")"}, L"{:?}", L"\x2028"); // LINE SEPARATOR 360 361 // Paragraph_Separator 362 test_format(V{LR"("\u{2029}")"}, L"{:?}", L"\x2029"); // PARAGRAPH SEPARATOR 363 364 // Format 365 test_format(V{LR"("\u{ad}\u{600}\u{feff}")"}, L"{:?}", L"\xad\x600\xfeff"); 366 367 // Incomplete surrogate pair in UTF-16 368 test_format(V{LR"("\x{d800}")"}, L"{:?}", L"\xd800"); 369 370 // Private_Use 371 test_format(V{LR"("\u{e000}\u{f8ff}")"}, L"{:?}", L"\xe000\xf8ff"); 372 373 // Unassigned 374 test_format(V{LR"("\u{378}\u{1774}\u{ffff}")"}, L"{:?}", L"\x378\x1774\xffff"); 375 376 // Grapheme Extended 377 test_format(V{LR"("\u{300}\u{fe20}")"}, L"{:?}", L"\x300\xfe20"); 378 } 379 # ifndef TEST_SHORT_WCHAR 380 if constexpr (sizeof(CharT) > 2) { 381 static_assert(sizeof(CharT) == 4, "add support for unexpected size"); 382 // Unicode fitting in a 32-bit wchar_t 383 384 constexpr wchar_t x = 0x1ffff; 385 constexpr std::uint32_t y = 0x1ffff; 386 static_assert(x == y); 387 388 using V = std::basic_string_view<CharT>; 389 390 // *** Non-printable *** 391 // Format 392 test_format(V{LR"("\u{110bd}\u{e007f}")"}, L"{:?}", L"\x110bd\xe007f"); 393 394 // Private_Use 395 test_format(V{LR"("\u{f0000}\u{ffffd}\u{100000}\u{10fffd}")"}, L"{:?}", L"\xf0000\xffffd\x100000\x10fffd"); 396 397 // Unassigned 398 test_format(V{LR"("\u{1000c}\u{fffff}\u{10fffe}")"}, L"{:?}", L"\x1000c\xfffff\x10fffe"); 399 400 // Grapheme Extended 401 test_format(V{LR"("\u{101fd}\u{e0100}")"}, L"{:?}", L"\x101fd\xe0100"); 402 403 // Ill-formed 404 test_format(V{LR"("\x{110000}\x{ffffffff}")"}, L"{:?}", L"\x110000\xffffffff"); 405 } 406 # endif // TEST_SHORT_WCHAR 407 #endif // TEST_HAS_NO_WIDE_CHARACTERS 408 } 409 410 template <class CharT, class TestFunction> 411 void test_format_functions(TestFunction check) { 412 // *** align-fill & width *** 413 check(SV(R"(***"hellö")"), SV("{:*>10?}"), SV("hellö")); // ö is LATIN SMALL LETTER O WITH DIAERESIS 414 check(SV(R"(*"hellö"**)"), SV("{:*^10?}"), SV("hellö")); 415 check(SV(R"("hellö"***)"), SV("{:*<10?}"), SV("hellö")); 416 417 check(SV(R"("hello\u{308}")"), SV("{:*>10?}"), SV("hello\u0308")); 418 check(SV(R"(***"hello\u{308}")"), SV("{:*>17?}"), SV("hello\u0308")); 419 check(SV(R"(*"hello\u{308}"**)"), SV("{:*^17?}"), SV("hello\u0308")); 420 check(SV(R"("hello\u{308}"***)"), SV("{:*<17?}"), SV("hello\u0308")); 421 422 check(SV(R"("hello \u{200d}♂\u{fe0f}")"), SV("{:*>10?}"), SV("hello ♂️")); 423 check(SV(R"(***"hello \u{200d}♂\u{fe0f}")"), SV("{:*>30?}"), SV("hello ♂️")); 424 check(SV(R"(*"hello \u{200d}♂\u{fe0f}"**)"), SV("{:*^30?}"), SV("hello ♂️")); 425 check(SV(R"("hello \u{200d}♂\u{fe0f}"***)"), SV("{:*<30?}"), SV("hello ♂️")); 426 427 // *** width *** 428 check(SV(R"("hellö" )"), SV("{:10?}"), SV("hellö")); 429 check(SV(R"("hello\u{308}" )"), SV("{:17?}"), SV("hello\u0308")); 430 check(SV(R"("hello \u{200d}♂\u{fe0f}" )"), SV("{:30?}"), SV("hello ♂️")); 431 432 // *** precision *** 433 check(SV(R"("hell)"), SV("{:.5?}"), SV("hellö")); 434 check(SV(R"("hellö)"), SV("{:.6?}"), SV("hellö")); 435 check(SV(R"("hellö")"), SV("{:.7?}"), SV("hellö")); 436 437 check(SV(R"("hello )"), SV("{:.7?}"), SV("hello ♂️")); 438 check(SV(R"("hello )"), SV("{:.8?}"), SV("hello ♂️")); // shrug is two columns 439 check(SV(R"("hello )"), SV("{:.9?}"), SV("hello ♂️")); 440 check(SV(R"("hello \)"), SV("{:.10?}"), SV("hello ♂️")); 441 check(SV(R"("hello \u{200d})"), SV("{:.17?}"), SV("hello ♂️")); 442 check(SV(R"("hello \u{200d}♂)"), SV("{:.18?}"), SV("hello ♂️")); 443 check(SV(R"("hello \u{200d}♂\)"), SV("{:.19?}"), SV("hello ♂️")); 444 check(SV(R"("hello \u{200d}♂\u{fe0f}")"), SV("{:.28?}"), SV("hello ♂️")); 445 446 // *** width & precision *** 447 check(SV(R"("hell#########################)"), SV("{:#<30.5?}"), SV("hellö")); 448 check(SV(R"("hellö########################)"), SV("{:#<30.6?}"), SV("hellö")); 449 check(SV(R"("hellö"#######################)"), SV("{:#<30.7?}"), SV("hellö")); 450 451 check(SV(R"("hello #######################)"), SV("{:#<30.7?}"), SV("hello ♂️")); 452 check(SV(R"("hello #######################)"), SV("{:#<30.8?}"), SV("hello ♂️")); 453 check(SV(R"("hello #####################)"), SV("{:#<30.9?}"), SV("hello ♂️")); 454 check(SV(R"("hello \####################)"), SV("{:#<30.10?}"), SV("hello ♂️")); 455 check(SV(R"("hello \u{200d}#############)"), SV("{:#<30.17?}"), SV("hello ♂️")); 456 check(SV(R"("hello \u{200d}♂############)"), SV("{:#<30.18?}"), SV("hello ♂️")); 457 check(SV(R"("hello \u{200d}♂\###########)"), SV("{:#<30.19?}"), SV("hello ♂️")); 458 check(SV(R"("hello \u{200d}♂\u{fe0f}"###)"), SV("{:#<30.28?}"), SV("hello ♂️")); 459 } 460 461 template <class CharT> 462 void test() { 463 test_char<CharT>(); 464 test_string<CharT>(); 465 466 test_format_functions<CharT>(test_format); 467 test_format_functions<CharT>(test_format_to); 468 test_format_functions<CharT>(test_formatted_size); 469 test_format_functions<CharT>(test_format_to_n); 470 } 471 472 static void test_ill_formed_utf8() { 473 using namespace std::literals; 474 475 // Too few code units 476 test_format(R"("\x{df}")"sv, "{:?}", "\xdf"); 477 test_format(R"("\x{ef}")"sv, "{:?}", "\xef"); 478 test_format(R"("\x{ef}\x{bf}")"sv, "{:?}", "\xef\xbf"); 479 test_format(R"("\x{f7}")"sv, "{:?}", "\xf7"); 480 test_format(R"("\x{f7}\x{bf}")"sv, "{:?}", "\xf7\xbf"); 481 test_format(R"("\x{f7}\x{bf}\x{bf}")"sv, "{:?}", "\xf7\xbf\xbf"); 482 483 // Invalid continuation byte 484 test_format(R"("\x{df}a")"sv, 485 "{:?}", 486 "\xdf" 487 "a"); 488 test_format(R"("\x{ef}a")"sv, 489 "{:?}", 490 "\xef" 491 "a"); 492 test_format(R"("\x{ef}\x{bf}a")"sv, 493 "{:?}", 494 "\xef\xbf" 495 "a"); 496 test_format(R"("\x{f7}a")"sv, 497 "{:?}", 498 "\xf7" 499 "a"); 500 test_format(R"("\x{f7}\x{bf}a")"sv, 501 "{:?}", 502 "\xf7\xbf" 503 "a"); 504 test_format(R"("\x{f7}\x{bf}\x{bf}a")"sv, 505 "{:?}", 506 "\xf7\xbf\xbf" 507 "a"); 508 509 // Code unit out of range 510 test_format(R"("\u{10ffff}")"sv, "{:?}", "\xf4\x8f\xbf\xbf"); // last valid code point 511 test_format(R"("\x{f4}\x{90}\x{80}\x{80}")"sv, "{:?}", "\xf4\x90\x80\x80"); // first invalid code point 512 test_format(R"("\x{f5}\x{b1}\x{b2}\x{b3}")"sv, "{:?}", "\xf5\xb1\xb2\xb3"); 513 test_format(R"("\x{f7}\x{bf}\x{bf}\x{bf}")"sv, "{:?}", "\xf7\xbf\xbf\xbf"); // largest encoded code point 514 } 515 516 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 517 # ifdef _LIBCPP_SHORT_WCHAR 518 static void test_ill_formed_utf16() { 519 using namespace std::literals; 520 521 // Too few code units 522 test_format(LR"("\x{d800}")"sv, L"{:?}", L"\xd800"); 523 test_format(LR"("\x{dbff}")"sv, L"{:?}", L"\xdbff"); 524 525 // Start with low surrogate pair 526 test_format(LR"("\x{dc00}a")"sv, 527 L"{:?}", 528 L"\xdc00" 529 "a"); 530 test_format(LR"("\x{dfff}a")"sv, 531 L"{:?}", 532 L"\xdfff" 533 "a"); 534 535 // Only high surrogate pair 536 test_format(LR"("\x{d800}a")"sv, 537 L"{:?}", 538 L"\xd800" 539 "a"); 540 test_format(LR"("\x{dbff}a")"sv, 541 L"{:?}", 542 L"\xdbff" 543 "a"); 544 } 545 # else // _LIBCPP_SHORT_WCHAR 546 static void test_ill_formed_utf32() { 547 using namespace std::literals; 548 549 test_format(LR"("\u{10ffff}")"sv, L"{:?}", L"\x10ffff"); // last valid code point 550 test_format(LR"("\x{110000}")"sv, L"{:?}", L"\x110000"); // first invalid code point 551 test_format(LR"("\x{ffffffff}")"sv, L"{:?}", L"\xffffffff"); // largest encoded code point 552 } 553 554 # endif // _LIBCPP_SHORT_WCHAR 555 #endif // TEST_HAS_NO_WIDE_CHARACTERS 556 557 int main(int, char**) { 558 test<char>(); 559 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 560 test<wchar_t>(); 561 #endif 562 563 test_ill_formed_utf8(); 564 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 565 # ifdef _LIBCPP_SHORT_WCHAR 566 test_ill_formed_utf16(); 567 assert(false); 568 # else // _LIBCPP_SHORT_WCHAR 569 test_ill_formed_utf32(); 570 # endif // _LIBCPP_SHORT_WCHAR 571 #endif // TEST_HAS_NO_WIDE_CHARACTERS 572 573 return 0; 574 } 575