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