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