xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/format.pass.cpp (revision 3d334df58742ff53fb00aa3caeb7eb5da3436348)
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-has-no-incomplete-format
10 // TODO FMT Evaluate gcc-12 status
11 // UNSUPPORTED: gcc-12
12 
13 // This test requires std::to_chars(floating-point), which is in the dylib
14 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0}}
15 
16 // Note this formatter shows additional information when tests are failing.
17 // This aids the development. Since other formatters fail in the same fashion
18 // they don't have this additional output.
19 
20 // <format>
21 
22 // template<class... Args>
23 //   string format(format-string<Args...> fmt, const Args&... args);
24 // template<class... Args>
25 //   wstring format(wformat-string<Args...> fmt, const Args&... args);
26 
27 #include <format>
28 #include <cassert>
29 #include <vector>
30 
31 #include "test_macros.h"
32 #include "format_tests.h"
33 #include "string_literal.h"
34 #include "test_format_string.h"
35 #include "assert_macros.h"
36 #include "concat_macros.h"
37 
38 auto test =
39     []<class CharT, class... Args>(
40         std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
41       std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...);
42       TEST_REQUIRE(
43           out == expected,
44           TEST_WRITE_CONCATENATED(
45               "\nFormat string   ", fmt.get(), "\nExpected output ", expected, "\nActual output   ", out, '\n'));
46     };
47 
48 auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) {
49   // After P2216 most exceptions thrown by std::format become ill-formed.
50   // Therefore this tests does nothing.
51   // A basic ill-formed test is done in format.verify.cpp
52   // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument.
53 };
54 
55 int main(int, char**) {
56   format_tests<char, execution_modus::full>(test, test_exception);
57 
58 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
59   format_tests_char_to_wchar_t(test);
60   format_tests<wchar_t, execution_modus::full>(test, test_exception);
61 #endif
62 
63   return 0;
64 }
65