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