Revert "[libc] Breakup freelist_malloc into separate files" (#119749)
[llvm-project.git] / libcxx / test / std / utilities / format / format.tuple / format.pass.cpp
blobc4fd45f528133e6fb0edc39954a9f3cdf74b7533
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
13 // <format>
15 // template<class charT, formattable<charT>... Ts>
16 // struct formatter<pair-or-tuple<Ts...>, charT>
18 // template<class FormatContext>
19 // typename FormatContext::iterator
20 // format(see below& elems, FormatContext& ctx) const;
22 // Note this tests the basics of this function. It's tested in more detail in
23 // the format functions tests.
25 #include <cassert>
26 #include <concepts>
27 #include <format>
28 #include <iterator>
29 #include <tuple>
30 #include <utility>
32 #include "assert_macros.h"
33 #include "format.functions.common.h"
34 #include "make_string.h"
35 #include "test_format_context.h"
36 #include "test_macros.h"
38 #define SV(S) MAKE_STRING_VIEW(CharT, S)
40 template <class StringViewT, class Arg>
41 void test(StringViewT expected, Arg arg) {
42 using CharT = typename StringViewT::value_type;
43 using String = std::basic_string<CharT>;
44 using OutIt = std::back_insert_iterator<String>;
45 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
47 const std::formatter<Arg, CharT> formatter;
49 String result;
50 OutIt out = std::back_inserter(result);
51 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
52 formatter.format(arg, format_ctx);
53 assert(result == expected);
56 template <class CharT>
57 void test_assure_parse_is_called(std::basic_string_view<CharT> fmt) {
58 using String = std::basic_string<CharT>;
59 using OutIt = std::back_insert_iterator<String>;
60 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
61 std::pair<parse_call_validator, parse_call_validator> arg;
63 String result;
64 OutIt out = std::back_inserter(result);
65 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
67 std::formatter<decltype(arg), CharT> formatter;
68 std::basic_format_parse_context<CharT> ctx{fmt};
70 formatter.parse(ctx);
71 formatter.format(arg, format_ctx);
74 template <class CharT>
75 void test_assure_parse_is_called() {
76 using String = std::basic_string<CharT>;
77 using OutIt = std::back_insert_iterator<String>;
78 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
79 std::pair<parse_call_validator, parse_call_validator> arg;
81 String result;
82 OutIt out = std::back_inserter(result);
83 [[maybe_unused]] FormatCtxT format_ctx =
84 test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
86 { // parse not called
87 [[maybe_unused]] const std::formatter<decltype(arg), CharT> formatter;
88 TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx));
91 test_assure_parse_is_called(SV("5"));
92 test_assure_parse_is_called(SV("n"));
93 test_assure_parse_is_called(SV("m"));
94 test_assure_parse_is_called(SV("5n"));
95 test_assure_parse_is_called(SV("5m"));
98 template <class CharT>
99 void test() {
100 test(SV("(1)"), std::tuple<int>{1});
101 test(SV("(1, 1)"), std::tuple<int, CharT>{1, CharT('1')});
102 test(SV("(1, 1)"), std::pair<int, CharT>{1, CharT('1')});
103 test(SV("(1, 1, true)"), std::tuple<int, CharT, bool>{1, CharT('1'), true});
105 test_assure_parse_is_called<CharT>();
108 void test() {
109 test<char>();
110 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
111 test<wchar_t>();
112 #endif
115 int main(int, char**) {
116 test();
118 return 0;