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 10 // UNSUPPORTED: no-threads 11 12 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME 13 14 // TODO FMT This test should not require std::to_chars(floating-point) 15 // XFAIL: availability-fp_to_chars-missing 16 17 // <thread> 18 19 // template<class charT> 20 // struct formatter<thread::id, charT>; 21 22 // string vformat(string_view fmt, format_args args); 23 // wstring vformat(wstring_view fmt, wformat_args args); 24 25 #include <format> 26 #include <cassert> 27 28 #include "assert_macros.h" 29 #include "concat_macros.h" 30 #include "format.functions.tests.h" 31 #include "test_macros.h" 32 33 auto test = []<class CharT, class... Args>( 34 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) { 35 std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...)); 36 TEST_REQUIRE(out == expected, 37 TEST_WRITE_CONCATENATED( 38 "\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n')); 39 }; 40 41 auto test_exception = 42 []<class CharT, class... Args>( 43 [[maybe_unused]] std::string_view what, 44 [[maybe_unused]] std::basic_string_view<CharT> fmt, 45 [[maybe_unused]] Args&&... args) { 46 TEST_VALIDATE_EXCEPTION( 47 std::format_error, 48 [&]([[maybe_unused]] const std::format_error& e) { 49 TEST_LIBCPP_REQUIRE( 50 e.what() == what, 51 TEST_WRITE_CONCATENATED( 52 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); 53 }, 54 TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...))); 55 }; 56 57 int main(int, char**) { 58 format_tests<char>(test, test_exception); 59 60 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 61 format_tests<wchar_t>(test, test_exception); 62 #endif 63 64 return 0; 65 } 66