1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 10 // UNSUPPORTED: no-localization 11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME 12 13 // XFAIL: availability-fp_to_chars-missing 14 15 // <format> 16 17 // Tests the behavior of 18 // 19 // runtime-format-string<char> runtime_format(string_view fmt) noexcept; 20 // runtime-format-string<wchar_t> runtime_format(wstring_view fmt) noexcept; 21 // 22 // and 23 // 24 // template<class charT, class... Args> 25 // struct basic_format_string { 26 // ... 27 // basic_format_string(runtime-format-string<charT> s) noexcept : str(s.str) {} 28 // ... 29 // } 30 // 31 // This is done by testing it in the top-level functions: 32 // 33 // template<class... Args> 34 // string format(const locale& loc, format_string<Args...> fmt, Args&&... args); 35 // template<class... Args> 36 // wstring format(const locale& loc, wformat_string<Args...> fmt, Args&&... args); 37 // 38 // The basics of runtime_format and basic_format_string's constructor are tested in 39 // - libcxx/test/std/utilities/format/format.syn/runtime_format_string.pass.cpp 40 // - libcxx/test/std/utilities/format/format.fmt.string/ctor.runtime-format-string.pass.cpp 41 42 #include <format> 43 #include <cassert> 44 #include <locale> 45 #include <vector> 46 47 #include "test_macros.h" 48 #include "format_tests.h" 49 #include "string_literal.h" 50 #include "assert_macros.h" 51 #include "concat_macros.h" 52 53 auto test = []<class CharT, class... Args>( 54 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr { 55 std::basic_string<CharT> out = std::format(std::locale(), std::runtime_format(fmt), std::forward<Args>(args)...); 56 TEST_REQUIRE(out == expected, 57 TEST_WRITE_CONCATENATED( 58 "\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n')); 59 }; 60 61 auto test_exception = 62 []<class CharT, class... Args>( 63 [[maybe_unused]] std::string_view what, 64 [[maybe_unused]] std::basic_string_view<CharT> fmt, 65 [[maybe_unused]] Args&&... args) { 66 TEST_VALIDATE_EXCEPTION( 67 std::format_error, 68 [&]([[maybe_unused]] const std::format_error& e) { 69 TEST_LIBCPP_REQUIRE( 70 e.what() == what, 71 TEST_WRITE_CONCATENATED( 72 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); 73 }, 74 TEST_IGNORE_NODISCARD std::format(std::locale(), std::runtime_format(fmt), std::forward<Args>(args)...)); 75 }; 76 77 int main(int, char**) { 78 format_tests<char, execution_modus::partial>(test, test_exception); 79 80 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 81 format_tests_char_to_wchar_t(test); 82 format_tests<wchar_t, execution_modus::partial>(test, test_exception); 83 #endif 84 85 return 0; 86 } 87