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 
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 
13 // XFAIL: availability-fp_to_chars-missing
14 
15 // <vector>
16 
17 // template<class T, class charT>
18 //   requires is-vector-bool-reference<T>
19 // struct formatter<T, charT>
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 "format.functions.tests.h"
28 #include "test_macros.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   format_tests<char>(test, test_exception);
58 
59 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
60   format_tests<wchar_t>(test, test_exception);
61 #endif
62 
63   return 0;
64 }
65