Debian Codegraph @

libstdc++-v3/testsuite/std/format/tuple.cc

raw
1 // { dg-do run { target c++23 } }
2
3 #include <format>
4 #include <string>
5 #include <testsuite_hooks.h>
6 #include <tuple>
7 #include <utility>
8
9 struct NotFormattable
10 {};
11
12 static_assert( !std::formattable<std::pair<int, NotFormattable>, char> );
13 static_assert( !std::formattable<std::tuple<int, NotFormattable, int>, wchar_t> );
14
15 template<typename... Args>
16 bool
17 is_format_string_for(const char* str, Args&&... args)
18 {
19 try {
20 (void) std::vformat(str, std::make_format_args(args...));
21 return true;
22 } catch (const std::format_error&) {
23 return false;
24 }
25 }
26
27 template<typename... Args>
28 bool
29 is_format_string_for(const wchar_t* str, Args&&... args)
30 {
31 try {
32 (void) std::vformat(str, std::make_wformat_args(args...));
33 return true;
34 } catch (const std::format_error&) {
35 return false;
36 }
37 }
38
39 #define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
40 #define WIDEN(S) WIDEN_(_CharT, S)
41
42 void
43 test_format_string()
44 {
45 // invalid format stringss
46 VERIFY( !is_format_string_for("{:p}", std::tuple<>()) );
47 VERIFY( !is_format_string_for("{:nm}", std::tuple<>()) );
48
49 // 'm' is only valid for 2 elemenst
50 VERIFY( !is_format_string_for("{:m}", std::tuple<>()) );
51 VERIFY( !is_format_string_for("{:m}", std::tuple<int, int, int>()) );
52
53 // element specifier is not supported
54 VERIFY( !is_format_string_for("{::}", std::tuple<>()) );
55
56 // precision is not supported
57 VERIFY( !is_format_string_for("{:.10}", std::tuple<>()) );
58
59 // width needs to be integer type
60 VERIFY( !is_format_string_for("{:{}}", std::tuple<>(), 1.0f) );
61 }
62
63 template<typename _CharT>
64 void test_multi()
65 {
66 using Sv = std::basic_string_view<_CharT>;
67 using Str = std::basic_string<_CharT>;
68
69 std::basic_string<_CharT> res;
70 std::size_t size = 0;
71 std::tuple<int, Str, float> t1(1, WIDEN("test"), 2.1);
72
73 res = std::format(WIDEN("{}"), t1);
74 VERIFY( res == WIDEN(R"((1, "test", 2.1))") );
75 res = std::format(WIDEN("{:}"), t1);
76 VERIFY( res == WIDEN(R"((1, "test", 2.1))") );
77 res = std::format(WIDEN("{:n}"), t1);
78 VERIFY( res == WIDEN(R"(1, "test", 2.1)") );
79
80 res = std::format(WIDEN("{:3}"), t1);
81 VERIFY( res == WIDEN(R"((1, "test", 2.1))") );
82
83 res = std::format(WIDEN("{:20}"), t1);
84 VERIFY( res == WIDEN(R"((1, "test", 2.1) )") );
85
86 res = std::format(WIDEN("{:{}}"), t1, 20);
87 VERIFY( res == WIDEN(R"((1, "test", 2.1) )") );
88
89 res = std::format(WIDEN("{1:{0}}"), 20, t1);
90 VERIFY( res == WIDEN(R"((1, "test", 2.1) )") );
91
92 res = std::format(WIDEN("{:^>17}"), t1);
93 VERIFY( res == WIDEN(R"(^(1, "test", 2.1))") );
94
95 res = std::format(WIDEN("{:$<18}"), t1);
96 VERIFY( res == WIDEN(R"((1, "test", 2.1)$$)") );
97
98 res = std::format(WIDEN("{:+^19}"), t1);
99 VERIFY( res == WIDEN(R"(+(1, "test", 2.1)++)") );
100
101 res = std::format(WIDEN("{:|^19n}"), t1);
102 VERIFY( res == WIDEN(R"(||1, "test", 2.1|||)") );
103
104 size = std::formatted_size(WIDEN("{}"), t1);
105 VERIFY( size == Sv(WIDEN(R"((1, "test", 2.1))")).size() );
106
107 size = std::formatted_size(WIDEN("{:3}"), t1);
108 VERIFY( size == Sv(WIDEN(R"((1, "test", 2.1))")).size() );
109
110 size = std::formatted_size(WIDEN("{:20}"), t1);
111 VERIFY( size == 20 );
112
113 std::tuple<int&, Str&, float&> t2 = t1;
114 res = std::format(WIDEN("{}"), t2);
115 VERIFY( res == WIDEN(R"((1, "test", 2.1))") );
116
117 std::tuple<int, int, int, int> t3(1, 2, 3, 4);
118 res = std::format(WIDEN("{}"), t3);
119 VERIFY( res == WIDEN(R"((1, 2, 3, 4))") );
120
121 }
122
123 template<typename _CharT, typename Tuple>
124 void test_empty()
125 {
126 std::basic_string<_CharT> res;
127
128 Tuple e1;
129 res = std::format(WIDEN("{}"), e1);
130 VERIFY( res == WIDEN(R"(())") );
131
132 res = std::format(WIDEN("{:}"), e1);
133 VERIFY( res == WIDEN(R"(())") );
134
135 res = std::format(WIDEN("{:n}"), e1);
136 VERIFY( res == WIDEN(R"()") );
137
138 res = std::format(WIDEN("{:^>6}"), e1);
139 VERIFY( res == WIDEN(R"(^^^^())") );
140 }
141
142 template<typename _CharT, typename Pair>
143 void test_pair()
144 {
145 using Ft = std::remove_cvref_t<std::tuple_element_t<0, Pair>>;
146 using St = std::remove_cvref_t<std::tuple_element_t<1, Pair>>;
147
148 std::basic_string<_CharT> res;
149
150 Ft f1 = 1;
151 St s1 = WIDEN("abc");
152 Pair p1(f1, s1);
153
154 res = std::format(WIDEN("{}"), p1);
155 VERIFY( res == WIDEN(R"((1, "abc"))") );
156
157 res = std::format(WIDEN("{:}"), p1);
158 VERIFY( res == WIDEN(R"((1, "abc"))") );
159
160 res = std::format(WIDEN("{:m}"), p1);
161 VERIFY( res == WIDEN(R"(1: "abc")") );
162
163 res = std::format(WIDEN("{:|^12m}"), p1);
164 VERIFY( res == WIDEN(R"(||1: "abc"||)") );
165 }
166
167 template<typename CharT, template<typename, typename> class PairT>
168 void test_pair_e()
169 {
170 test_pair<CharT, PairT<int, std::basic_string<CharT>>>();
171 test_pair<CharT, PairT<int, const CharT*>>();
172 test_pair<CharT, PairT<const int, std::basic_string<CharT>>>();
173 test_pair<CharT, PairT<int&, std::basic_string<CharT>&>>();
174 test_pair<CharT, PairT<const int&, const std::basic_string<CharT>&>>();
175 }
176
177 template<typename Pair>
178 struct MyPair : Pair
179 {
180 using Pair::Pair;
181 };
182
183 template<typename Pair, typename CharT>
184 struct std::formatter<MyPair<Pair>, CharT>
185 {
186 constexpr formatter() noexcept
187 {
188 using _CharT = CharT;
189 _formatter.set_brackets(WIDEN("<"), WIDEN(">"));
190 _formatter.set_separator(WIDEN("; "));
191 }
192
193 constexpr std::basic_format_parse_context<CharT>::iterator
194 parse(std::basic_format_parse_context<CharT>& pc)
195 { return _formatter.parse(pc); }
196
197 template<typename Out>
198 typename std::basic_format_context<Out, CharT>::iterator
199 format(const MyPair<Pair>& mp,
200 std::basic_format_context<Out, CharT>& fc) const
201 { return _formatter.format(mp, fc); }
202
203 private:
204 std::formatter<Pair, CharT> _formatter;
205 };
206
207 template<typename _CharT, template<typename, typename> class PairT>
208 void test_custom()
209 {
210 std::basic_string<_CharT> res;
211 MyPair<PairT<int, const _CharT*>> c1(1, WIDEN("abc"));
212
213 res = std::format(WIDEN("{}"), c1);
214 VERIFY( res == WIDEN(R"(<1; "abc">)") );
215
216 res = std::format(WIDEN("{:}"), c1);
217 VERIFY( res == WIDEN(R"(<1; "abc">)") );
218
219 res = std::format(WIDEN("{:n}"), c1);
220 VERIFY( res == WIDEN(R"(1; "abc")") );
221
222 res = std::format(WIDEN("{:m}"), c1);
223 VERIFY( res == WIDEN(R"(1: "abc")") );
224
225 res = std::format(WIDEN("{:|^14}"), c1);
226 VERIFY( res == WIDEN(R"(||<1; "abc">||)") );
227 }
228
229 template<typename CharT>
230 void test_outputs()
231 {
232 test_multi<CharT>();
233 test_empty<CharT, std::tuple<>>();
234 test_pair_e<CharT, std::pair>();
235 test_pair_e<CharT, std::tuple>();
236 test_custom<CharT, std::pair>();
237 test_custom<CharT, std::tuple>();
238 }
239
240 void test_nested()
241 {
242 std::string res;
243 std::tuple<std::tuple<>, std::pair<int, std::string>> tt{{}, {1, "abc"}};
244
245 res = std::format("{}", tt);
246 VERIFY( res == R"(((), (1, "abc")))" );
247 res = std::format("{:n}", tt);
248 VERIFY( res == R"((), (1, "abc"))" );
249 res = std::format("{:m}", tt);
250 VERIFY( res == R"((): (1, "abc"))" );
251 }
252
253 int main()
254 {
255 test_format_string();
256 test_outputs<char>();
257 test_outputs<wchar_t>();
258 test_nested();
259 }