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