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_view<CharT> expected, std::basic_string_view<CharT> fmt,
28                                            const Args&... args) {
29   std::basic_string<CharT> out = std::vformat(std::locale(), fmt, std::make_format_args<context_t<CharT>>(args...));
30   assert(out == expected);
31 };
32 
33 auto test_exception = []<class CharT, class... Args>(std::string_view what, std::basic_string_view<CharT> fmt,
34                                                      const Args&... args) {
35 #ifndef TEST_HAS_NO_EXCEPTIONS
36   try {
37     (void)std::vformat(std::locale(), fmt, std::make_format_args<context_t<CharT>>(args...));
38     assert(false);
39   } catch ([[maybe_unused]] const std::format_error& e) {
40     LIBCPP_ASSERT(e.what() == what);
41     return;
42   }
43   assert(false);
44 #endif
45   (void)what;
46   (void)fmt;
47   (void)sizeof...(args);
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