1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
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.
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
;
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
;
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
};
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
;
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
));
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
>
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
>();
110 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
115 int main(int, char**) {