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