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-incomplete-format
11 // TODO FMT Evaluate gcc-11 status
12 // UNSUPPORTED: gcc-11
13 
14 // Note this formatter shows additional information when tests are failing.
15 // This aids the development. Since other formatters fail in the same fashion
16 // they don't have this additional output.
17 
18 // <format>
19 
20 // template<class... Args>
21 //   string format(string_view fmt, const Args&... args);
22 // template<class... Args>
23 //   wstring format(wstring_view fmt, const Args&... args);
24 
25 #include <format>
26 #include <cassert>
27 #include <vector>
28 
29 #include "test_macros.h"
30 #include "format_tests.h"
31 
32 #ifndef TEST_HAS_NO_LOCALIZATION
33 #  include <iostream>
34 #endif
35 
36 auto test = []<class CharT, class... Args>(std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt,
37                                            const Args&... args) {
38   std::basic_string<CharT> out = std::format(fmt, args...);
39 #ifndef TEST_HAS_NO_LOCALIZATION
40   if constexpr (std::same_as<CharT, char>)
41     if (out != expected)
42       std::cerr << "\nFormat string   " << fmt << "\nExpected output " << expected << "\nActual output   " << out
43                 << '\n';
44 #endif
45   assert(out == expected);
46 };
47 
48 auto test_exception = []<class CharT, class... Args>(std::string_view what, std::basic_string_view<CharT> fmt,
49                                                      const Args&... args) {
50 #ifndef TEST_HAS_NO_EXCEPTIONS
51   try {
52     std::format(fmt, args...);
53 #  ifndef TEST_HAS_NO_LOCALIZATION
54     if constexpr (std::same_as<CharT, char>)
55       std::cerr << "\nFormat string   " << fmt << "\nDidn't throw an exception.\n";
56 #  endif
57     assert(false);
58   } catch (const std::format_error& e) {
59     if constexpr (std::same_as<CharT, char>)
60 #  if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_LOCALIZATION)
61       if (e.what() != what)
62         std::cerr << "\nFormat string   " << fmt << "\nExpected exception " << what << "\nActual exception   "
63                   << e.what() << '\n';
64 #  endif
65     LIBCPP_ASSERT(e.what() == what);
66     return;
67   }
68   assert(false);
69 #endif
70   (void)what;
71   (void)fmt;
72   (void)sizeof...(args);
73 };
74 
75 int main(int, char**) {
76   format_tests<char>(test, test_exception);
77 
78 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
79   format_tests_char_to_wchar_t(test);
80   format_tests<wchar_t>(test, test_exception);
81 #endif
82 
83   return 0;
84 }
85