Debian Codegraph @

libcxx/test/std/containers/container.adaptors/container.adaptors.format/format.functions.tests.h

raw
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 #ifndef TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_CONTAINER_ADAPTORS_FORMAT_FORMAT_FUNCTIONS_TESTS_H
9 #define TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_CONTAINER_ADAPTORS_FORMAT_FORMAT_FUNCTIONS_TESTS_H
10
11 #include <algorithm>
12 #include <array>
13 #include <charconv>
14 #include <concepts>
15 #include <format>
16 #include <list>
17 #include <queue>
18 #include <stack>
19
20 #include "format.functions.common.h"
21 #include "make_string.h"
22 #include "platform_support.h" // locale name macros
23 #include "test_macros.h"
24
25 //
26 // Char
27 //
28
29 template <class CharT, class TestFunction, class ExceptionTest>
30 void test_char_default(TestFunction check, ExceptionTest check_exception, auto&& input) {
31 // Note when no range-underlying-spec is present the char is escaped,
32 check(SV("['H', 'e', 'l', 'l', 'o']"), SV("{}"), input);
33 check(SV("['H', 'e', 'l', 'l', 'o']^42"), SV("{}^42"), input);
34 check(SV("['H', 'e', 'l', 'l', 'o']^42"), SV("{:}^42"), input);
35
36 // when one is present there is no escaping,
37 check(SV("[H, e, l, l, o]"), SV("{::}"), input);
38 check(SV("[H, e, l, l, o]"), SV("{::<}"), input);
39 // unless forced by the type specifier.
40 check(SV("['H', 'e', 'l', 'l', 'o']"), SV("{::?}"), input);
41 check(SV("['H', 'e', 'l', 'l', 'o']"), SV("{::<?}"), input);
42
43 // ***** underlying has no format-spec
44
45 // *** align-fill & width ***
46 check(SV("['H', 'e', 'l', 'l', 'o'] "), SV("{:30}"), input);
47 check(SV("['H', 'e', 'l', 'l', 'o']*****"), SV("{:*<30}"), input);
48 check(SV("__['H', 'e', 'l', 'l', 'o']___"), SV("{:_^30}"), input);
49 check(SV("#####['H', 'e', 'l', 'l', 'o']"), SV("{:#>30}"), input);
50
51 check(SV("['H', 'e', 'l', 'l', 'o'] "), SV("{:{}}"), input, 30);
52 check(SV("['H', 'e', 'l', 'l', 'o']*****"), SV("{:*<{}}"), input, 30);
53 check(SV("__['H', 'e', 'l', 'l', 'o']___"), SV("{:_^{}}"), input, 30);
54 check(SV("#####['H', 'e', 'l', 'l', 'o']"), SV("{:#>{}}"), input, 30);
55
56 check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
57 check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
58
59 // *** sign ***
60 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
61 check_exception("The format specifier should consume the input or end with a '}'", SV("{:+}"), input);
62 check_exception("The format specifier should consume the input or end with a '}'", SV("{: }"), input);
63
64 // *** alternate form ***
65 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
66
67 // *** zero-padding ***
68 check_exception("The width option should not have a leading zero", SV("{:0}"), input);
69
70 // *** precision ***
71 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.}"), input);
72
73 // *** locale-specific form ***
74 check_exception("The format specifier should consume the input or end with a '}'", SV("{:L}"), input);
75
76 // *** n
77 check(SV("__'H', 'e', 'l', 'l', 'o'___"), SV("{:_^28n}"), input);
78
79 // *** type ***
80 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
81 for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
82 check_exception("The format specifier should consume the input or end with a '}'", fmt, input);
83
84 // ***** Only underlying has a format-spec
85 check(SV("[H , e , l , l , o ]"), SV("{::4}"), input);
86 check(SV("[H***, e***, l***, l***, o***]"), SV("{::*<4}"), input);
87 check(SV("[_H__, _e__, _l__, _l__, _o__]"), SV("{::_^4}"), input);
88 check(SV("[:::H, :::e, :::l, :::l, :::o]"), SV("{:::>4}"), input);
89
90 check(SV("[H , e , l , l , o ]"), SV("{::{}}"), input, 4);
91 check(SV("[H***, e***, l***, l***, o***]"), SV("{::*<{}}"), input, 4);
92 check(SV("[_H__, _e__, _l__, _l__, _o__]"), SV("{::_^{}}"), input, 4);
93 check(SV("[:::H, :::e, :::l, :::l, :::o]"), SV("{:::>{}}"), input, 4);
94
95 check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
96 check_exception("The fill option contains an invalid value", SV("{::{<}"), input);
97
98 // *** sign ***
99 check_exception("The format specifier for a character does not allow the sign option", SV("{::-}"), input);
100 check_exception("The format specifier for a character does not allow the sign option", SV("{::+}"), input);
101 check_exception("The format specifier for a character does not allow the sign option", SV("{:: }"), input);
102
103 check(SV("[72, 101, 108, 108, 111]"), SV("{::-d}"), input);
104 check(SV("[+72, +101, +108, +108, +111]"), SV("{::+d}"), input);
105 check(SV("[ 72, 101, 108, 108, 111]"), SV("{:: d}"), input);
106
107 // *** alternate form ***
108 check_exception("The format specifier for a character does not allow the alternate form option", SV("{::#}"), input);
109
110 check(SV("[0x48, 0x65, 0x6c, 0x6c, 0x6f]"), SV("{::#x}"), input);
111
112 // *** zero-padding ***
113 check_exception("The format specifier for a character does not allow the zero-padding option", SV("{::05}"), input);
114
115 check(SV("[00110, 00145, 00154, 00154, 00157]"), SV("{::05o}"), input);
116
117 // *** precision ***
118 check_exception("The format specifier should consume the input or end with a '}'", SV("{::.}"), input);
119
120 // *** locale-specific form ***
121 check(SV("[H, e, l, l, o]"), SV("{::L}"), input);
122
123 // *** type ***
124 for (std::basic_string_view<CharT> fmt : fmt_invalid_nested_types<CharT>("bBcdoxX?"))
125 check_exception("The type option contains an invalid value for a character formatting argument", fmt, input);
126
127 // ***** Both have a format specifier
128 check(SV("^^[:H, :e, :l, :l, :o]^^^"), SV("{:^^25::>2}"), input);
129 check(SV("^^[:H, :e, :l, :l, :o]^^^"), SV("{:^^{}::>2}"), input, 25);
130 check(SV("^^[:H, :e, :l, :l, :o]^^^"), SV("{:^^{}::>{}}"), input, 25, 2);
131
132 check_exception(
133 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>2}"), input);
134 check_exception(
135 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>{}}"), input, 25);
136 }
137
138 template <class CharT, class TestFunction, class ExceptionTest>
139 void test_char_string(TestFunction check, ExceptionTest check_exception, auto&& input) {
140 check(SV("Hello"), SV("{:s}"), input);
141
142 // ***** underlying has no format-spec
143
144 // *** align-fill & width ***
145 check(SV("Hello "), SV("{:8s}"), input);
146 check(SV("Hello***"), SV("{:*<8s}"), input);
147 check(SV("_Hello__"), SV("{:_^8s}"), input);
148 check(SV("###Hello"), SV("{:#>8s}"), input);
149
150 check(SV("Hello "), SV("{:{}s}"), input, 8);
151 check(SV("Hello***"), SV("{:*<{}s}"), input, 8);
152 check(SV("_Hello__"), SV("{:_^{}s}"), input, 8);
153 check(SV("###Hello"), SV("{:#>{}s}"), input, 8);
154
155 check_exception("The format string contains an invalid escape sequence", SV("{:}<s}"), input);
156 check_exception("The fill option contains an invalid value", SV("{:{<s}"), input);
157
158 // *** sign ***
159 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-s}"), input);
160 check_exception("The format specifier should consume the input or end with a '}'", SV("{:+s}"), input);
161 check_exception("The format specifier should consume the input or end with a '}'", SV("{: s}"), input);
162
163 // *** alternate form ***
164 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#s}"), input);
165
166 // *** zero-padding ***
167 check_exception("The width option should not have a leading zero", SV("{:0s}"), input);
168
169 // *** precision ***
170 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.s}"), input);
171
172 // *** locale-specific form ***
173 check_exception("The format specifier should consume the input or end with a '}'", SV("{:Ls}"), input);
174
175 // *** n
176 check_exception("The n option and type s can't be used together", SV("{:ns}"), input);
177
178 // *** type ***
179 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
180 check_exception("The type option contains an invalid value for a character formatting argument", SV("{::<s}"), input);
181
182 // ***** Only underlying has a format-spec
183 check_exception("Type s and an underlying format specification can't be used together", SV("{:s:}"), input);
184 for (std::basic_string_view<CharT> fmt : fmt_invalid_nested_types<CharT>("bBcdoxX?"))
185 check_exception("The type option contains an invalid value for a character formatting argument", fmt, input);
186
187 // ***** Both have a format-spec
188 check_exception("Type s and an underlying format specification can't be used together", SV("{:5s:5}"), input);
189 }
190
191 template <class CharT, class TestFunction, class ExceptionTest>
192 void test_char_escaped_string(TestFunction check, ExceptionTest check_exception, auto&& input) {
193 check(SV(R"("Hello")"), SV("{:?s}"), input);
194
195 // ***** underlying has no format-spec
196
197 // *** align-fill & width ***
198 check(SV(R"("Hello" )"), SV("{:10?s}"), input);
199 check(SV(R"("Hello"***)"), SV("{:*<10?s}"), input);
200 check(SV(R"(_"Hello"__)"), SV("{:_^10?s}"), input);
201 check(SV(R"(###"Hello")"), SV("{:#>10?s}"), input);
202
203 check(SV(R"("Hello" )"), SV("{:{}?s}"), input, 10);
204 check(SV(R"("Hello"***)"), SV("{:*<{}?s}"), input, 10);
205 check(SV(R"(_"Hello"__)"), SV("{:_^{}?s}"), input, 10);
206 check(SV(R"(###"Hello")"), SV("{:#>{}?s}"), input, 10);
207
208 check_exception("The format string contains an invalid escape sequence", SV("{:}<?s}"), input);
209 check_exception("The fill option contains an invalid value", SV("{:{<?s}"), input);
210 check_exception("The format specifier should consume the input or end with a '}'", SV("{::<?s}"), input);
211
212 // *** sign ***
213 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-?s}"), input);
214 check_exception("The format specifier should consume the input or end with a '}'", SV("{:+?s}"), input);
215 check_exception("The format specifier should consume the input or end with a '}'", SV("{: ?s}"), input);
216
217 // *** alternate form ***
218 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#?s}"), input);
219
220 // *** zero-padding ***
221 check_exception("The width option should not have a leading zero", SV("{:0?s}"), input);
222
223 // *** precision ***
224 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.?s}"), input);
225
226 // *** locale-specific form ***
227 check_exception("The format specifier should consume the input or end with a '}'", SV("{:L?s}"), input);
228
229 // *** n
230 check_exception("The n option and type ?s can't be used together", SV("{:n?s}"), input);
231
232 // *** type ***
233 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
234
235 // ***** Only underlying has a format-spec
236 check_exception("Type ?s and an underlying format specification can't be used together", SV("{:?s:}"), input);
237
238 // ***** Both have a format-spec
239 check_exception("Type ?s and an underlying format specification can't be used together", SV("{:5?s:5}"), input);
240 }
241
242 template <class CharT, class TestFunction, class ExceptionTest>
243 void test_char(TestFunction check, ExceptionTest check_exception) {
244 // These values are in numeric order when using ASCII, which is used by the priority_queue.
245 std::array input{CharT('H'), CharT('e'), CharT('l'), CharT('l'), CharT('o')};
246 test_char_default<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
247 test_char_default<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end(), std::greater{}});
248 test_char_default<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
249
250 test_char_string<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
251 test_char_string<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end(), std::greater{}});
252 test_char_string<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
253
254 test_char_escaped_string<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
255 test_char_escaped_string<CharT>(
256 check, check_exception, std::priority_queue{input.begin(), input.end(), std::greater{}});
257 test_char_escaped_string<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
258
259 // LWG3881 fixes formatting container adaptors backed by a std::string.
260 test_char_default<CharT>(check, check_exception, std::queue{std::basic_string<CharT>{input.begin(), input.end()}});
261 test_char_default<CharT>(
262 check,
263 check_exception,
264 std::priority_queue{std::greater{}, std::basic_string<CharT>{input.begin(), input.end()}});
265 test_char_default<CharT>(check, check_exception, std::stack{std::basic_string<CharT>{input.begin(), input.end()}});
266 }
267
268 //
269 // char -> wchar_t
270 //
271
272 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
273 template <class TestFunction, class ExceptionTest>
274 void test_char_to_wchar(TestFunction check, ExceptionTest check_exception) {
275 std::array input{'H', 'e', 'l', 'l', 'o'};
276 test_char_default<wchar_t>(check, check_exception, std::queue{input.begin(), input.end()});
277 test_char_default<wchar_t>(check, check_exception, std::priority_queue{input.begin(), input.end(), std::greater{}});
278 test_char_default<wchar_t>(check, check_exception, std::stack{input.begin(), input.end()});
279
280 // The types s and ?s may only be used when using range_formatter<T, charT>
281 // where the types T and charT are the same. This means this can't be used for
282 // range_formatter<wchar_t, char> even when formatter<wchar_t, char> has a
283 // debug-enabled specialization.
284
285 using CharT = wchar_t;
286 check_exception(
287 "Type s requires character type as formatting argument", SV("{:s}"), std::queue{input.begin(), input.end()});
288 check_exception("Type s requires character type as formatting argument",
289 SV("{:s}"),
290 std::priority_queue{input.begin(), input.end()});
291 check_exception(
292 "Type s requires character type as formatting argument", SV("{:s}"), std::stack{input.begin(), input.end()});
293 check_exception(
294 "Type ?s requires character type as formatting argument", SV("{:?s}"), std::queue{input.begin(), input.end()});
295 check_exception("Type ?s requires character type as formatting argument",
296 SV("{:?s}"),
297 std::priority_queue{input.begin(), input.end()});
298 check_exception(
299 "Type ?s requires character type as formatting argument", SV("{:?s}"), std::stack{input.begin(), input.end()});
300 }
301 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
302
303 //
304 // Bool
305 //
306
307 template <class CharT, class TestFunction, class ExceptionTest>
308 void test_bool(TestFunction check, ExceptionTest check_exception, auto&& input) {
309 check(SV("[true, true, false]"), SV("{}"), input);
310 check(SV("[true, true, false]^42"), SV("{}^42"), input);
311 check(SV("[true, true, false]^42"), SV("{:}^42"), input);
312
313 // ***** underlying has no format-spec
314
315 // *** align-fill & width ***
316 check(SV("[true, true, false] "), SV("{:24}"), input);
317 check(SV("[true, true, false]*****"), SV("{:*<24}"), input);
318 check(SV("__[true, true, false]___"), SV("{:_^24}"), input);
319 check(SV("#####[true, true, false]"), SV("{:#>24}"), input);
320
321 check(SV("[true, true, false] "), SV("{:{}}"), input, 24);
322 check(SV("[true, true, false]*****"), SV("{:*<{}}"), input, 24);
323 check(SV("__[true, true, false]___"), SV("{:_^{}}"), input, 24);
324 check(SV("#####[true, true, false]"), SV("{:#>{}}"), input, 24);
325
326 check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
327 check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
328
329 // *** sign ***
330 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
331 check_exception("The format specifier should consume the input or end with a '}'", SV("{:+}"), input);
332 check_exception("The format specifier should consume the input or end with a '}'", SV("{: }"), input);
333
334 // *** alternate form ***
335 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
336
337 // *** zero-padding ***
338 check_exception("The width option should not have a leading zero", SV("{:0}"), input);
339
340 // *** precision ***
341 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.}"), input);
342
343 // *** locale-specific form ***
344 check_exception("The format specifier should consume the input or end with a '}'", SV("{:L}"), input);
345
346 // *** n
347 check(SV("__true, true, false___"), SV("{:_^22n}"), input);
348
349 // *** type ***
350 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
351 check_exception("Type s requires character type as formatting argument", SV("{:s}"), input);
352 check_exception("Type ?s requires character type as formatting argument", SV("{:?s}"), input);
353 for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
354 check_exception("The format specifier should consume the input or end with a '}'", fmt, input);
355
356 // ***** Only underlying has a format-spec
357 check(SV("[true , true , false ]"), SV("{::7}"), input);
358 check(SV("[true***, true***, false**]"), SV("{::*<7}"), input);
359 check(SV("[_true__, _true__, _false_]"), SV("{::_^7}"), input);
360 check(SV("[:::true, :::true, ::false]"), SV("{:::>7}"), input);
361
362 check(SV("[true , true , false ]"), SV("{::{}}"), input, 7);
363 check(SV("[true***, true***, false**]"), SV("{::*<{}}"), input, 7);
364 check(SV("[_true__, _true__, _false_]"), SV("{::_^{}}"), input, 7);
365 check(SV("[:::true, :::true, ::false]"), SV("{:::>{}}"), input, 7);
366
367 check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
368 check_exception("The fill option contains an invalid value", SV("{::{<}"), input);
369
370 // *** sign ***
371 check_exception("The format specifier for a bool does not allow the sign option", SV("{::-}"), input);
372 check_exception("The format specifier for a bool does not allow the sign option", SV("{::+}"), input);
373 check_exception("The format specifier for a bool does not allow the sign option", SV("{:: }"), input);
374
375 check(SV("[1, 1, 0]"), SV("{::-d}"), input);
376 check(SV("[+1, +1, +0]"), SV("{::+d}"), input);
377 check(SV("[ 1, 1, 0]"), SV("{:: d}"), input);
378
379 // *** alternate form ***
380 check_exception("The format specifier for a bool does not allow the alternate form option", SV("{::#}"), input);
381
382 check(SV("[0x1, 0x1, 0x0]"), SV("{::#x}"), input);
383
384 // *** zero-padding ***
385 check_exception("The format specifier for a bool does not allow the zero-padding option", SV("{::05}"), input);
386
387 check(SV("[00001, 00001, 00000]"), SV("{::05o}"), input);
388
389 // *** precision ***
390 check_exception("The format specifier should consume the input or end with a '}'", SV("{::.}"), input);
391
392 // *** locale-specific form ***
393 check(SV("[true, true, false]"), SV("{::L}"), input);
394
395 // *** type ***
396 for (std::basic_string_view<CharT> fmt : fmt_invalid_nested_types<CharT>("bBdosxX"))
397 check_exception("The type option contains an invalid value for a bool formatting argument", fmt, input);
398
399 // ***** Both have a format-spec
400 check(SV("^^[:::true, :::true, ::false]^^^"), SV("{:^^32::>7}"), input);
401 check(SV("^^[:::true, :::true, ::false]^^^"), SV("{:^^{}::>7}"), input, 32);
402 check(SV("^^[:::true, :::true, ::false]^^^"), SV("{:^^{}::>{}}"), input, 32, 7);
403
404 check_exception(
405 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>5}"), input);
406 check_exception(
407 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>{}}"), input, 32);
408 }
409
410 template <class CharT, class TestFunction, class ExceptionTest>
411 void test_bool(TestFunction check, ExceptionTest check_exception) {
412 std::array input{true, true, false};
413 test_bool<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
414 test_bool<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end()});
415 test_bool<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
416 }
417
418 //
419 // Integral
420 //
421
422 template <class CharT, class TestFunction, class ExceptionTest>
423 void test_int(TestFunction check, ExceptionTest check_exception, auto&& input) {
424 check(SV("[-42, 1, 2, 42]"), SV("{}"), input);
425 check(SV("[-42, 1, 2, 42]^42"), SV("{}^42"), input);
426 check(SV("[-42, 1, 2, 42]^42"), SV("{:}^42"), input);
427
428 // ***** underlying has no format-spec
429
430 // *** align-fill & width ***
431 check(SV("[-42, 1, 2, 42] "), SV("{:20}"), input);
432 check(SV("[-42, 1, 2, 42]*****"), SV("{:*<20}"), input);
433 check(SV("__[-42, 1, 2, 42]___"), SV("{:_^20}"), input);
434 check(SV("#####[-42, 1, 2, 42]"), SV("{:#>20}"), input);
435
436 check(SV("[-42, 1, 2, 42] "), SV("{:{}}"), input, 20);
437 check(SV("[-42, 1, 2, 42]*****"), SV("{:*<{}}"), input, 20);
438 check(SV("__[-42, 1, 2, 42]___"), SV("{:_^{}}"), input, 20);
439 check(SV("#####[-42, 1, 2, 42]"), SV("{:#>{}}"), input, 20);
440
441 check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
442 check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
443
444 // *** sign ***
445 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
446 check_exception("The format specifier should consume the input or end with a '}'", SV("{:+}"), input);
447 check_exception("The format specifier should consume the input or end with a '}'", SV("{: }"), input);
448
449 // *** alternate form ***
450 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
451
452 // *** zero-padding ***
453 check_exception("The width option should not have a leading zero", SV("{:0}"), input);
454
455 // *** precision ***
456 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.}"), input);
457
458 // *** locale-specific form ***
459 check_exception("The format specifier should consume the input or end with a '}'", SV("{:L}"), input);
460
461 // *** n
462 check(SV("__-42, 1, 2, 42___"), SV("{:_^18n}"), input);
463
464 // *** type ***
465 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
466 check_exception("Type s requires character type as formatting argument", SV("{:s}"), input);
467 check_exception("Type ?s requires character type as formatting argument", SV("{:?s}"), input);
468 for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
469 check_exception("The format specifier should consume the input or end with a '}'", fmt, input);
470
471 // ***** Only underlying has a format-spec
472 check(SV("[ -42, 1, 2, 42]"), SV("{::5}"), input);
473 check(SV("[-42**, 1****, 2****, 42***]"), SV("{::*<5}"), input);
474 check(SV("[_-42_, __1__, __2__, _42__]"), SV("{::_^5}"), input);
475 check(SV("[::-42, ::::1, ::::2, :::42]"), SV("{:::>5}"), input);
476
477 check(SV("[ -42, 1, 2, 42]"), SV("{::{}}"), input, 5);
478 check(SV("[-42**, 1****, 2****, 42***]"), SV("{::*<{}}"), input, 5);
479 check(SV("[_-42_, __1__, __2__, _42__]"), SV("{::_^{}}"), input, 5);
480 check(SV("[::-42, ::::1, ::::2, :::42]"), SV("{:::>{}}"), input, 5);
481
482 check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
483 check_exception("The fill option contains an invalid value", SV("{::{<}"), input);
484
485 // *** sign ***
486 check(SV("[-42, 1, 2, 42]"), SV("{::-}"), input);
487 check(SV("[-42, +1, +2, +42]"), SV("{::+}"), input);
488 check(SV("[-42, 1, 2, 42]"), SV("{:: }"), input);
489
490 // *** alternate form ***
491 check(SV("[-0x2a, 0x1, 0x2, 0x2a]"), SV("{::#x}"), input);
492
493 // *** zero-padding ***
494 check(SV("[-0042, 00001, 00002, 00042]"), SV("{::05}"), input);
495 check(SV("[-002a, 00001, 00002, 0002a]"), SV("{::05x}"), input);
496 check(SV("[-0x2a, 0x001, 0x002, 0x02a]"), SV("{::#05x}"), input);
497
498 // *** precision ***
499 check_exception("The format specifier should consume the input or end with a '}'", SV("{::.}"), input);
500
501 // *** locale-specific form ***
502 check(SV("[-42, 1, 2, 42]"), SV("{::L}"), input); // does nothing in this test, but is accepted.
503
504 // *** type ***
505 for (std::basic_string_view<CharT> fmt : fmt_invalid_nested_types<CharT>("bBcdoxX"))
506 check_exception("The type option contains an invalid value for an integer formatting argument", fmt, input);
507
508 // ***** Both have a format-spec
509 check(SV("^^[::-42, ::::1, ::::2, :::42]^^^"), SV("{:^^33::>5}"), input);
510 check(SV("^^[::-42, ::::1, ::::2, :::42]^^^"), SV("{:^^{}::>5}"), input, 33);
511 check(SV("^^[::-42, ::::1, ::::2, :::42]^^^"), SV("{:^^{}::>{}}"), input, 33, 5);
512
513 check_exception(
514 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>5}"), input);
515 check_exception(
516 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>{}}"), input, 33);
517 }
518
519 template <class CharT, class TestFunction, class ExceptionTest>
520 void test_int(TestFunction check, ExceptionTest check_exception) {
521 std::array input{-42, 1, 2, 42};
522 test_int<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
523 test_int<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end(), std::greater{}});
524 test_int<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
525 }
526
527 //
528 // Floating point
529 //
530
531 template <class CharT, class TestFunction, class ExceptionTest>
532 void test_floating_point(TestFunction check, ExceptionTest check_exception, auto&& input) {
533 check(SV("[-42.5, 0, 1.25, 42.5]"), SV("{}"), input);
534 check(SV("[-42.5, 0, 1.25, 42.5]^42"), SV("{}^42"), input);
535 check(SV("[-42.5, 0, 1.25, 42.5]^42"), SV("{:}^42"), input);
536
537 // ***** underlying has no format-spec
538
539 // *** align-fill & width ***
540 check(SV("[-42.5, 0, 1.25, 42.5] "), SV("{:27}"), input);
541 check(SV("[-42.5, 0, 1.25, 42.5]*****"), SV("{:*<27}"), input);
542 check(SV("__[-42.5, 0, 1.25, 42.5]___"), SV("{:_^27}"), input);
543 check(SV("#####[-42.5, 0, 1.25, 42.5]"), SV("{:#>27}"), input);
544
545 check(SV("[-42.5, 0, 1.25, 42.5] "), SV("{:{}}"), input, 27);
546 check(SV("[-42.5, 0, 1.25, 42.5]*****"), SV("{:*<{}}"), input, 27);
547 check(SV("__[-42.5, 0, 1.25, 42.5]___"), SV("{:_^{}}"), input, 27);
548 check(SV("#####[-42.5, 0, 1.25, 42.5]"), SV("{:#>{}}"), input, 27);
549
550 check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
551 check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
552
553 // *** sign ***
554 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
555 check_exception("The format specifier should consume the input or end with a '}'", SV("{:+}"), input);
556 check_exception("The format specifier should consume the input or end with a '}'", SV("{: }"), input);
557
558 // *** alternate form ***
559 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
560
561 // *** zero-padding ***
562 check_exception("The width option should not have a leading zero", SV("{:0}"), input);
563
564 // *** precision ***
565 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.}"), input);
566
567 // *** locale-specific form ***
568 check_exception("The format specifier should consume the input or end with a '}'", SV("{:L}"), input);
569
570 // *** n
571 check(SV("__-42.5, 0, 1.25, 42.5___"), SV("{:_^25n}"), input);
572
573 // *** type ***
574 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
575 check_exception("Type s requires character type as formatting argument", SV("{:s}"), input);
576 check_exception("Type ?s requires character type as formatting argument", SV("{:?s}"), input);
577 for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
578 check_exception("The format specifier should consume the input or end with a '}'", fmt, input);
579
580 // ***** Only underlying has a format-spec
581 check(SV("[-42.5, 0, 1.25, 42.5]"), SV("{::5}"), input);
582 check(SV("[-42.5, 0****, 1.25*, 42.5*]"), SV("{::*<5}"), input);
583 check(SV("[-42.5, __0__, 1.25_, 42.5_]"), SV("{::_^5}"), input);
584 check(SV("[-42.5, ::::0, :1.25, :42.5]"), SV("{:::>5}"), input);
585
586 check(SV("[-42.5, 0, 1.25, 42.5]"), SV("{::{}}"), input, 5);
587 check(SV("[-42.5, 0****, 1.25*, 42.5*]"), SV("{::*<{}}"), input, 5);
588 check(SV("[-42.5, __0__, 1.25_, 42.5_]"), SV("{::_^{}}"), input, 5);
589 check(SV("[-42.5, ::::0, :1.25, :42.5]"), SV("{:::>{}}"), input, 5);
590
591 check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
592 check_exception("The fill option contains an invalid value", SV("{::{<}"), input);
593
594 // *** sign ***
595 check(SV("[-42.5, 0, 1.25, 42.5]"), SV("{::-}"), input);
596 check(SV("[-42.5, +0, +1.25, +42.5]"), SV("{::+}"), input);
597 check(SV("[-42.5, 0, 1.25, 42.5]"), SV("{:: }"), input);
598
599 // *** alternate form ***
600 check(SV("[-42.5, 0., 1.25, 42.5]"), SV("{::#}"), input);
601
602 // *** zero-padding ***
603 check(SV("[-42.5, 00000, 01.25, 042.5]"), SV("{::05}"), input);
604 check(SV("[-42.5, 0000., 01.25, 042.5]"), SV("{::#05}"), input);
605
606 // *** precision ***
607 check(SV("[-42, 0, 1.2, 42]"), SV("{::.2}"), input);
608 check(SV("[-42.500, 0.000, 1.250, 42.500]"), SV("{::.3f}"), input);
609
610 check(SV("[-42, 0, 1.2, 42]"), SV("{::.{}}"), input, 2);
611 check(SV("[-42.500, 0.000, 1.250, 42.500]"), SV("{::.{}f}"), input, 3);
612
613 check_exception("The precision option does not contain a value or an argument index", SV("{::.}"), input);
614
615 // *** locale-specific form ***
616 check(SV("[-42.5, 0, 1.25, 42.5]"), SV("{::L}"), input); // does not require locales present
617 #ifndef TEST_HAS_NO_LOCALIZATION
618 // TODO FMT Enable with locale testing active
619 # if 0
620 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
621 check(SV("[-42,5, 0, 1,25, 42,5]"), SV("{::L}"), input);
622
623 std::locale::global(std::locale(LOCALE_en_US_UTF_8));
624 check(SV("[-42.5, 0, 1.25, 42.5]"), SV("{::L}"), input);
625
626 std::locale::global(std::locale::classic());
627 # endif
628 #endif // TEST_HAS_NO_LOCALIZATION
629
630 // *** type ***
631 for (std::basic_string_view<CharT> fmt : fmt_invalid_nested_types<CharT>("aAeEfFgG"))
632 check_exception("The type option contains an invalid value for a floating-point formatting argument", fmt, input);
633
634 // ***** Both have a format-spec
635 check(SV("^^[-42.5, ::::0, :1.25, :42.5]^^^"), SV("{:^^33::>5}"), input);
636 check(SV("^^[-42.5, ::::0, :1.25, :42.5]^^^"), SV("{:^^{}::>5}"), input, 33);
637 check(SV("^^[-42.5, ::::0, :1.25, :42.5]^^^"), SV("{:^^{}::>{}}"), input, 33, 5);
638
639 check(SV("^^[::-42, ::::0, ::1.2, :::42]^^^"), SV("{:^^33::>5.2}"), input);
640 check(SV("^^[::-42, ::::0, ::1.2, :::42]^^^"), SV("{:^^{}::>5.2}"), input, 33);
641 check(SV("^^[::-42, ::::0, ::1.2, :::42]^^^"), SV("{:^^{}::>{}.2}"), input, 33, 5);
642 check(SV("^^[::-42, ::::0, ::1.2, :::42]^^^"), SV("{:^^{}::>{}.{}}"), input, 33, 5, 2);
643
644 check_exception(
645 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>5.2}"), input);
646 check_exception(
647 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>{}.2}"), input, 33);
648 check_exception(
649 "The argument index value is too large for the number of arguments supplied",
650 SV("{:^^{}::>{}.{}}"),
651 input,
652 33,
653 5);
654 }
655
656 template <class CharT, class TestFunction, class ExceptionTest>
657 void test_floating_point(TestFunction check, ExceptionTest check_exception) {
658 std::array input{-42.5l, 0.0l, 1.25l, 42.5l};
659 test_floating_point<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
660 test_floating_point<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end(), std::greater{}});
661 test_floating_point<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
662 }
663
664 //
665 // Pointer
666 //
667
668 template <class CharT, class TestFunction, class ExceptionTest>
669 void test_pointer(TestFunction check, ExceptionTest check_exception, auto&& input) {
670 check(SV("[0x0]"), SV("{}"), input);
671 check(SV("[0x0]^42"), SV("{}^42"), input);
672 check(SV("[0x0]^42"), SV("{:}^42"), input);
673
674 // ***** underlying has no format-spec
675
676 // *** align-fill & width ***
677 check(SV("[0x0] "), SV("{:10}"), input);
678 check(SV("[0x0]*****"), SV("{:*<10}"), input);
679 check(SV("__[0x0]___"), SV("{:_^10}"), input);
680 check(SV("#####[0x0]"), SV("{:#>10}"), input);
681
682 check(SV("[0x0] "), SV("{:{}}"), input, 10);
683 check(SV("[0x0]*****"), SV("{:*<{}}"), input, 10);
684 check(SV("__[0x0]___"), SV("{:_^{}}"), input, 10);
685 check(SV("#####[0x0]"), SV("{:#>{}}"), input, 10);
686
687 check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
688 check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
689
690 // *** sign ***
691 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
692
693 // *** alternate form ***
694 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
695
696 // *** zero-padding ***
697 check_exception("The width option should not have a leading zero", SV("{:0}"), input);
698
699 // *** precision ***
700 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.}"), input);
701
702 // *** locale-specific form ***
703 check_exception("The format specifier should consume the input or end with a '}'", SV("{:L}"), input);
704
705 // *** n
706 check(SV("_0x0_"), SV("{:_^5n}"), input);
707
708 // *** type ***
709 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
710 check_exception("Type s requires character type as formatting argument", SV("{:s}"), input);
711 check_exception("Type ?s requires character type as formatting argument", SV("{:?s}"), input);
712 for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
713 check_exception("The format specifier should consume the input or end with a '}'", fmt, input);
714
715 // ***** Only underlying has a format-spec
716 check(SV("[ 0x0]"), SV("{::5}"), input);
717 check(SV("[0x0**]"), SV("{::*<5}"), input);
718 check(SV("[_0x0_]"), SV("{::_^5}"), input);
719 check(SV("[::0x0]"), SV("{:::>5}"), input);
720
721 check(SV("[ 0x0]"), SV("{::{}}"), input, 5);
722 check(SV("[0x0**]"), SV("{::*<{}}"), input, 5);
723 check(SV("[_0x0_]"), SV("{::_^{}}"), input, 5);
724 check(SV("[::0x0]"), SV("{:::>{}}"), input, 5);
725
726 check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
727 check_exception("The fill option contains an invalid value", SV("{::{<}"), input);
728
729 // *** sign ***
730 check_exception("The format specifier should consume the input or end with a '}'", SV("{::-}"), input);
731
732 // *** alternate form ***
733 check_exception("The format specifier should consume the input or end with a '}'", SV("{::#}"), input);
734
735 // *** zero-padding ***
736 check(SV("[0x0000]"), SV("{::06}"), input);
737 check(SV("[0x0000]"), SV("{::06p}"), input);
738 check(SV("[0X0000]"), SV("{::06P}"), input);
739
740 // *** precision ***
741 check_exception("The format specifier should consume the input or end with a '}'", SV("{::.}"), input);
742
743 // *** locale-specific form ***
744 check_exception("The format specifier should consume the input or end with a '}'", SV("{::L}"), input);
745
746 // *** type ***
747 for (std::basic_string_view<CharT> fmt : fmt_invalid_nested_types<CharT>("pP"))
748 check_exception("The type option contains an invalid value for a pointer formatting argument", fmt, input);
749
750 // ***** Both have a format-spec
751 check(SV("^^[::0x0]^^^"), SV("{:^^12::>5}"), input);
752 check(SV("^^[::0x0]^^^"), SV("{:^^{}::>5}"), input, 12);
753 check(SV("^^[::0x0]^^^"), SV("{:^^{}::>{}}"), input, 12, 5);
754
755 check(SV("^^[::0x0]^^^"), SV("{:^^12::>5}"), input);
756 check(SV("^^[::0x0]^^^"), SV("{:^^{}::>5}"), input, 12);
757 check(SV("^^[::0x0]^^^"), SV("{:^^{}::>{}}"), input, 12, 5);
758
759 check_exception(
760 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>5}"), input);
761 check_exception(
762 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>{}}"), input, 12);
763 }
764
765 template <class CharT, class TestFunction, class ExceptionTest>
766 void test_pointer(TestFunction check, ExceptionTest check_exception) {
767 std::array input{static_cast<void*>(0)};
768 test_pointer<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
769 test_pointer<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end()});
770 test_pointer<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
771 }
772
773 //
774 // String
775 //
776
777 template <class CharT, class TestFunction, class ExceptionTest>
778 void test_string(TestFunction check, ExceptionTest check_exception, auto&& input) {
779 check(SV(R"(["Hello", "world"])"), SV("{}"), input);
780 check(SV(R"(["Hello", "world"]^42)"), SV("{}^42"), input);
781 check(SV(R"(["Hello", "world"]^42)"), SV("{:}^42"), input);
782
783 // ***** underlying has no format-spec
784
785 // *** align-fill & width ***
786 check(SV(R"(["Hello", "world"] )"), SV("{:23}"), input);
787 check(SV(R"(["Hello", "world"]*****)"), SV("{:*<23}"), input);
788 check(SV(R"(__["Hello", "world"]___)"), SV("{:_^23}"), input);
789 check(SV(R"(#####["Hello", "world"])"), SV("{:#>23}"), input);
790
791 check(SV(R"(["Hello", "world"] )"), SV("{:{}}"), input, 23);
792 check(SV(R"(["Hello", "world"]*****)"), SV("{:*<{}}"), input, 23);
793 check(SV(R"(__["Hello", "world"]___)"), SV("{:_^{}}"), input, 23);
794 check(SV(R"(#####["Hello", "world"])"), SV("{:#>{}}"), input, 23);
795
796 check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
797 check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
798
799 // *** sign ***
800 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
801
802 // *** alternate form ***
803 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
804
805 // *** zero-padding ***
806 check_exception("The width option should not have a leading zero", SV("{:0}"), input);
807
808 // *** precision ***
809 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.}"), input);
810
811 // *** locale-specific form ***
812 check_exception("The format specifier should consume the input or end with a '}'", SV("{:L}"), input);
813
814 // *** n
815 check(SV(R"(_"Hello", "world"_)"), SV("{:_^18n}"), input);
816
817 // *** type ***
818 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
819 check_exception("Type s requires character type as formatting argument", SV("{:s}"), input);
820 check_exception("Type ?s requires character type as formatting argument", SV("{:?s}"), input);
821 for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
822 check_exception("The format specifier should consume the input or end with a '}'", fmt, input);
823
824 // ***** Only underlying has a format-spec
825 check(SV(R"([Hello , world ])"), SV("{::8}"), input);
826 check(SV(R"([Hello***, world***])"), SV("{::*<8}"), input);
827 check(SV(R"([_Hello__, _world__])"), SV("{::_^8}"), input);
828 check(SV(R"([:::Hello, :::world])"), SV("{:::>8}"), input);
829
830 check(SV(R"([Hello , world ])"), SV("{::{}}"), input, 8);
831 check(SV(R"([Hello***, world***])"), SV("{::*<{}}"), input, 8);
832 check(SV(R"([_Hello__, _world__])"), SV("{::_^{}}"), input, 8);
833 check(SV(R"([:::Hello, :::world])"), SV("{:::>{}}"), input, 8);
834
835 check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
836 check_exception("The fill option contains an invalid value", SV("{::{<}"), input);
837
838 // *** sign ***
839 check_exception("The format specifier should consume the input or end with a '}'", SV("{::-}"), input);
840
841 // *** alternate form ***
842 check_exception("The format specifier should consume the input or end with a '}'", SV("{::#}"), input);
843
844 // *** zero-padding ***
845 check_exception("The width option should not have a leading zero", SV("{::05}"), input);
846
847 // *** precision ***
848 check(SV(R"([Hel, wor])"), SV("{::.3}"), input);
849
850 check(SV(R"([Hel, wor])"), SV("{::.{}}"), input, 3);
851
852 check_exception("The precision option does not contain a value or an argument index", SV("{::.}"), input);
853
854 // *** locale-specific form ***
855 check_exception("The format specifier should consume the input or end with a '}'", SV("{::L}"), input);
856
857 // *** type ***
858 for (std::basic_string_view<CharT> fmt : fmt_invalid_nested_types<CharT>("s?"))
859 check_exception("The type option contains an invalid value for a string formatting argument", fmt, input);
860
861 // ***** Both have a format-spec
862 check(SV(R"(^^[:::Hello, :::world]^^^)"), SV("{:^^25::>8}"), input);
863 check(SV(R"(^^[:::Hello, :::world]^^^)"), SV("{:^^{}::>8}"), input, 25);
864 check(SV(R"(^^[:::Hello, :::world]^^^)"), SV("{:^^{}::>{}}"), input, 25, 8);
865
866 check(SV(R"(^^[:::Hello, :::world]^^^)"), SV("{:^^25::>8}"), input);
867 check(SV(R"(^^[:::Hello, :::world]^^^)"), SV("{:^^{}::>8}"), input, 25);
868 check(SV(R"(^^[:::Hello, :::world]^^^)"), SV("{:^^{}::>{}}"), input, 25, 8);
869
870 check_exception(
871 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>8}"), input);
872 check_exception(
873 "The argument index value is too large for the number of arguments supplied", SV("{:^^{}::>{}}"), input, 25);
874 }
875
876 template <class CharT, class TestFunction, class ExceptionTest>
877 void test_string(TestFunction check, ExceptionTest check_exception) {
878 std::array input{STR("Hello"), STR("world")};
879 test_string<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
880 test_string<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end(), std::greater{}});
881 test_string<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
882 }
883
884 //
885 // Handle
886 //
887
888 template <class CharT, class TestFunction, class ExceptionTest>
889 void test_status(TestFunction check, ExceptionTest check_exception, auto&& input) {
890 check(SV("[0xaaaa, 0x5555, 0xaa55]"), SV("{}"), input);
891 check(SV("[0xaaaa, 0x5555, 0xaa55]^42"), SV("{}^42"), input);
892 check(SV("[0xaaaa, 0x5555, 0xaa55]^42"), SV("{:}^42"), input);
893
894 // ***** underlying has no format-spec
895
896 // *** align-fill & width ***
897 check(SV("[0xaaaa, 0x5555, 0xaa55] "), SV("{:29}"), input);
898 check(SV("[0xaaaa, 0x5555, 0xaa55]*****"), SV("{:*<29}"), input);
899 check(SV("__[0xaaaa, 0x5555, 0xaa55]___"), SV("{:_^29}"), input);
900 check(SV("#####[0xaaaa, 0x5555, 0xaa55]"), SV("{:#>29}"), input);
901
902 check(SV("[0xaaaa, 0x5555, 0xaa55] "), SV("{:{}}"), input, 29);
903 check(SV("[0xaaaa, 0x5555, 0xaa55]*****"), SV("{:*<{}}"), input, 29);
904 check(SV("__[0xaaaa, 0x5555, 0xaa55]___"), SV("{:_^{}}"), input, 29);
905 check(SV("#####[0xaaaa, 0x5555, 0xaa55]"), SV("{:#>{}}"), input, 29);
906
907 check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
908 check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
909
910 // *** sign ***
911 check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
912 check_exception("The format specifier should consume the input or end with a '}'", SV("{:+}"), input);
913 check_exception("The format specifier should consume the input or end with a '}'", SV("{: }"), input);
914
915 // *** alternate form ***
916 check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
917
918 // *** zero-padding ***
919 check_exception("The width option should not have a leading zero", SV("{:0}"), input);
920
921 // *** precision ***
922 check_exception("The format specifier should consume the input or end with a '}'", SV("{:.}"), input);
923
924 // *** locale-specific form ***
925 check_exception("The format specifier should consume the input or end with a '}'", SV("{:L}"), input);
926
927 // *** n
928 check(SV("__0xaaaa, 0x5555, 0xaa55___"), SV("{:_^27n}"), input);
929
930 // *** type ***
931 check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
932 check_exception("Type s requires character type as formatting argument", SV("{:s}"), input);
933 check_exception("Type ?s requires character type as formatting argument", SV("{:?s}"), input);
934 for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
935 check_exception("The format specifier should consume the input or end with a '}'", fmt, input);
936
937 // ***** Only underlying has a format-spec
938 check_exception("The type option contains an invalid value for a status formatting argument", SV("{::*<7}"), input);
939 for (std::basic_string_view<CharT> fmt : fmt_invalid_nested_types<CharT>("sxX"))
940 check_exception("The type option contains an invalid value for a status formatting argument", fmt, input);
941
942 check(SV("[0xaaaa, 0x5555, 0xaa55]"), SV("{::x}"), input);
943 check(SV("[0XAAAA, 0X5555, 0XAA55]"), SV("{::X}"), input);
944 check(SV("[foo, bar, foobar]"), SV("{::s}"), input);
945
946 // ***** Both have a format-spec
947 check(SV("^^[0XAAAA, 0X5555, 0XAA55]^^^"), SV("{:^^29:X}"), input);
948 check(SV("^^[0XAAAA, 0X5555, 0XAA55]^^^"), SV("{:^^{}:X}"), input, 29);
949
950 check_exception("The argument index value is too large for the number of arguments supplied", SV("{:^^{}:X}"), input);
951 }
952
953 template <class CharT, class TestFunction, class ExceptionTest>
954 void test_status(TestFunction check, ExceptionTest check_exception) {
955 std::array input{status::foo, status::bar, status::foobar};
956 test_status<CharT>(check, check_exception, std::queue{input.begin(), input.end()});
957 test_status<CharT>(check, check_exception, std::priority_queue{input.begin(), input.end(), std::less{}});
958 test_status<CharT>(check, check_exception, std::stack{input.begin(), input.end()});
959 }
960
961 //
962 // Driver
963 //
964
965 template <class CharT, class TestFunction, class ExceptionTest>
966 void format_tests(TestFunction check, ExceptionTest check_exception) {
967 test_char<CharT>(check, check_exception);
968 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
969 if (std::same_as<CharT, wchar_t>) // avoid testing twice
970 test_char_to_wchar(check, check_exception);
971 #endif
972 test_bool<CharT>(check, check_exception);
973 test_int<CharT>(check, check_exception);
974 test_floating_point<CharT>(check, check_exception);
975 test_pointer<CharT>(check, check_exception);
976 test_string<CharT>(check, check_exception);
977
978 test_status<CharT>(check, check_exception); // Has its own handler with its own parser
979 }
980
981 #endif // TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_CONTAINER_ADAPTORS_FORMAT_FORMAT_FUNCTIONS_TESTS_H