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