| 1 | // { dg-options "-fexec-charset=UTF-8 -fwide-exec-charset=UTF-32LE -DUNICODE_ENC" { target le } } |
| 2 | // { dg-options "-fexec-charset=UTF-8 -fwide-exec-charset=UTF-32BE -DUNICODE_ENC" { target be } } |
| 3 | // { dg-do run { target c++23 } } |
| 4 | // { dg-add-options no_pch } |
| 5 | |
| 6 | #include <format> |
| 7 | #include <testsuite_hooks.h> |
| 8 | |
| 9 | std::string |
| 10 | fdebug(char t) |
| 11 | { return std::format("{:?}", t); } |
| 12 | |
| 13 | std::wstring |
| 14 | fdebug(wchar_t t) |
| 15 | { return std::format(L"{:?}", t); } |
| 16 | |
| 17 | std::string |
| 18 | fdebug(std::string_view t) |
| 19 | { return std::format("{:?}", t); } |
| 20 | |
| 21 | std::wstring |
| 22 | fdebug(std::wstring_view t) |
| 23 | { return std::format(L"{:?}", t); } |
| 24 | |
| 25 | |
| 26 | #define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S) |
| 27 | #define WIDEN(S) WIDEN_(_CharT, S) |
| 28 | |
| 29 | template<typename _CharT> |
| 30 | void |
| 31 | test_basic_escapes() |
| 32 | { |
| 33 | std::basic_string<_CharT> res; |
| 34 | |
| 35 | const auto tab = WIDEN("\t"); |
| 36 | res = fdebug(tab); |
| 37 | VERIFY( res == WIDEN(R"("\t")") ); |
| 38 | res = fdebug(tab[0]); |
| 39 | VERIFY( res == WIDEN(R"('\t')") ); |
| 40 | |
| 41 | const auto nline = WIDEN("\n"); |
| 42 | res = fdebug(nline); |
| 43 | VERIFY( res == WIDEN(R"("\n")") ); |
| 44 | res = fdebug(nline[0]); |
| 45 | VERIFY( res == WIDEN(R"('\n')") ); |
| 46 | |
| 47 | const auto carret = WIDEN("\r"); |
| 48 | res = fdebug(carret); |
| 49 | VERIFY( res == WIDEN(R"("\r")") ); |
| 50 | res = fdebug(carret[0]); |
| 51 | VERIFY( res == WIDEN(R"('\r')") ); |
| 52 | |
| 53 | const auto bslash = WIDEN("\\"); |
| 54 | res = fdebug(bslash); |
| 55 | VERIFY( res == WIDEN(R"("\\")") ); |
| 56 | res = fdebug(bslash[0]); |
| 57 | VERIFY( res == WIDEN(R"('\\')") ); |
| 58 | |
| 59 | const auto quote = WIDEN("\""); |
| 60 | res = fdebug(quote); |
| 61 | VERIFY( res == WIDEN(R"("\"")") ); |
| 62 | res = fdebug(quote[0]); |
| 63 | VERIFY( res == WIDEN(R"('"')") ); |
| 64 | |
| 65 | const auto apos = WIDEN("\'"); |
| 66 | res = fdebug(apos); |
| 67 | VERIFY( res == WIDEN(R"("'")") ); |
| 68 | res = fdebug(apos[0]); |
| 69 | VERIFY( res == WIDEN(R"('\'')") ); |
| 70 | } |
| 71 | |
| 72 | template<typename _CharT> |
| 73 | void |
| 74 | test_ascii_escapes() |
| 75 | { |
| 76 | std::basic_string<_CharT> res; |
| 77 | |
| 78 | const auto in = WIDEN("\x10 abcde\x7f\t0123"); |
| 79 | res = fdebug(in); |
| 80 | VERIFY( res == WIDEN(R"("\u{10} abcde\u{7f}\t0123")") ); |
| 81 | res = fdebug(in[0]); |
| 82 | VERIFY( res == WIDEN(R"('\u{10}')") ); |
| 83 | res = fdebug(in[1]); |
| 84 | VERIFY( res == WIDEN(R"(' ')") ); |
| 85 | res = fdebug(in[2]); |
| 86 | VERIFY( res == WIDEN(R"('a')") ); |
| 87 | } |
| 88 | |
| 89 | template<typename _CharT> |
| 90 | void |
| 91 | test_extended_ascii() |
| 92 | { |
| 93 | std::basic_string<_CharT> res; |
| 94 | |
| 95 | const auto in = WIDEN("Åëÿ"); |
| 96 | res = fdebug(in); |
| 97 | VERIFY( res == WIDEN(R"("Åëÿ")") ); |
| 98 | |
| 99 | static constexpr bool __test_characters |
| 100 | #if UNICODE_ENC |
| 101 | = sizeof(_CharT) >= 2; |
| 102 | #else // ISO8859-1 |
| 103 | = true; |
| 104 | #endif // UNICODE_ENC |
| 105 | |
| 106 | if constexpr (__test_characters) |
| 107 | { |
| 108 | res = fdebug(in[0]); |
| 109 | VERIFY( res == WIDEN(R"('Å')") ); |
| 110 | res = fdebug(in[1]); |
| 111 | VERIFY( res == WIDEN(R"('ë')") ); |
| 112 | res = fdebug(in[2]); |
| 113 | VERIFY( res == WIDEN(R"('ÿ')") ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | #if UNICODE_ENC |
| 118 | template<typename _CharT> |
| 119 | void |
| 120 | test_unicode_escapes() |
| 121 | { |
| 122 | std::basic_string<_CharT> res; |
| 123 | |
| 124 | const auto in = WIDEN( |
| 125 | "\u008a" // Cc, Control, Line Tabulation Set, |
| 126 | "\u00ad" // Cf, Format, Soft Hyphen |
| 127 | "\u1d3d" // Lm, Modifier letter, Modifier Letter Capital Ou |
| 128 | "\u00a0" // Zs, Space Separator, No-Break Space (NBSP) |
| 129 | "\u2029" // Zp, Paragraph Separator, Paragraph Separator |
| 130 | "\U0001f984" // So, Other Symbol, Unicorn Face |
| 131 | ); |
| 132 | const auto out = WIDEN("\"" |
| 133 | R"(\u{8a})" |
| 134 | R"(\u{ad})" |
| 135 | "\u1d3d" |
| 136 | R"(\u{a0})" |
| 137 | R"(\u{2029})" |
| 138 | "\U0001f984" |
| 139 | "\""); |
| 140 | |
| 141 | res = fdebug(in); |
| 142 | VERIFY( res == out ); |
| 143 | |
| 144 | if constexpr (sizeof(_CharT) >= 2) |
| 145 | { |
| 146 | res = fdebug(in[0]); |
| 147 | VERIFY( res == WIDEN(R"('\u{8a}')") ); |
| 148 | res = fdebug(in[1]); |
| 149 | VERIFY( res == WIDEN(R"('\u{ad}')") ); |
| 150 | res = fdebug(in[2]); |
| 151 | VERIFY( res == WIDEN("'\u1d3d'") ); |
| 152 | res = fdebug(in[3]); |
| 153 | VERIFY( res == WIDEN(R"('\u{a0}')") ); |
| 154 | res = fdebug(in[4]); |
| 155 | VERIFY( res == WIDEN(R"('\u{2029}')") ); |
| 156 | } |
| 157 | |
| 158 | if constexpr (sizeof(_CharT) >= 4) |
| 159 | { |
| 160 | res = fdebug(in[5]); |
| 161 | VERIFY( res == WIDEN("'\U0001f984'") ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | template<typename _CharT> |
| 166 | void |
| 167 | test_grapheme_extend() |
| 168 | { |
| 169 | std::basic_string<_CharT> res; |
| 170 | |
| 171 | const auto vin = WIDEN("o\u0302\u0323"); |
| 172 | res = fdebug(vin); |
| 173 | VERIFY( res == WIDEN("\"o\u0302\u0323\"") ); |
| 174 | |
| 175 | std::basic_string_view<_CharT> in = WIDEN("\t\u0302\u0323"); |
| 176 | res = fdebug(in); |
| 177 | VERIFY( res == WIDEN(R"("\t\u{302}\u{323}")") ); |
| 178 | |
| 179 | res = fdebug(in.substr(1)); |
| 180 | VERIFY( res == WIDEN(R"("\u{302}\u{323}")") ); |
| 181 | |
| 182 | if constexpr (sizeof(_CharT) >= 2) |
| 183 | { |
| 184 | res = fdebug(in[1]); |
| 185 | VERIFY( res == WIDEN(R"('\u{302}')") ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | template<typename _CharT> |
| 190 | void |
| 191 | test_replacement_char() |
| 192 | { |
| 193 | std::basic_string<_CharT> repl = WIDEN("\uFFFD"); |
| 194 | std::basic_string<_CharT> res = fdebug(repl); |
| 195 | VERIFY( res == WIDEN("\"\uFFFD\"") ); |
| 196 | |
| 197 | repl = WIDEN("\uFFFD\uFFFD"); |
| 198 | res = fdebug(repl); |
| 199 | VERIFY( res == WIDEN("\"\uFFFD\uFFFD\"") ); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | test_ill_formed_utf8_seq() |
| 204 | { |
| 205 | std::string_view seq = "\xf0\x9f\xa6\x84"; // \U0001F984 |
| 206 | std::string res; |
| 207 | |
| 208 | res = fdebug(seq); |
| 209 | VERIFY( res == "\"\U0001F984\"" ); |
| 210 | |
| 211 | res = fdebug(seq.substr(1)); |
| 212 | VERIFY( res == R"("\x{9f}\x{a6}\x{84}")" ); |
| 213 | |
| 214 | res = fdebug(seq.substr(2)); |
| 215 | VERIFY( res == R"("\x{a6}\x{84}")" ); |
| 216 | |
| 217 | res = fdebug(seq[0]); |
| 218 | VERIFY( res == R"('\x{f0}')" ); |
| 219 | res = fdebug(seq.substr(0, 1)); |
| 220 | VERIFY( res == R"("\x{f0}")" ); |
| 221 | |
| 222 | res = fdebug(seq[1]); |
| 223 | VERIFY( res == R"('\x{9f}')" ); |
| 224 | res = fdebug(seq.substr(1, 1)); |
| 225 | VERIFY( res == R"("\x{9f}")" ); |
| 226 | |
| 227 | res = fdebug(seq[2]); |
| 228 | VERIFY( res == R"('\x{a6}')" ); |
| 229 | res = fdebug(seq.substr(2, 1)); |
| 230 | VERIFY( res == R"("\x{a6}")" ); |
| 231 | |
| 232 | res = fdebug(seq[3]); |
| 233 | VERIFY( res == R"('\x{84}')" ); |
| 234 | res = fdebug(seq.substr(3, 1)); |
| 235 | VERIFY( res == R"("\x{84}")" ); |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | test_ill_formed_utf32() |
| 240 | { |
| 241 | std::wstring res; |
| 242 | |
| 243 | wchar_t ic1 = static_cast<wchar_t>(0xff'ffff); |
| 244 | res = fdebug(ic1); |
| 245 | VERIFY( res == LR"('\x{ffffff}')" ); |
| 246 | |
| 247 | std::wstring is1(1, ic1); |
| 248 | res = fdebug(is1); |
| 249 | VERIFY( res == LR"("\x{ffffff}")" ); |
| 250 | |
| 251 | wchar_t ic2 = static_cast<wchar_t>(0xffff'ffff); |
| 252 | res = fdebug(ic2); |
| 253 | VERIFY( res == LR"('\x{ffffffff}')" ); |
| 254 | |
| 255 | std::wstring is2(1, ic2); |
| 256 | res = fdebug(is2); |
| 257 | VERIFY( res == LR"("\x{ffffffff}")" ); |
| 258 | } |
| 259 | #endif // UNICODE_ENC |
| 260 | |
| 261 | template<typename _CharT> |
| 262 | void |
| 263 | test_fill() |
| 264 | { |
| 265 | std::basic_string<_CharT> res; |
| 266 | |
| 267 | std::basic_string_view<_CharT> in = WIDEN("a\t\x10\u00ad"); |
| 268 | res = std::format(WIDEN("{:10?}"), in.substr(0, 1)); |
| 269 | VERIFY( res == WIDEN(R"("a" )") ); |
| 270 | |
| 271 | res = std::format(WIDEN("{:->10?}"), in.substr(1, 1)); |
| 272 | VERIFY( res == WIDEN(R"(------"\t")") ); |
| 273 | |
| 274 | res = std::format(WIDEN("{:+<10?}"), in.substr(2, 1)); |
| 275 | VERIFY( res == WIDEN(R"("\u{10}"++)") ); |
| 276 | |
| 277 | |
| 278 | res = std::format(WIDEN("{:10?}"), in[0]); |
| 279 | VERIFY( res == WIDEN(R"('a' )") ); |
| 280 | |
| 281 | res = std::format(WIDEN("{:->10?}"), in[1]); |
| 282 | VERIFY( res == WIDEN(R"(------'\t')") ); |
| 283 | |
| 284 | res = std::format(WIDEN("{:+<10?}"), in[2]); |
| 285 | VERIFY( res == WIDEN(R"('\u{10}'++)") ); |
| 286 | |
| 287 | #if UNICODE_ENC |
| 288 | res = std::format(WIDEN("{:=^10?}"), in.substr(3)); |
| 289 | VERIFY( res == WIDEN(R"(="\u{ad}"=)") ); |
| 290 | |
| 291 | // width is 2 |
| 292 | std::basic_string_view<_CharT> in2 = WIDEN("\u1100"); |
| 293 | res = std::format(WIDEN("{:*^10?}"), in2); |
| 294 | VERIFY( res == WIDEN("***\"\u1100\"***") ); |
| 295 | |
| 296 | if constexpr (sizeof(_CharT) >= 2) |
| 297 | { |
| 298 | res = std::format(WIDEN("{:=^10?}"), in[3]); |
| 299 | VERIFY( res == WIDEN(R"(='\u{ad}'=)") ); |
| 300 | |
| 301 | res = std::format(WIDEN("{:*^10?}"), in2[0]); |
| 302 | VERIFY( res == WIDEN("***'\u1100'***") ); |
| 303 | } |
| 304 | #endif // UNICODE_ENC |
| 305 | } |
| 306 | |
| 307 | template<typename _CharT> |
| 308 | void |
| 309 | test_prec() |
| 310 | { |
| 311 | std::basic_string<_CharT> res; |
| 312 | // with ? escpaed presentation is copied to ouput, same as source |
| 313 | |
| 314 | std::basic_string_view<_CharT> in = WIDEN("a\t\x10\u00ad"); |
| 315 | res = std::format(WIDEN("{:.2?}"), in.substr(0, 1)); |
| 316 | VERIFY( res == WIDEN(R"("a)") ); |
| 317 | |
| 318 | res = std::format(WIDEN("{:.4?}"), in.substr(1, 1)); |
| 319 | VERIFY( res == WIDEN(R"("\t")") ); |
| 320 | |
| 321 | res = std::format(WIDEN("{:.5?}"), in.substr(2, 1)); |
| 322 | VERIFY( res == WIDEN(R"("\u{1)") ); |
| 323 | |
| 324 | #if UNICODE_ENC |
| 325 | res = std::format(WIDEN("{:.10?}"), in.substr(3)); |
| 326 | VERIFY( res == WIDEN(R"("\u{ad}")") ); |
| 327 | |
| 328 | std::basic_string_view<_CharT> in2 = WIDEN("\u1100"); |
| 329 | res = std::format(WIDEN("{:.3?}"), in2); |
| 330 | VERIFY( res == WIDEN("\"\u1100") ); |
| 331 | #endif // UNICODE_ENC |
| 332 | } |
| 333 | |
| 334 | void test_char_as_wchar() |
| 335 | { |
| 336 | std::wstring res; |
| 337 | |
| 338 | res = std::format(L"{:?}", 'a'); |
| 339 | VERIFY( res == LR"('a')" ); |
| 340 | |
| 341 | res = std::format(L"{:?}", '\t'); |
| 342 | VERIFY( res == LR"('\t')" ); |
| 343 | |
| 344 | res = std::format(L"{:+<10?}", '\x10'); |
| 345 | VERIFY( res == LR"('\u{10}'++)" ); |
| 346 | } |
| 347 | |
| 348 | template<typename T> |
| 349 | struct DebugWrapper |
| 350 | { |
| 351 | T val; |
| 352 | }; |
| 353 | |
| 354 | template<typename T, typename CharT> |
| 355 | struct std::formatter<DebugWrapper<T>, CharT> |
| 356 | { |
| 357 | constexpr std::basic_format_parse_context<CharT>::iterator |
| 358 | parse(std::basic_format_parse_context<CharT>& pc) |
| 359 | { |
| 360 | auto out = under.parse(pc); |
| 361 | under.set_debug_format(); |
| 362 | return out; |
| 363 | } |
| 364 | |
| 365 | template<typename Out> |
| 366 | Out format(DebugWrapper<T> const& t, |
| 367 | std::basic_format_context<Out, CharT>& fc) const |
| 368 | { return under.format(t.val, fc); } |
| 369 | |
| 370 | private: |
| 371 | std::formatter<T, CharT> under; |
| 372 | }; |
| 373 | |
| 374 | template<typename _CharT, typename StrT> |
| 375 | void |
| 376 | test_formatter_str() |
| 377 | { |
| 378 | _CharT buf[]{ 'a', 'b', 'c', 0 }; |
| 379 | DebugWrapper<StrT> in{ buf }; |
| 380 | std::basic_string<_CharT> res = std::format(WIDEN("{:?}"), in ); |
| 381 | VERIFY( res == WIDEN(R"("abc")") ); |
| 382 | } |
| 383 | |
| 384 | template<typename _CharT> |
| 385 | void |
| 386 | test_formatter_arr() |
| 387 | { |
| 388 | std::basic_string<_CharT> res; |
| 389 | |
| 390 | DebugWrapper<_CharT[3]> in3{ 'a', 'b', 'c' }; |
| 391 | res = std::format(WIDEN("{:?}"), in3 ); |
| 392 | VERIFY( res == WIDEN(R"("abc")") ); |
| 393 | |
| 394 | // We print all characters, including null-terminator |
| 395 | DebugWrapper<_CharT[4]> in4{ 'a', 'b', 'c', 0 }; |
| 396 | res = std::format(WIDEN("{:?}"), in4 ); |
| 397 | VERIFY( res == WIDEN(R"("abc\u{0}")") ); |
| 398 | } |
| 399 | |
| 400 | template<typename _CharT, typename SrcT> |
| 401 | void |
| 402 | test_formatter_char() |
| 403 | { |
| 404 | DebugWrapper<SrcT> in{ 'a' }; |
| 405 | std::basic_string<_CharT> res = std::format(WIDEN("{:?}"), in); |
| 406 | VERIFY( res == WIDEN(R"('a')") ); |
| 407 | } |
| 408 | |
| 409 | template<typename CharT> |
| 410 | void |
| 411 | test_formatters() |
| 412 | { |
| 413 | test_formatter_char<CharT, CharT>(); |
| 414 | test_formatter_str<CharT, CharT*>(); |
| 415 | test_formatter_str<CharT, const CharT*>(); |
| 416 | test_formatter_str<CharT, std::basic_string<CharT>>(); |
| 417 | test_formatter_str<CharT, std::basic_string_view<CharT>>(); |
| 418 | test_formatter_arr<CharT>(); |
| 419 | } |
| 420 | |
| 421 | void |
| 422 | test_formatters_c() |
| 423 | { |
| 424 | test_formatters<char>(); |
| 425 | test_formatters<wchar_t>(); |
| 426 | test_formatter_char<wchar_t, char>(); |
| 427 | } |
| 428 | |
| 429 | int main() |
| 430 | { |
| 431 | test_basic_escapes<char>(); |
| 432 | test_basic_escapes<wchar_t>(); |
| 433 | test_ascii_escapes<char>(); |
| 434 | test_ascii_escapes<wchar_t>(); |
| 435 | test_extended_ascii<char>(); |
| 436 | test_extended_ascii<wchar_t>(); |
| 437 | |
| 438 | #if UNICODE_ENC |
| 439 | test_unicode_escapes<char>(); |
| 440 | test_unicode_escapes<wchar_t>(); |
| 441 | test_grapheme_extend<char>(); |
| 442 | test_grapheme_extend<wchar_t>(); |
| 443 | test_replacement_char<char>(); |
| 444 | test_replacement_char<wchar_t>(); |
| 445 | test_ill_formed_utf8_seq(); |
| 446 | test_ill_formed_utf32(); |
| 447 | #endif // UNICODE_ENC |
| 448 | |
| 449 | test_fill<char>(); |
| 450 | test_fill<wchar_t>(); |
| 451 | test_prec<char>(); |
| 452 | test_prec<wchar_t>(); |
| 453 | |
| 454 | test_formatters_c(); |
| 455 | } |