xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/vformat.pass.cpp (revision d2baefae6846765eef6a6dd69d4fdf1082ce29ad)
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-11 status
11 // UNSUPPORTED: gcc-11
12 
13 // <format>
14 
15 // string vformat(string_view fmt, format_args args);
16 // wstring vformat(wstring_view fmt, wformat_args args);
17 
18 #include <format>
19 #include <cassert>
20 #include <vector>
21 
22 #include "test_macros.h"
23 #include "format_tests.h"
24 
25 auto test = []<class CharT, class... Args>(std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt,
26                                            const Args&... args) {
27   std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
28   assert(out == expected);
29 };
30 
31 auto test_exception = []<class CharT, class... Args>(std::string_view what, std::basic_string_view<CharT> fmt,
32                                                      const Args&... args) {
33 #ifndef TEST_HAS_NO_EXCEPTIONS
34   try {
35     TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
36     assert(false);
37   } catch ([[maybe_unused]] const std::format_error& e) {
38     LIBCPP_ASSERT(e.what() == what);
39     return;
40   }
41   assert(false);
42 #endif
43   (void)what;
44   (void)fmt;
45   (void)sizeof...(args);
46 };
47 
48 int main(int, char**) {
49   format_tests<char>(test, test_exception);
50 
51 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
52   format_tests_char_to_wchar_t(test);
53   format_tests<wchar_t>(test, test_exception);
54 #endif
55 
56   return 0;
57 }
58