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