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-localization
11 // UNSUPPORTED: libcpp-has-no-incomplete-format
12 // TODO FMT Evaluate gcc-11 status
13 // UNSUPPORTED: gcc-11
14 
15 // <format>
16 
17 // string vformat(const locale& loc, string_view fmt, format_args args);
18 // wstring vformat(const locale& loc, 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 
27 auto test = []<class CharT, class... Args>(std::basic_string<CharT> expected,
28                                            std::basic_string<CharT> fmt,
29                                            const Args&... args) {
30   std::basic_string<CharT> out = std::vformat(
31       std::locale(), fmt, std::make_format_args<context_t<CharT>>(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     (void) std::vformat(std::locale(), fmt,
40                         std::make_format_args<context_t<CharT>>(args...));
41     assert(false);
42   } catch ([[maybe_unused]] std::format_error& e) {
43     LIBCPP_ASSERT(e.what() == what);
44     return;
45   }
46   assert(false);
47 #endif
48   (void)what;
49   (void)fmt;
50   (void)sizeof...(args);
51 };
52 
53 int main(int, char**) {
54   format_tests<char>(test, test_exception);
55 
56 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
57   format_tests_char_to_wchar_t(test);
58   format_tests<wchar_t>(test, test_exception);
59 #endif
60 
61   return 0;
62 }
63