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 9 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME 10 11 // This version runs the test when the platform has Unicode support. 12 // UNSUPPORTED: libcpp-has-no-unicode 13 14 // XFAIL: availability-fp_to_chars-missing 15 16 // <format> 17 18 // The paper 19 // P2572R1 std::format fill character allowances 20 // adds support for Unicode Scalar Values as fill character. 21 22 #include <format> 23 24 #include "assert_macros.h" 25 #include "concat_macros.h" 26 #include "format.functions.common.h" 27 #include "make_string.h" 28 #include "string_literal.h" 29 #include "test_format_string.h" 30 #include "test_macros.h" 31 32 #define SV(S) MAKE_STRING_VIEW(CharT, S) 33 34 auto check = []<class CharT, class... Args>( 35 std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) { 36 std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...); 37 TEST_REQUIRE(out == expected, 38 TEST_WRITE_CONCATENATED( 39 "\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n')); 40 }; 41 42 auto check_exception = 43 []<class CharT, class... Args>( 44 [[maybe_unused]] std::string_view what, 45 [[maybe_unused]] std::basic_string_view<CharT> fmt, 46 [[maybe_unused]] Args&&... args) { 47 TEST_VALIDATE_EXCEPTION( 48 std::format_error, 49 [&]([[maybe_unused]] const std::format_error& e) { 50 TEST_LIBCPP_REQUIRE( 51 e.what() == what, 52 TEST_WRITE_CONCATENATED( 53 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); 54 }, 55 TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...))); 56 }; 57 58 template <class CharT> 59 void test() { 60 // 1, 2, 3, 4 code unit UTF-8 transitions 61 check(SV("\u000042\u0000"), SV("{:\u0000^4}"), 42); 62 check(SV("\u007f42\u007f"), SV("{:\u007f^4}"), 42); 63 check(SV("\u008042\u0080"), SV("{:\u0080^4}"), 42); 64 check(SV("\u07ff42\u07ff"), SV("{:\u07ff^4}"), 42); 65 check(SV("\u080042\u0800"), SV("{:\u0800^4}"), 42); 66 check(SV("\uffff42\uffff"), SV("{:\uffff^4}"), 42); 67 check(SV("\U0010000042\U00100000"), SV("{:\U00100000^4}"), 42); 68 check(SV("\U0010ffff42\U0010ffff"), SV("{:\U0010ffff^4}"), 42); 69 70 // Examples of P2572R1 71 check(SV("x"), SV("{:^6}"), SV("x")); 72 check(SV(""), SV("{:*^6}"), SV("")); 73 check(SV("12345678"), SV("{:*>6}"), SV("12345678")); 74 75 // Invalid Unicode Scalar Values 76 if constexpr (std::same_as<CharT, char>) { 77 check_exception("The format-spec contains malformed Unicode characters", SV("{:\xed\xa0\x80^}"), 42); // U+D800 78 check_exception("The format-spec contains malformed Unicode characters", SV("{:\xed\xa0\xbf^}"), 42); // U+DBFF 79 check_exception("The format-spec contains malformed Unicode characters", SV("{:\xed\xbf\x80^}"), 42); // U+DC00 80 check_exception("The format-spec contains malformed Unicode characters", SV("{:\xed\xbf\xbf^}"), 42); // U+DFFF 81 82 check_exception( 83 "The format-spec contains malformed Unicode characters", SV("{:\xf4\x90\x80\x80^}"), 42); // U+110000 84 check_exception( 85 "The format-spec contains malformed Unicode characters", SV("{:\xf4\x90\xbf\xbf^}"), 42); // U+11FFFF 86 87 check_exception("The format-spec contains malformed Unicode characters", 88 SV("{:\x80^}"), 89 42); // Trailing code unit with no leading one. 90 check_exception( 91 "The format-spec contains malformed Unicode characters", SV("{:\xc0^}"), 42); // Missing trailing code unit. 92 check_exception( 93 "The format-spec contains malformed Unicode characters", SV("{:\xe0\x80^}"), 42); // Missing trailing code unit. 94 check_exception("The format-spec contains malformed Unicode characters", 95 SV("{:\xf0\x80^}"), 96 42); // Missing two trailing code units. 97 check_exception("The format-spec contains malformed Unicode characters", 98 SV("{:\xf0\x80\x80^}"), 99 42); // Missing trailing code unit. 100 101 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 102 } else { 103 # ifdef TEST_SHORT_WCHAR 104 check_exception("The format-spec contains malformed Unicode characters", std::wstring_view{L"{:\xd800^}"}, 42); 105 check_exception("The format-spec contains malformed Unicode characters", std::wstring_view{L"{:\xdbff^}"}, 42); 106 check_exception("The format-spec contains malformed Unicode characters", std::wstring_view{L"{:\xdc00^}"}, 42); 107 check_exception("The format-spec contains malformed Unicode characters", std::wstring_view{L"{:\xddff^}"}, 42); 108 109 check_exception("The format-spec contains malformed Unicode characters", 110 std::wstring_view{L"{:\xdc00\xd800^}"}, 111 42); // Reverted surrogates. 112 113 # else // TEST_SHORT_WCHAR 114 check_exception("The fill character contains an invalid value", std::wstring_view{L"{:\xd800^}"}, 42); 115 check_exception("The fill character contains an invalid value", std::wstring_view{L"{:\xdbff^}"}, 42); 116 check_exception("The fill character contains an invalid value", std::wstring_view{L"{:\xdc00^}"}, 42); 117 check_exception("The fill character contains an invalid value", std::wstring_view{L"{:\xddff^}"}, 42); 118 119 check_exception( 120 "The format-spec should consume the input or end with a '}'", std::wstring_view{L"{:\xdc00\xd800^}"}, 42); 121 122 check_exception("The fill character contains an invalid value", std::wstring_view{L"{:\x00110000^}"}, 42); 123 check_exception("The fill character contains an invalid value", std::wstring_view{L"{:\x0011ffff^}"}, 42); 124 # endif // TEST_SHORT_WCHAR 125 #endif // TEST_HAS_NO_WIDE_CHARACTERS 126 } 127 } 128 129 int main(int, char**) { 130 test<char>(); 131 132 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 133 test<wchar_t>(); 134 #endif 135 136 return 0; 137 } 138