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