File: format.functions.tests.h

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (373 lines) | stat: -rw-r--r-- 17,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//===----------------------------------------------------------------------===//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef TEST_STD_UTILITIES_FORMAT_FORMAT_TUPLE_FORMAT_TESTS_H
#define TEST_STD_UTILITIES_FORMAT_FORMAT_TUPLE_FORMAT_TESTS_H

#include <concepts>
#include <format>

#include "format.functions.common.h"

enum class color { black, red, gold };

template <class CharT>
struct std::formatter<color, CharT> : std::formatter<basic_string_view<CharT>, CharT> {
  static constexpr basic_string_view<CharT> color_names[] = {SV("black"), SV("red"), SV("gold")};
  auto format(color c, auto& ctx) const {
    return formatter<basic_string_view<CharT>, CharT>::format(color_names[static_cast<int>(c)], ctx);
  }
};

//
// Generic tests for a tuple and pair with two elements.
//
template <class CharT, class TestFunction, class ExceptionTest, class TupleOrPair>
void test_tuple_or_pair_int_int(TestFunction check, ExceptionTest check_exception, TupleOrPair&& input) {
  check(SV("(42, 99)"), SV("{}"), input);

  // *** align-fill & width ***
  check(SV("(42, 99)     "), SV("{:13}"), input);
  check(SV("(42, 99)*****"), SV("{:*<13}"), input);
  check(SV("__(42, 99)___"), SV("{:_^13}"), input);
  check(SV("#####(42, 99)"), SV("{:#>13}"), input);

  check(SV("(42, 99)     "), SV("{:{}}"), input, 13);
  check(SV("(42, 99)*****"), SV("{:*<{}}"), input, 13);
  check(SV("__(42, 99)___"), SV("{:_^{}}"), input, 13);
  check(SV("#####(42, 99)"), SV("{:#>{}}"), input, 13);

  check_exception("The format-spec range-fill field contains an invalid character", SV("{:}<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{:{<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{::<}"), input);

  // *** sign ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:-}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:+}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{: }"), input);

  // *** alternate form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:#}"), input);

  // *** zero-padding ***
  check_exception("A format-spec width field shouldn't have a leading zero", SV("{:0}"), input);

  // *** precision ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), input);

  // *** locale-specific form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:L}"), input);

  // *** type ***
  check(SV("__42: 99___"), SV("{:_^11m}"), input);
  check(SV("__42, 99___"), SV("{:_^11n}"), input);

  for (CharT c : SV("aAbBcdeEfFgGopsxX?")) {
    check_exception("The format-spec should consume the input or end with a '}'",
                    std::basic_string_view{STR("{:") + c + STR("}")},
                    input);
  }
}

template <class CharT, class TestFunction, class ExceptionTest, class TupleOrPair>
void test_tuple_or_pair_int_string(TestFunction check, ExceptionTest check_exception, TupleOrPair&& input) {
  check(SV("(42, \"hello\")"), SV("{}"), input);

  // *** align-fill & width ***
  check(SV("(42, \"hello\")     "), SV("{:18}"), input);
  check(SV("(42, \"hello\")*****"), SV("{:*<18}"), input);
  check(SV("__(42, \"hello\")___"), SV("{:_^18}"), input);
  check(SV("#####(42, \"hello\")"), SV("{:#>18}"), input);

  check(SV("(42, \"hello\")     "), SV("{:{}}"), input, 18);
  check(SV("(42, \"hello\")*****"), SV("{:*<{}}"), input, 18);
  check(SV("__(42, \"hello\")___"), SV("{:_^{}}"), input, 18);
  check(SV("#####(42, \"hello\")"), SV("{:#>{}}"), input, 18);

  check_exception("The format-spec range-fill field contains an invalid character", SV("{:}<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{:{<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{::<}"), input);

  // *** sign ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:-}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:+}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{: }"), input);

  // *** alternate form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:#}"), input);

  // *** zero-padding ***
  check_exception("A format-spec width field shouldn't have a leading zero", SV("{:0}"), input);

  // *** precision ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), input);

  // *** locale-specific form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:L}"), input);

  // *** type ***
  check(SV("__42: \"hello\"___"), SV("{:_^16m}"), input);
  check(SV("__42, \"hello\"___"), SV("{:_^16n}"), input);

  for (CharT c : SV("aAbBcdeEfFgGopsxX?")) {
    check_exception("The format-spec should consume the input or end with a '}'",
                    std::basic_string_view{STR("{:") + c + STR("}")},
                    input);
  }
}

template <class CharT, class TestFunction, class TupleOrPair>
void test_escaping(TestFunction check, TupleOrPair&& input) {
  static_assert(std::same_as<std::remove_cvref_t<decltype(std::get<0>(input))>, CharT>);
  static_assert(std::same_as<std::remove_cvref_t<decltype(std::get<1>(input))>, std::basic_string<CharT>>);

  check(SV(R"(('*', ""))"), SV("{}"), input);

  // Char
  std::get<0>(input) = CharT('\t');
  check(SV(R"(('\t', ""))"), SV("{}"), input);
  std::get<0>(input) = CharT('\n');
  check(SV(R"(('\n', ""))"), SV("{}"), input);
  std::get<0>(input) = CharT('\0');
  check(SV(R"(('\u{0}', ""))"), SV("{}"), input);

  // String
  std::get<0>(input) = CharT('*');
  std::get<1>(input) = SV("hellö");
  check(SV("('*', \"hellö\")"), SV("{}"), input);
}

//
// pair tests
//

template <class CharT, class TestFunction, class ExceptionTest>
void test_pair_int_int(TestFunction check, ExceptionTest check_exception) {
  test_tuple_or_pair_int_int<CharT>(check, check_exception, std::make_pair(42, 99));
}

template <class CharT, class TestFunction, class ExceptionTest>
void test_pair_int_string(TestFunction check, ExceptionTest check_exception) {
  test_tuple_or_pair_int_string<CharT>(check, check_exception, std::make_pair(42, SV("hello")));
  test_tuple_or_pair_int_string<CharT>(check, check_exception, std::make_pair(42, STR("hello")));
  test_tuple_or_pair_int_string<CharT>(check, check_exception, std::make_pair(42, CSTR("hello")));
}

//
// tuple tests
//

template <class CharT, class TestFunction, class ExceptionTest>
void test_tuple_int(TestFunction check, ExceptionTest check_exception) {
  auto input = std::make_tuple(42);

  check(SV("(42)"), SV("{}"), input);

  // *** align-fill & width ***
  check(SV("(42)     "), SV("{:9}"), input);
  check(SV("(42)*****"), SV("{:*<9}"), input);
  check(SV("__(42)___"), SV("{:_^9}"), input);
  check(SV("#####(42)"), SV("{:#>9}"), input);

  check(SV("(42)     "), SV("{:{}}"), input, 9);
  check(SV("(42)*****"), SV("{:*<{}}"), input, 9);
  check(SV("__(42)___"), SV("{:_^{}}"), input, 9);
  check(SV("#####(42)"), SV("{:#>{}}"), input, 9);

  check_exception("The format-spec range-fill field contains an invalid character", SV("{:}<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{:{<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{::<}"), input);

  // *** sign ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:-}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:+}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{: }"), input);

  // *** alternate form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:#}"), input);

  // *** zero-padding ***
  check_exception("A format-spec width field shouldn't have a leading zero", SV("{:0}"), input);

  // *** precision ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), input);

  // *** locale-specific form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:L}"), input);

  // *** type ***
  check_exception("The format specifier m requires a pair or a two-element tuple", SV("{:m}"), input);
  check(SV("__42___"), SV("{:_^7n}"), input);

  for (CharT c : SV("aAbBcdeEfFgGopsxX?")) {
    check_exception("The format-spec should consume the input or end with a '}'",
                    std::basic_string_view{STR("{:") + c + STR("}")},
                    input);
  }
}

template <class CharT, class TestFunction, class ExceptionTest>
void test_tuple_int_string_color(TestFunction check, ExceptionTest check_exception) {
  const auto input = std::make_tuple(42, SV("hello"), color::red);

  check(SV("(42, \"hello\", \"red\")"), SV("{}"), input);

  // *** align-fill & width ***
  check(SV("(42, \"hello\", \"red\")     "), SV("{:25}"), input);
  check(SV("(42, \"hello\", \"red\")*****"), SV("{:*<25}"), input);
  check(SV("__(42, \"hello\", \"red\")___"), SV("{:_^25}"), input);
  check(SV("#####(42, \"hello\", \"red\")"), SV("{:#>25}"), input);

  check(SV("(42, \"hello\", \"red\")     "), SV("{:{}}"), input, 25);
  check(SV("(42, \"hello\", \"red\")*****"), SV("{:*<{}}"), input, 25);
  check(SV("__(42, \"hello\", \"red\")___"), SV("{:_^{}}"), input, 25);
  check(SV("#####(42, \"hello\", \"red\")"), SV("{:#>{}}"), input, 25);

  check_exception("The format-spec range-fill field contains an invalid character", SV("{:}<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{:{<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{::<}"), input);

  // *** sign ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:-}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:+}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{: }"), input);

  // *** alternate form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:#}"), input);

  // *** zero-padding ***
  check_exception("A format-spec width field shouldn't have a leading zero", SV("{:0}"), input);

  // *** precision ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), input);

  // *** locale-specific form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:L}"), input);

  // *** type ***
  check_exception("The format specifier m requires a pair or a two-element tuple", SV("{:m}"), input);
  check(SV("__42, \"hello\", \"red\"___"), SV("{:_^23n}"), input);

  for (CharT c : SV("aAbBcdeEfFgGopsxX?")) {
    check_exception("The format-spec should consume the input or end with a '}'",
                    std::basic_string_view{STR("{:") + c + STR("}")},
                    input);
  }
}

template <class CharT, class TestFunction, class ExceptionTest>
void test_tuple_int_int(TestFunction check, ExceptionTest check_exception) {
  test_tuple_or_pair_int_int<CharT>(check, check_exception, std::make_tuple(42, 99));
}

template <class CharT, class TestFunction, class ExceptionTest>
void test_tuple_int_string(TestFunction check, ExceptionTest check_exception) {
  test_tuple_or_pair_int_string<CharT>(check, check_exception, std::make_tuple(42, SV("hello")));
  test_tuple_or_pair_int_string<CharT>(check, check_exception, std::make_tuple(42, STR("hello")));
  test_tuple_or_pair_int_string<CharT>(check, check_exception, std::make_tuple(42, CSTR("hello")));
}

//
// nested tests
//

template <class CharT, class TestFunction, class ExceptionTest, class Nested>
void test_nested(TestFunction check, ExceptionTest check_exception, Nested&& input) {
  // [format.formatter.spec]/2
  //   A debug-enabled specialization of formatter additionally provides a
  //   public, constexpr, non-static member function set_debug_format()
  //   which modifies the state of the formatter to be as if the type of the
  //   std-format-spec parsed by the last call to parse were ?.
  // pair and tuple are not debug-enabled specializations to the
  // set_debug_format is not propagated. The paper
  //   P2733 Fix handling of empty specifiers in std::format
  // addressed this.

  check(SV("(42, (hello, red))"), SV("{}"), input);

  // *** align-fill & width ***
  check(SV("(42, (hello, red))     "), SV("{:23}"), input);
  check(SV("(42, (hello, red))*****"), SV("{:*<23}"), input);
  check(SV("__(42, (hello, red))___"), SV("{:_^23}"), input);
  check(SV("#####(42, (hello, red))"), SV("{:#>23}"), input);

  check(SV("(42, (hello, red))     "), SV("{:{}}"), input, 23);
  check(SV("(42, (hello, red))*****"), SV("{:*<{}}"), input, 23);
  check(SV("__(42, (hello, red))___"), SV("{:_^{}}"), input, 23);
  check(SV("#####(42, (hello, red))"), SV("{:#>{}}"), input, 23);

  check_exception("The format-spec range-fill field contains an invalid character", SV("{:}<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{:{<}"), input);
  check_exception("The format-spec range-fill field contains an invalid character", SV("{::<}"), input);

  // *** sign ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:-}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:+}"), input);
  check_exception("The format-spec should consume the input or end with a '}'", SV("{: }"), input);

  // *** alternate form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:#}"), input);

  // *** zero-padding ***
  check_exception("A format-spec width field shouldn't have a leading zero", SV("{:0}"), input);

  // *** precision ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), input);

  // *** locale-specific form ***
  check_exception("The format-spec should consume the input or end with a '}'", SV("{:L}"), input);

  // *** type ***
  check(SV("__42: (hello, red)___"), SV("{:_^21m}"), input);
  check(SV("__42, (hello, red)___"), SV("{:_^21n}"), input);

  for (CharT c : SV("aAbBcdeEfFgGopsxX?")) {
    check_exception("The format-spec should consume the input or end with a '}'",
                    std::basic_string_view{STR("{:") + c + STR("}")},
                    input);
  }
}

template <class CharT, class TestFunction, class ExceptionTest>
void run_tests(TestFunction check, ExceptionTest check_exception) {
  test_pair_int_int<CharT>(check, check_exception);
  test_pair_int_string<CharT>(check, check_exception);

  test_tuple_int<CharT>(check, check_exception);
  test_tuple_int_int<CharT>(check, check_exception);
  test_tuple_int_string<CharT>(check, check_exception);
  test_tuple_int_string_color<CharT>(check, check_exception);

  test_nested<CharT>(check, check_exception, std::make_pair(42, std::make_pair(SV("hello"), color::red)));
  test_nested<CharT>(check, check_exception, std::make_pair(42, std::make_tuple(SV("hello"), color::red)));
  test_nested<CharT>(check, check_exception, std::make_tuple(42, std::make_pair(SV("hello"), color::red)));
  test_nested<CharT>(check, check_exception, std::make_tuple(42, std::make_tuple(SV("hello"), color::red)));

  test_escaping<CharT>(check, std::make_pair(CharT('*'), STR("")));
  test_escaping<CharT>(check, std::make_tuple(CharT('*'), STR("")));

  // Test cvref-qualified types.
  // clang-format off
  check(SV("(42)"), SV("{}"), std::tuple<               int  >{42});
  check(SV("(42)"), SV("{}"), std::tuple<const          int  >{42});
  check(SV("(42)"), SV("{}"), std::tuple<      volatile int  >{42});
  check(SV("(42)"), SV("{}"), std::tuple<const volatile int  >{42});

  int answer = 42;
  check(SV("(42)"), SV("{}"), std::tuple<               int& >{answer});
  check(SV("(42)"), SV("{}"), std::tuple<const          int& >{answer});
  check(SV("(42)"), SV("{}"), std::tuple<      volatile int& >{answer});
  check(SV("(42)"), SV("{}"), std::tuple<const volatile int& >{answer});

  check(SV("(42)"), SV("{}"), std::tuple<               int&&>{42});
  check(SV("(42)"), SV("{}"), std::tuple<const          int&&>{42});
  check(SV("(42)"), SV("{}"), std::tuple<      volatile int&&>{42});
  check(SV("(42)"), SV("{}"), std::tuple<const volatile int&&>{42});
  // clang-format on
}

#endif // TEST_STD_UTILITIES_FORMAT_FORMAT_TUPLE_FORMAT_TESTS_H