xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/vformat.pass.cpp (revision 3d334df58742ff53fb00aa3caeb7eb5da3436348)
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: libcpp-has-no-incomplete-format
10 // TODO FMT Evaluate gcc-12 status
11 // UNSUPPORTED: gcc-12
12 
13 // This test requires std::to_chars(floating-point), which is in the dylib
14 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0}}
15 
16 // <format>
17 
18 // string vformat(string_view fmt, format_args args);
19 // wstring vformat(wstring_view fmt, wformat_args args);
20 
21 #include <format>
22 #include <cassert>
23 #include <vector>
24 
25 #include "test_macros.h"
26 #include "format_tests.h"
27 #include "string_literal.h"
28 #include "assert_macros.h"
29 #include "concat_macros.h"
30 
31 auto test = []<class CharT, class... Args>(
32                 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr {
33   std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
34   TEST_REQUIRE(out == expected,
35                TEST_WRITE_CONCATENATED(
36                    "\nFormat string   ", fmt, "\nExpected output ", expected, "\nActual output   ", out, '\n'));
37 };
38 
39 auto test_exception =
40     []<class CharT, class... Args>(
41         [[maybe_unused]] std::string_view what,
42         [[maybe_unused]] std::basic_string_view<CharT> fmt,
43         [[maybe_unused]] Args&&... args) {
44       TEST_VALIDATE_EXCEPTION(
45           std::format_error,
46           [&]([[maybe_unused]] const std::format_error& e) {
47             TEST_LIBCPP_REQUIRE(
48                 e.what() == what,
49                 TEST_WRITE_CONCATENATED(
50                     "\nFormat string   ", fmt, "\nExpected exception ", what, "\nActual exception   ", e.what(), '\n'));
51           },
52           TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...)));
53     };
54 
55 int main(int, char**) {
56   format_tests<char, execution_modus::full>(test, test_exception);
57 
58 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
59   format_tests_char_to_wchar_t(test);
60   format_tests<wchar_t, execution_modus::full>(test, test_exception);
61 #endif
62 
63   return 0;
64 }
65