xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/vformat.pass.cpp (revision 396fbe264f5a43a57241043f66c1df3c526bc1ca)
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-12 status
11 // UNSUPPORTED: gcc-12
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 #include "string_literal.h"
25 auto test = []<class CharT, class... Args>(
26                 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr {
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 =
32     []<class CharT, class... Args>(
33         [[maybe_unused]] std::string_view what,
34         [[maybe_unused]] std::basic_string_view<CharT> fmt,
35         [[maybe_unused]] 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]] const std::format_error& e) {
41         LIBCPP_ASSERT(e.what() == what);
42         return;
43       }
44       assert(false);
45 #endif
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