xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/vformat.pass.cpp (revision ff8a4766ac256e3361ef66c0765ef5b041f4dd5f)
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, std::basic_string<CharT> fmt,
27                                            const Args&... args) {
28   std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
29   assert(out == expected);
30 };
31 
32 auto test_exception =
33     []<class CharT, class... Args>(std::string_view what, std::basic_string<CharT> fmt, const Args&... args) {
34 #ifndef TEST_HAS_NO_EXCEPTIONS
35   try {
36     TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
37     assert(false);
38   } catch ([[maybe_unused]] std::format_error& e) {
39     LIBCPP_ASSERT(e.what() == what);
40     return;
41   }
42   assert(false);
43 #endif
44   (void)what;
45   (void)fmt;
46   (void)sizeof...(args);
47 };
48 
49 int main(int, char**) {
50   format_tests<char>(test, test_exception);
51 
52 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
53   format_tests_char_to_wchar_t(test);
54   format_tests<wchar_t>(test, test_exception);
55 #endif
56 
57   return 0;
58 }
59