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-localization
10 // UNSUPPORTED: libcpp-has-no-incomplete-format
11 // TODO FMT Evaluate gcc-11 status
12 // UNSUPPORTED: gcc-11
13 
14 // <format>
15 
16 // template<class... Args>
17 //   string format(const locale& loc, string_view fmt, const Args&... args);
18 // template<class... Args>
19 //   wstring format(const locale& loc, wstring_view fmt, const Args&... args);
20 
21 #include <format>
22 #include <cassert>
23 #include <iostream>
24 #include <vector>
25 
26 #include "test_macros.h"
27 #include "format_tests.h"
28 
29 auto test = []<class CharT, class... Args>(std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt,
30                                            const Args&... args) {
31   std::basic_string<CharT> out = std::format(std::locale(), fmt, args...);
32   if constexpr (std::same_as<CharT, char>)
33     if (out != expected)
34       std::cerr << "\nFormat string   " << fmt << "\nExpected output " << expected << "\nActual output   " << out
35                 << '\n';
36   assert(out == expected);
37 };
38 
39 auto test_exception = []<class CharT, class... Args>(std::string_view what, std::basic_string_view<CharT> fmt,
40                                                      const Args&... args) {
41 #ifndef TEST_HAS_NO_EXCEPTIONS
42   try {
43     std::format(std::locale(), fmt, args...);
44     if constexpr (std::same_as<CharT, char>)
45       std::cerr << "\nFormat string   " << fmt << "\nDidn't throw an exception.\n";
46     assert(false);
47   } catch (const std::format_error& e) {
48 #  ifdef _LIBCPP_VERSION
49     if constexpr (std::same_as<CharT, char>)
50       if (e.what() != what)
51         std::cerr << "\nFormat string   " << fmt << "\nExpected exception " << what << "\nActual exception   "
52                   << e.what() << '\n';
53 #  endif
54     LIBCPP_ASSERT(e.what() == what);
55     return;
56   }
57   assert(false);
58 #else
59   (void)what;
60   (void)fmt;
61   (void)sizeof...(args);
62 #endif
63 };
64 
65 int main(int, char**) {
66   format_tests<char>(test, test_exception);
67 
68 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
69   format_tests_char_to_wchar_t(test);
70   format_tests<wchar_t>(test, test_exception);
71 #endif
72 
73   return 0;
74 }
75