| 1 | // { dg-options "-fexec-charset=UTF-8" } |
| 2 | // { dg-do run { target c++20 } } |
| 3 | // { dg-add-options no_pch } |
| 4 | |
| 5 | #include <format> |
| 6 | |
| 7 | #ifndef __cpp_lib_format |
| 8 | # error "Feature test macro for std::format is missing in <format>" |
| 9 | #elif __cpp_lib_format < 202110L |
| 10 | # error "Feature test macro for std::format has wrong value in <format>" |
| 11 | #elif __cplusplus > 202302L && __cpp_lib_format < 202311L |
| 12 | # error "Feature test macro for std::format has wrong value in <format>" |
| 13 | #endif |
| 14 | |
| 15 | #ifndef __cpp_lib_format_uchar |
| 16 | # error "Feature test macro for formatting chars as integers is missing in <format>" |
| 17 | #elif __cpp_lib_format_uchar < 202311L |
| 18 | # error "Feature test macro for formatting chars as integers has wrong value in <format>" |
| 19 | #endif |
| 20 | |
| 21 | #undef __cpp_lib_format |
| 22 | #include <version> |
| 23 | #ifndef __cpp_lib_format |
| 24 | # error "Feature test macro for std::format is missing in <version>" |
| 25 | #elif __cpp_lib_format < 202110L |
| 26 | # error "Feature test macro for std::format has wrong value in <version>" |
| 27 | #elif __cplusplus > 202302L && __cpp_lib_format < 202311L |
| 28 | # error "Feature test macro for std::format has wrong value in <version>" |
| 29 | #endif |
| 30 | |
| 31 | #ifndef __cpp_lib_format_uchar |
| 32 | # error "Feature test macro for formatting chars as integers is missing in <version>" |
| 33 | #elif __cpp_lib_format_uchar < 202311L |
| 34 | # error "Feature test macro for formatting chars as integers has wrong value in <version>" |
| 35 | #endif |
| 36 | |
| 37 | #include <string> |
| 38 | #include <limits> |
| 39 | #include <cstdint> |
| 40 | #include <cstdio> |
| 41 | #include <testsuite_hooks.h> |
| 42 | |
| 43 | void |
| 44 | test_no_args() |
| 45 | { |
| 46 | std::string s; |
| 47 | s = std::format("disco"); |
| 48 | VERIFY( s == "disco" ); |
| 49 | |
| 50 | s = std::format("}} machine {{ funk }} specialists {{"); |
| 51 | VERIFY( s == "} machine { funk } specialists {" ); |
| 52 | |
| 53 | s = std::format("128bpm }}"); |
| 54 | VERIFY( s == "128bpm }" ); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | test_unescaped() |
| 59 | { |
| 60 | #ifdef __cpp_exceptions |
| 61 | for (auto f : { "{", "}", "{{{", "{{}", "}{", "{{{{{" }) |
| 62 | try { |
| 63 | (void) std::vformat(f, std::make_format_args()); |
| 64 | VERIFY( false ); |
| 65 | } catch (const std::format_error& e) { |
| 66 | std::string what = e.what(); |
| 67 | VERIFY( what.find("unmatched") != what.npos ); |
| 68 | } |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | struct brit_punc : std::numpunct<char> |
| 73 | { |
| 74 | std::string do_grouping() const override { return "\3\3"; } |
| 75 | char do_thousands_sep() const override { return ','; } |
| 76 | std::string do_truename() const override { return "yes mate"; } |
| 77 | std::string do_falsename() const override { return "nah bruv"; } |
| 78 | }; |
| 79 | |
| 80 | void |
| 81 | test_std_examples() |
| 82 | { |
| 83 | using namespace std; |
| 84 | |
| 85 | string s = format("{0}-{{", 8); // value of s is "8-{" |
| 86 | VERIFY( s == "8-{" ); |
| 87 | |
| 88 | // align |
| 89 | { |
| 90 | char c = 120; |
| 91 | string s0 = format("{:6}", 42); |
| 92 | VERIFY(s0 == " 42"); |
| 93 | string s1 = format("{:6}", 'x'); |
| 94 | VERIFY(s1 == "x "); |
| 95 | string s2 = format("{:*<6}", 'x'); |
| 96 | VERIFY(s2 == "x*****"); |
| 97 | string s3 = format("{:*>6}", 'x'); |
| 98 | VERIFY(s3 == "*****x"); |
| 99 | string s4 = format("{:*^6}", 'x'); |
| 100 | VERIFY(s4 == "**x***"); |
| 101 | string s5 = format("{:6d}", c); |
| 102 | VERIFY(s5 == " 120"); |
| 103 | string s6 = format("{:6}", true); |
| 104 | VERIFY(s6 == "true "); |
| 105 | string s7 = format("{:*<6.3}", "123456"); |
| 106 | VERIFY( s7 == "123***" ); |
| 107 | string s8 = format("{:02}", 1234); |
| 108 | VERIFY( s8 == "1234" ); |
| 109 | string s9 = format("{:*<}", "12"); |
| 110 | VERIFY( s9 == "12" ); |
| 111 | string sA = format("{:*<6}", "12345678"); |
| 112 | VERIFY( sA == "12345678" ); |
| 113 | string sB = format("{:🤡^6}", "x"); |
| 114 | VERIFY( sB == "🤡🤡x🤡🤡🤡" ); |
| 115 | string sC = format("{:*^6}", "🤡🤡🤡"); |
| 116 | VERIFY( sC == "🤡🤡🤡" ); |
| 117 | } |
| 118 | |
| 119 | // sign |
| 120 | { |
| 121 | double inf = numeric_limits<double>::infinity(); |
| 122 | double nan = numeric_limits<double>::quiet_NaN(); |
| 123 | string s0 = format("{0:},{0:+},{0:-},{0: }", 1); |
| 124 | VERIFY(s0 == "1,+1,1, 1"); |
| 125 | string s1 = format("{0:},{0:+},{0:-},{0: }", -1); |
| 126 | VERIFY(s1 == "-1,-1,-1,-1"); |
| 127 | string s2 = format("{0:},{0:+},{0:-},{0: }", inf); |
| 128 | VERIFY(s2 == "inf,+inf,inf, inf"); |
| 129 | string s3 = format("{0:},{0:+},{0:-},{0: }", nan); |
| 130 | VERIFY(s3 == "nan,+nan,nan, nan"); |
| 131 | } |
| 132 | |
| 133 | // alternate form and zero fill |
| 134 | { |
| 135 | char c = 120; |
| 136 | string s1 = format("{:+06d}", c); |
| 137 | VERIFY(s1 == "+00120"); |
| 138 | string s2 = format("{:#06x}", 0xa); |
| 139 | VERIFY(s2 == "0x000a"); |
| 140 | string s3 = format("{:<06}", -42); |
| 141 | VERIFY(s3 == "-42 "); // 0 is ignored because of < alignment |
| 142 | } |
| 143 | |
| 144 | // integer presentation types |
| 145 | { |
| 146 | // Change global locale so "{:L}" adds digit separators. |
| 147 | std::locale::global(std::locale({}, new brit_punc)); |
| 148 | |
| 149 | string s0 = format("{}", 42); |
| 150 | VERIFY(s0 == "42"); |
| 151 | string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); |
| 152 | VERIFY(s1 == "101010 42 52 2a"); |
| 153 | string s2 = format("{0:#x} {0:#X}", 42); |
| 154 | VERIFY(s2 == "0x2a 0X2A"); |
| 155 | string s3 = format("{:L}", 1234); |
| 156 | VERIFY(s3 == "1,234"); |
| 157 | |
| 158 | // Test locale's "byte-and-a-half" grouping (Imperial word? tribble?). |
| 159 | string s4 = format("{:#Lx}", 0xfffff); |
| 160 | VERIFY(s4 == "0xff,fff"); |
| 161 | |
| 162 | // Restore |
| 163 | std::locale::global(std::locale::classic()); |
| 164 | |
| 165 | string s5 = format("{}", -100); // PR libstdc++/114325 |
| 166 | VERIFY(s5 == "-100"); |
| 167 | string s6 = format("{:d} {:d}", -123, 999); |
| 168 | VERIFY(s6 == "-123 999"); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | test_alternate_forms() |
| 174 | { |
| 175 | std::string s; |
| 176 | |
| 177 | s = std::format("{0:#b} {0:+#B} {0:#o} {0:#x} {0:+#X} {0: #d}", 42); |
| 178 | VERIFY( s == "0b101010 +0B101010 052 0x2a +0X2A 42" ); |
| 179 | s = std::format("{0:#b} {0:+#B} {0:#o} {0:#x} {0:+#X} {0: #d}", 0); |
| 180 | VERIFY( s == "0b0 +0B0 0 0x0 +0X0 0" ); |
| 181 | |
| 182 | s = std::format("{0:+#012g} {0:+#014g} {0:+#014g}", 1234.0); |
| 183 | VERIFY( s == "+00001234.00 +0000001234.00 +0000001234.00" ); |
| 184 | s = std::format("{0:+#0{1}g} {0:+#0{2}g} {0:+#0{2}g}", 1234.5, 12, 14); |
| 185 | VERIFY( s == "+00001234.50 +0000001234.50 +0000001234.50" ); |
| 186 | |
| 187 | s = std::format("{:#.2g}", -0.0); |
| 188 | VERIFY( s == "-0.0" ); |
| 189 | |
| 190 | // PR libstdc++/108046 |
| 191 | s = std::format("{0:#.0} {0:#.1} {0:#.0g}", 10.0); |
| 192 | VERIFY( s == "1.e+01 1.e+01 1.e+01" ); |
| 193 | |
| 194 | // PR libstdc++/113512 |
| 195 | s = std::format("{:#.3g}", 0.025); |
| 196 | VERIFY( s == "0.0250" ); |
| 197 | s = std::format("{:#07.3g}", 0.02); |
| 198 | VERIFY( s == "00.0200" ); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | test_infnan() |
| 203 | { |
| 204 | double inf = std::numeric_limits<double>::infinity(); |
| 205 | double nan = std::numeric_limits<double>::quiet_NaN(); |
| 206 | std::string s; |
| 207 | s = std::format("{0} {0:e} {0:E} {0:f} {0:F} {0:g} {0:G} {0:a} {0:A}", inf); |
| 208 | VERIFY( s == "inf inf INF inf INF inf INF inf INF" ); |
| 209 | s = std::format("{0} {0:e} {0:E} {0:f} {0:F} {0:g} {0:G} {0:a} {0:A}", nan); |
| 210 | VERIFY( s == "nan nan NAN nan NAN nan NAN nan NAN" ); |
| 211 | } |
| 212 | |
| 213 | struct euro_punc : std::numpunct<char> |
| 214 | { |
| 215 | std::string do_grouping() const override { return "\3\3"; } |
| 216 | char do_thousands_sep() const override { return '.'; } |
| 217 | char do_decimal_point() const override { return ','; } |
| 218 | }; |
| 219 | |
| 220 | void |
| 221 | test_locale() |
| 222 | { |
| 223 | // The default C locale. |
| 224 | std::locale cloc = std::locale::classic(); |
| 225 | // A custom locale using comma digit separators. |
| 226 | std::locale bloc(cloc, new brit_punc); |
| 227 | // A custom locale using period digit separators. |
| 228 | std::locale eloc(cloc, new euro_punc); |
| 229 | |
| 230 | std::string s; |
| 231 | |
| 232 | // Change the global locale: |
| 233 | std::locale::global(bloc); |
| 234 | // Format using the global locale: |
| 235 | s = std::format("{0:L} {0:Lx} {0:Lb}", 12345); |
| 236 | VERIFY( s == "12,345 3,039 11,000,000,111,001" ); |
| 237 | s = std::format("{0:L} {0:.7Lg} {0:La}", 12345.6789); |
| 238 | VERIFY( s == "12,345.6789 12,345.68 1.81cd6e631f8a1p+13" ); |
| 239 | |
| 240 | s = std::format("{0:s} {0:L} {1:Ls} {0:Ld}", true, false); |
| 241 | VERIFY( s == "true yes mate nah bruv 1" ); |
| 242 | |
| 243 | // Format using a specific locale: |
| 244 | s = std::format(eloc, "{0:L} {0:Lx} {0:Lb}", 12345); |
| 245 | VERIFY( s == "12.345 3.039 11.000.000.111.001" ); |
| 246 | s = std::format(eloc, "{0:L} {0:.7LG} {0:La}", 12345.6789); |
| 247 | VERIFY( s == "12.345,6789 12.345,68 1,81cd6e631f8a1p+13" ); |
| 248 | |
| 249 | s = std::format(eloc, "{0:#Lg} {0:+#.3Lg} {0:#08.4Lg}", -1234.); |
| 250 | VERIFY( s == "-1.234,00 -1,23e+03 -01.234," ); |
| 251 | |
| 252 | s = std::format(cloc, "{:05L}", -1.0); // PR libstdc++/110968 |
| 253 | VERIFY( s == "-0001" ); |
| 254 | |
| 255 | // PR libstdc++/114863 grouping applied to nan and inf |
| 256 | double inf = std::numeric_limits<double>::infinity(); |
| 257 | s = std::format(eloc, "{0:Le} {0:Lf} {0:Lg}", -inf); |
| 258 | VERIFY( s == "-inf -inf -inf" ); |
| 259 | double nan = std::numeric_limits<double>::quiet_NaN(); |
| 260 | s = std::format(eloc, "{0:Le} {0:Lf} {0:Lg}", -nan); |
| 261 | VERIFY( s == "-nan -nan -nan" ); |
| 262 | |
| 263 | // PR libstdc++/120548 format confuses a negative sign for a thousands digit |
| 264 | s = std::format(bloc, "{:L}", -123.45); |
| 265 | VERIFY( s == "-123.45" ); |
| 266 | s = std::format(bloc, "{:-L}", -876543.21); |
| 267 | VERIFY( s == "-876,543.21" ); |
| 268 | s = std::format(bloc, "{:+L}", 333.22); |
| 269 | VERIFY( s == "+333.22" ); |
| 270 | s = std::format(bloc, "{: L}", 999.44); |
| 271 | VERIFY( s == " 999.44" ); |
| 272 | |
| 273 | // Restore |
| 274 | std::locale::global(cloc); |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | test_width() |
| 279 | { |
| 280 | std::string s; |
| 281 | |
| 282 | s = std::format("{:4}", ""); |
| 283 | VERIFY( s == " " ); |
| 284 | s = std::format("{:{}}", "", 3); |
| 285 | VERIFY( s == " " ); |
| 286 | s = std::format("{:{}}|{:{}}", 1, 2, 3, 4); |
| 287 | VERIFY( s == " 1| 3" ); |
| 288 | s = std::format("{1:{0}}", 2, ""); |
| 289 | VERIFY( s == " " ); |
| 290 | s = std::format("{:03}", 9); |
| 291 | VERIFY( s == "009" ); |
| 292 | |
| 293 | s = std::format("DR {0:{1}}: allow width {1} from arg-id", 3721, 0); |
| 294 | VERIFY( s == "DR 3721: allow width 0 from arg-id" ); |
| 295 | |
| 296 | try { |
| 297 | s = std::format("Negative width is an error: {0:{1}}", 123, -1); |
| 298 | VERIFY(false); |
| 299 | } catch (const std::format_error&) { |
| 300 | } |
| 301 | |
| 302 | try { |
| 303 | bool no = false, yes = true; |
| 304 | auto args = std::make_format_args(no, yes); |
| 305 | s = std::vformat("DR 3720: restrict type of width arg-id {0:{1}}", args); |
| 306 | VERIFY(false); |
| 307 | } catch (const std::format_error&) { |
| 308 | } |
| 309 | |
| 310 | try { |
| 311 | char wat = '?', bang = '!'; |
| 312 | auto args = std::make_format_args(wat, bang); |
| 313 | s = std::vformat("DR 3720: restrict type of width arg-id {0:{1}}", args); |
| 314 | VERIFY(false); |
| 315 | } catch (const std::format_error&) { |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | test_char() |
| 321 | { |
| 322 | std::string s; |
| 323 | |
| 324 | s = std::format("{}", 'a'); |
| 325 | VERIFY( s == "a" ); |
| 326 | |
| 327 | s = std::format("{:c} {:d} {:o}", 'b', '\x17', '\x3f'); |
| 328 | VERIFY( s == "b 23 77" ); |
| 329 | |
| 330 | s = std::format("{:#d} {:#o}", '\x17', '\x3f'); |
| 331 | VERIFY( s == "23 077" ); |
| 332 | |
| 333 | s = std::format("{:04d} {:04o}", '\x17', '\x3f'); |
| 334 | VERIFY( s == "0023 0077" ); |
| 335 | |
| 336 | s = std::format("{:b} {:B} {:#b} {:#B}", '\xff', '\xa0', '\x17', '\x3f'); |
| 337 | VERIFY( s == "11111111 10100000 0b10111 0B111111" ); |
| 338 | |
| 339 | s = std::format("{:x} {:#x} {:#X}", '\x12', '\x34', '\x45'); |
| 340 | VERIFY( s == "12 0x34 0X45" ); |
| 341 | |
| 342 | // P2909R4 Fix formatting of code units as integers (Dude, where’s my char?) |
| 343 | // char and wchar_t should be converted to unsigned when formatting them |
| 344 | // with an integer presentation type. |
| 345 | s = std::format("{0:b} {0:B} {0:d} {0:o} {0:x} {0:X}", '\xf0'); |
| 346 | VERIFY( s == "11110000 11110000 240 360 f0 F0" ); |
| 347 | } |
| 348 | |
| 349 | void |
| 350 | test_wchar() |
| 351 | { |
| 352 | using namespace std::literals; |
| 353 | std::wstring s; |
| 354 | |
| 355 | s = std::format(L"{}", L'a'); |
| 356 | VERIFY( s == L"a" ); |
| 357 | |
| 358 | s = std::format(L"{} {} {} {} {} {}", L'0', 1, 2LL, 3.4, L"five", L"six"s); |
| 359 | VERIFY( s == L"0 1 2 3.4 five six" ); |
| 360 | |
| 361 | std::locale loc; |
| 362 | s = std::format(loc, L"{:L} {:.3s}{:Lc}", true, L"data"sv, '.'); |
| 363 | VERIFY( s == L"true dat." ); |
| 364 | |
| 365 | s = std::format(L"{}", 0.0625); |
| 366 | VERIFY( s == L"0.0625" ); |
| 367 | s = std::format(L"{}", 0.25); |
| 368 | VERIFY( s == L"0.25" ); |
| 369 | s = std::format(L"{:+a} {:A}", 0x1.23p45, -0x1.abcdefp-15); |
| 370 | VERIFY( s == L"+1.23p+45 -1.ABCDEFP-15" ); |
| 371 | |
| 372 | double inf = std::numeric_limits<double>::infinity(); |
| 373 | double nan = std::numeric_limits<double>::quiet_NaN(); |
| 374 | s = std::format(L"{0} {0:F} {1} {1:E}", -inf, -nan); |
| 375 | VERIFY( s == L"-inf -INF -nan -NAN" ); |
| 376 | |
| 377 | s = std::format(L"{0:#b} {0:#B} {0:#x} {0:#X}", 99); |
| 378 | VERIFY( s == L"0b1100011 0B1100011 0x63 0X63" ); |
| 379 | |
| 380 | // P2909R4 Fix formatting of code units as integers (Dude, where’s my char?) |
| 381 | s = std::format(L"{:d} {:d}", wchar_t(-1), char(-1)); |
| 382 | VERIFY( s.find('-') == std::wstring::npos ); |
| 383 | |
| 384 | auto ws = std::format(L"{:L}", 0.5); |
| 385 | VERIFY( ws == L"0.5" ); |
| 386 | // The default C locale. |
| 387 | std::locale cloc = std::locale::classic(); |
| 388 | // PR libstdc++/119671 use-after-free formatting floating-point to wstring |
| 389 | ws = std::format(cloc, L"{:L}", 0.5); |
| 390 | VERIFY( ws == L"0.5" ); |
| 391 | // A locale with no name, but with the same facets as the C locale. |
| 392 | std::locale locx(cloc, &std::use_facet<std::ctype<char>>(cloc)); |
| 393 | ws = std::format(locx, L"{:L}", 0.5); |
| 394 | VERIFY( ws == L"0.5" ); |
| 395 | } |
| 396 | |
| 397 | void |
| 398 | test_minmax() |
| 399 | { |
| 400 | auto check = []<typename T, typename U = std::make_unsigned_t<T>>(T, U = 0) { |
| 401 | const int digits = std::numeric_limits<T>::digits; |
| 402 | const std::string zeros(digits, '0'); |
| 403 | const std::string ones(digits, '1'); |
| 404 | auto s = std::format("{:b}" , std::numeric_limits<T>::min()); |
| 405 | VERIFY( s == "-1" + zeros ); |
| 406 | s = std::format("{:b}" , std::numeric_limits<T>::max()); |
| 407 | VERIFY( s == ones ); |
| 408 | s = std::format("{:0{}b}" , std::numeric_limits<U>::min(), digits + 1); |
| 409 | VERIFY( s == '0' + zeros ); |
| 410 | s = std::format("{:b}" , std::numeric_limits<U>::max()); |
| 411 | VERIFY( s == '1' + ones ); |
| 412 | }; |
| 413 | check((signed char)(0)); // int8_t is char on Solaris, see PR 113450 |
| 414 | check(std::int16_t(0)); |
| 415 | check(std::int32_t(0)); |
| 416 | check(std::int64_t(0)); |
| 417 | #ifdef __SIZEOF_INT128__ |
| 418 | // std::make_unsigned_t<__int128> is invalid for strict -std=c++20 mode, |
| 419 | // so pass a second argument of the unsigned type. |
| 420 | check(__int128(0), (unsigned __int128)(0)); |
| 421 | #endif |
| 422 | } |
| 423 | |
| 424 | void |
| 425 | test_p1652r1() // printf corner cases in std::format |
| 426 | { |
| 427 | std::string s; |
| 428 | |
| 429 | // Problem 1: "#o" specification should not print 0 as "00" |
| 430 | s = std::format("{:#o}", 0); |
| 431 | VERIFY( s == "0" ); |
| 432 | |
| 433 | // Problem 2: 'c' should be able to print 65 as "A" (ASCII) |
| 434 | int c = 'A'; |
| 435 | s = std::format("{:c}", c); |
| 436 | VERIFY( s == "A" ); |
| 437 | |
| 438 | // Problem 3: "-000nan" is not a floating point value |
| 439 | double nan = std::numeric_limits<double>::quiet_NaN(); |
| 440 | try { |
| 441 | s = std::vformat("{:0=6}", std::make_format_args(nan)); |
| 442 | VERIFY( false ); |
| 443 | } catch (const std::format_error&) { |
| 444 | } |
| 445 | |
| 446 | s = std::format("{:06}", nan); |
| 447 | VERIFY( s == " nan" ); |
| 448 | |
| 449 | // Problem 4: bool needs a type format specifier |
| 450 | s = std::format("{:s}", true); |
| 451 | VERIFY( s == "true" ); |
| 452 | |
| 453 | // Problem 5: double does not roundtrip float |
| 454 | s = std::format("{}", 3.31f); |
| 455 | VERIFY( s == "3.31" ); |
| 456 | } |
| 457 | |
| 458 | void |
| 459 | test_pointer() |
| 460 | { |
| 461 | void* p = nullptr; |
| 462 | const void* pc = p; |
| 463 | std::string s, str_int; |
| 464 | |
| 465 | s = std::format("{}", p); |
| 466 | VERIFY( s == "0x0" ); |
| 467 | |
| 468 | s = std::format("{} {} {}", p, pc, nullptr); |
| 469 | VERIFY( s == "0x0 0x0 0x0" ); |
| 470 | s = std::format("{:p} {:p} {:p}", p, pc, nullptr); |
| 471 | VERIFY( s == "0x0 0x0 0x0" ); |
| 472 | s = std::format("{:4},{:5},{:6}", p, pc, nullptr); // width |
| 473 | VERIFY( s == " 0x0, 0x0, 0x0" ); |
| 474 | s = std::format("{:<4},{:>5},{:^7}", p, pc, nullptr); // align+width |
| 475 | VERIFY( s == "0x0 , 0x0, 0x0 " ); |
| 476 | s = std::format("{:o<4},{:o>5},{:o^7}", p, pc, nullptr); // fill+align+width |
| 477 | VERIFY( s == "0x0o,oo0x0,oo0x0oo" ); |
| 478 | |
| 479 | pc = p = &s; |
| 480 | str_int = std::format("{:#x}", reinterpret_cast<std::uintptr_t>(p)); |
| 481 | s = std::format("{} {} {}", p, pc, nullptr); |
| 482 | VERIFY( s == (str_int + ' ' + str_int + " 0x0") ); |
| 483 | str_int = std::format("{:#20x}", reinterpret_cast<std::uintptr_t>(p)); |
| 484 | s = std::format("{:20} {:20p}", p, pc); |
| 485 | VERIFY( s == (str_int + ' ' + str_int) ); |
| 486 | |
| 487 | #if __cpp_lib_format >= 202304L |
| 488 | // P2510R3 Formatting pointers |
| 489 | s = std::format("{:06} {:07P} {:08p}", (void*)0, (const void*)0, nullptr); |
| 490 | VERIFY( s == "0x0000 0X00000 0x000000" ); |
| 491 | str_int = std::format("{:#016x}", reinterpret_cast<std::uintptr_t>(p)); |
| 492 | s = std::format("{:016} {:016}", p, pc); |
| 493 | VERIFY( s == (str_int + ' ' + str_int) ); |
| 494 | str_int = std::format("{:#016X}", reinterpret_cast<std::uintptr_t>(p)); |
| 495 | s = std::format("{:016P} {:016P}", p, pc); |
| 496 | VERIFY( s == (str_int + ' ' + str_int) ); |
| 497 | #endif |
| 498 | } |
| 499 | |
| 500 | void |
| 501 | test_bool() |
| 502 | { |
| 503 | std::string s; |
| 504 | |
| 505 | s = std::format("{}", true); |
| 506 | VERIFY( s == "true" ); |
| 507 | s = std::format("{:} {:s}", true, false); |
| 508 | VERIFY( s == "true false" ); |
| 509 | s = std::format("{:b} {:#b}", true, false); |
| 510 | VERIFY( s == "1 0b0" ); |
| 511 | s = std::format("{:B} {:#B}", false, true); |
| 512 | VERIFY( s == "0 0B1" ); |
| 513 | s = std::format("{:d} {:#d}", false, true); |
| 514 | VERIFY( s == "0 1" ); |
| 515 | s = std::format("{:o} {:#o} {:#o}", false, true, false); |
| 516 | VERIFY( s == "0 01 0" ); |
| 517 | s = std::format("{:x} {:#x} {:#X}", false, true, false); |
| 518 | VERIFY( s == "0 0x1 0X0" ); |
| 519 | } |
| 520 | |
| 521 | void |
| 522 | test_unicode() |
| 523 | { |
| 524 | // Similar to sC example in test_std_examples, but not from the standard. |
| 525 | // Verify that the character "🤡" has estimated field width 2, |
| 526 | // rather than estimated field width equal to strlen("🤡"), which would be 4, |
| 527 | // or just width 1 for single character. |
| 528 | std::string sC = std::format("{:*<3}", "🤡"); |
| 529 | VERIFY( sC == "🤡*" ); |
| 530 | std::wstring wsC = std::format(L"{:*<3}", L"🤡"); |
| 531 | VERIFY( wsC == L"🤡*" ); |
| 532 | wsC = std::format(L"{:*<3}", L'🤡'); |
| 533 | VERIFY( wsC == L"🤡*" ); |
| 534 | |
| 535 | // Verify that "£" has estimated field width 1, not strlen("£") == 2. |
| 536 | std::string sL = std::format("{:*<3}", "£"); |
| 537 | VERIFY( sL == "£**" ); |
| 538 | |
| 539 | // Verify that precision is measured in field width units (column positions) |
| 540 | // not bytes. The result should contain complete Unicode characters, not be |
| 541 | // truncated in the middle of a multibyte UTF-8 sequence. The string "£" has |
| 542 | // field width 1 despite being 2 bytes, and the string "🤡" has field width 2 |
| 543 | // and so cannot be formatted into a replacement field using .1 precision. |
| 544 | std::string sP = std::format("{:1.1} {:*<1.1}", "£", "🤡"); |
| 545 | VERIFY( sP == "£ *" ); |
| 546 | sP = std::format("{:*<2.1} {:*<2.1}", "£", "🤡"); |
| 547 | VERIFY( sP == "£* **" ); |
| 548 | |
| 549 | // Verify field width handling for extended grapheme clusters, |
| 550 | // and that a cluster gets output as a single item, not truncated. |
| 551 | std::string sG = std::format("{:*>2.1}", "\u006f\u0302\u0323!"); |
| 552 | VERIFY( sG == "*\u006f\u0302\u0323" ); |
| 553 | |
| 554 | // Examples from P1868R2 |
| 555 | // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1868r2.html |
| 556 | const char* inputs[][2] = { |
| 557 | {"\x41", " \u0041"}, |
| 558 | {"\xC3\x81", " \u00c1"}, |
| 559 | {"\x41\xCC\x81", " \u0041\u0301"}, |
| 560 | {"\xc4\xb2", " \u0132"}, |
| 561 | {"\xce\x94", " \u0394"}, |
| 562 | {"\xd0\xa9", " \u0429"}, |
| 563 | {"\xd7\x90", " \u05D0"}, |
| 564 | {"\xd8\xb4", " \u0634"}, |
| 565 | {"\xe3\x80\x89", " \u3009"}, |
| 566 | {"\xe7\x95\x8c", " \u754C"}, |
| 567 | {"\xf0\x9f\xa6\x84", " \U0001F984"}, |
| 568 | {"\xf0\x9f\x91\xa8\xe2\x80\x8d\xf0\x9f\x91\xa9\xe2\x80\x8d" |
| 569 | "\xf0\x9f\x91\xa7\xe2\x80\x8d\xf0\x9f\x91\xa6", |
| 570 | " \U0001F468\u200D\U0001F469\u200D\U0001F467\u200D\U0001F466" } |
| 571 | }; |
| 572 | for (auto& input : inputs) |
| 573 | { |
| 574 | std::string sA = std::format("{:>5}", input[0]); |
| 575 | VERIFY( sA == input[1] ); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | int main() |
| 580 | { |
| 581 | test_no_args(); |
| 582 | test_unescaped(); |
| 583 | test_std_examples(); |
| 584 | test_alternate_forms(); |
| 585 | test_locale(); |
| 586 | test_width(); |
| 587 | test_char(); |
| 588 | test_wchar(); |
| 589 | test_minmax(); |
| 590 | test_p1652r1(); |
| 591 | test_pointer(); |
| 592 | test_bool(); |
| 593 | test_unicode(); |
| 594 | } |