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