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