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, c++20 |
9 | |
10 | // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
11 | |
12 | // <format> |
13 | |
14 | // template<class charT, formattable<charT>... Ts> |
15 | // struct formatter<pair-or-tuple<Ts...>, charT> |
16 | |
17 | // template<class FormatContext> |
18 | // typename FormatContext::iterator |
19 | // format(see below& elems, FormatContext& ctx) const; |
20 | |
21 | // Note this tests the basics of this function. It's tested in more detail in |
22 | // the format functions tests. |
23 | |
24 | #include <cassert> |
25 | #include <concepts> |
26 | #include <format> |
27 | #include <iterator> |
28 | #include <tuple> |
29 | #include <utility> |
30 | |
31 | #include "assert_macros.h" |
32 | #include "format.functions.common.h" |
33 | #include "make_string.h" |
34 | #include "test_format_context.h" |
35 | #include "test_macros.h" |
36 | |
37 | #define SV(S) MAKE_STRING_VIEW(CharT, S) |
38 | |
39 | template <class StringViewT, class Arg> |
40 | void test(StringViewT expected, Arg arg) { |
41 | using CharT = typename StringViewT::value_type; |
42 | using String = std::basic_string<CharT>; |
43 | using OutIt = std::back_insert_iterator<String>; |
44 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
45 | |
46 | const std::formatter<Arg, CharT> formatter; |
47 | |
48 | String result; |
49 | OutIt out = std::back_inserter(result); |
50 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
51 | formatter.format(arg, format_ctx); |
52 | assert(result == expected); |
53 | } |
54 | |
55 | template <class CharT> |
56 | void test_assure_parse_is_called(std::basic_string_view<CharT> fmt) { |
57 | using String = std::basic_string<CharT>; |
58 | using OutIt = std::back_insert_iterator<String>; |
59 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
60 | std::pair<parse_call_validator, parse_call_validator> arg; |
61 | |
62 | String result; |
63 | OutIt out = std::back_inserter(result); |
64 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
65 | |
66 | std::formatter<decltype(arg), CharT> formatter; |
67 | std::basic_format_parse_context<CharT> ctx{fmt}; |
68 | |
69 | formatter.parse(ctx); |
70 | formatter.format(arg, format_ctx); |
71 | } |
72 | |
73 | template <class CharT> |
74 | void test_assure_parse_is_called() { |
75 | using String = std::basic_string<CharT>; |
76 | using OutIt = std::back_insert_iterator<String>; |
77 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
78 | std::pair<parse_call_validator, parse_call_validator> arg; |
79 | |
80 | String result; |
81 | OutIt out = std::back_inserter(result); |
82 | [[maybe_unused]] FormatCtxT format_ctx = |
83 | test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
84 | |
85 | { // parse not called |
86 | [[maybe_unused]] const std::formatter<decltype(arg), CharT> formatter; |
87 | TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx)); |
88 | } |
89 | |
90 | test_assure_parse_is_called(SV("5" )); |
91 | test_assure_parse_is_called(SV("n" )); |
92 | test_assure_parse_is_called(SV("m" )); |
93 | test_assure_parse_is_called(SV("5n" )); |
94 | test_assure_parse_is_called(SV("5m" )); |
95 | } |
96 | |
97 | template <class CharT> |
98 | void test() { |
99 | test(SV("(1)" ), std::tuple<int>{1}); |
100 | test(SV("(1, 1)" ), std::tuple<int, CharT>{1, CharT('1')}); |
101 | test(SV("(1, 1)" ), std::pair<int, CharT>{1, CharT('1')}); |
102 | test(SV("(1, 1, true)" ), std::tuple<int, CharT, bool>{1, CharT('1'), true}); |
103 | |
104 | test_assure_parse_is_called<CharT>(); |
105 | } |
106 | |
107 | void test() { |
108 | test<char>(); |
109 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
110 | test<wchar_t>(); |
111 | #endif |
112 | } |
113 | |
114 | int main(int, char**) { |
115 | test(); |
116 | |
117 | return 0; |
118 | } |
119 | |