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