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 10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME 11 12 // XFAIL: availability-fp_to_chars-missing 13 14 // <format> 15 16 // string vformat(string_view fmt, format_args args); 17 // wstring vformat(wstring_view fmt, wformat_args args); 18 19 #include <format> 20 #include <cassert> 21 #include <vector> 22 23 #include "test_macros.h" 24 #include "format_tests.h" 25 #include "string_literal.h" 26 #include "assert_macros.h" 27 #include "concat_macros.h" 28 29 auto test = []<class CharT, class... Args>( 30 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr { 31 std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...)); 32 TEST_REQUIRE(out == expected, 33 TEST_WRITE_CONCATENATED( 34 "\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n')); 35 }; 36 37 auto test_exception = 38 []<class CharT, class... Args>( 39 [[maybe_unused]] std::string_view what, 40 [[maybe_unused]] std::basic_string_view<CharT> fmt, 41 [[maybe_unused]] Args&&... args) { 42 TEST_VALIDATE_EXCEPTION( 43 std::format_error, 44 [&]([[maybe_unused]] const std::format_error& e) { 45 TEST_LIBCPP_REQUIRE( 46 e.what() == what, 47 TEST_WRITE_CONCATENATED( 48 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); 49 }, 50 TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...))); 51 }; 52 53 int main(int, char**) { 54 #if !defined(TEST_HAS_NO_EXCEPTIONS) 55 // reproducer of https://llvm.org/PR65011 56 try { 57 const char fmt[] = {'{', '0'}; 58 char buf[4096]; 59 [[maybe_unused]] auto ignored = 60 std::vformat_to(buf, std::string_view{fmt, fmt + sizeof(fmt)}, std::make_format_args()); 61 } catch (...) { 62 } 63 #endif // !defined(TEST_HAS_NO_EXCEPTIONS) 64 65 format_tests<char, execution_modus::full>(test, test_exception); 66 67 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 68 format_tests_char_to_wchar_t(test); 69 format_tests<wchar_t, execution_modus::full>(test, test_exception); 70 #endif 71 72 return 0; 73 } 74