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