Debian Codegraph @

libcxx/test/std/utilities/format/format.functions/format.runtime_format.pass.cpp

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