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
|
// { dg-options "-std=gnu++20" }
// { dg-do run { target c++20 } }
#include <format>
#ifndef __cpp_lib_format
# error "Feature test macro for std::format is missing in <format>"
#elif __cpp_lib_format < 202106L
# error "Feature test macro for std::format has wrong value in <format>"
#endif
#undef __cpp_lib_format
#include <version>
#ifndef __cpp_lib_format
# error "Feature test macro for std::format is missing in <version>"
#elif __cpp_lib_format < 202106L
# error "Feature test macro for std::format has wrong value in <version>"
#endif
#include <string>
#include <limits>
#include <cstdint>
#include <cstdio>
#include <testsuite_hooks.h>
void
test_no_args()
{
std::string s;
s = std::format("disco");
VERIFY( s == "disco" );
s = std::format("}} machine {{ funk }} specialists {{");
VERIFY( s == "} machine { funk } specialists {" );
s = std::format("128bpm }}");
VERIFY( s == "128bpm }" );
}
void
test_unescaped()
{
#ifdef __cpp_exceptions
for (auto f : { "{", "}", "{{{", "{{}", "}{", "{{{{{" })
try {
(void) std::vformat(f, std::make_format_args());
VERIFY( false );
} catch (const std::format_error& e) {
std::string what = e.what();
VERIFY( what.find("unmatched") != what.npos );
}
#endif
}
struct brit_punc : std::numpunct<char>
{
std::string do_grouping() const override { return "\3\3"; }
char do_thousands_sep() const override { return ','; }
std::string do_truename() const override { return "yes mate"; }
std::string do_falsename() const override { return "nah bruv"; }
};
void
test_std_examples()
{
using namespace std;
string s = format("{0}-{{", 8); // value of s is "8-{"
VERIFY( s == "8-{" );
// align
{
char c = 120;
string s0 = format("{:6}", 42);
VERIFY(s0 == " 42");
string s1 = format("{:6}", 'x');
VERIFY(s1 == "x ");
string s2 = format("{:*<6}", 'x');
VERIFY(s2 == "x*****");
string s3 = format("{:*>6}", 'x');
VERIFY(s3 == "*****x");
string s4 = format("{:*^6}", 'x');
VERIFY(s4 == "**x***");
string s5 = format("{:6d}", c);
VERIFY(s5 == " 120");
string s6 = format("{:6}", true);
VERIFY(s6 == "true ");
}
// sign
{
double inf = numeric_limits<double>::infinity();
double nan = numeric_limits<double>::quiet_NaN();
string s0 = format("{0:},{0:+},{0:-},{0: }", 1);
VERIFY(s0 == "1,+1,1, 1");
string s1 = format("{0:},{0:+},{0:-},{0: }", -1);
VERIFY(s1 == "-1,-1,-1,-1");
string s2 = format("{0:},{0:+},{0:-},{0: }", inf);
VERIFY(s2 == "inf,+inf,inf, inf");
string s3 = format("{0:},{0:+},{0:-},{0: }", nan);
VERIFY(s3 == "nan,+nan,nan, nan");
}
// alternate form and zero fill
{
char c = 120;
string s1 = format("{:+06d}", c);
VERIFY(s1 == "+00120");
string s2 = format("{:#06x}", 0xa);
VERIFY(s2 == "0x000a");
string s3 = format("{:<06}", -42);
VERIFY(s3 == "-42 "); // 0 is ignored because of < alignment
}
// integer presentation types
{
// Change global locale so "{:L}" adds digit separators.
std::locale::global(std::locale({}, new brit_punc));
string s0 = format("{}", 42);
VERIFY(s0 == "42");
string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42);
VERIFY(s1 == "101010 42 52 2a");
string s2 = format("{0:#x} {0:#X}", 42);
VERIFY(s2 == "0x2a 0X2A");
string s3 = format("{:L}", 1234);
VERIFY(s3 == "1,234");
// Test locale's "byte-and-a-half" grouping (Imperial word? tribble?).
string s4 = format("{:#Lx}", 0xfffff);
VERIFY(s4 == "0xff,fff");
// Restore
std::locale::global(std::locale::classic());
}
}
void
test_alternate_forms()
{
std::string s;
s = std::format("{0:#b} {0:+#B} {0:#o} {0:#x} {0:+#X} {0: #d}", 42);
VERIFY( s == "0b101010 +0B101010 052 0x2a +0X2A 42" );
s = std::format("{0:#b} {0:+#B} {0:#o} {0:#x} {0:+#X} {0: #d}", 0);
VERIFY( s == "0b0 +0B0 0 0x0 +0X0 0" );
s = std::format("{0:+#012g} {0:+#014g} {0:+#014g}", 1234.0);
VERIFY( s == "+00001234.00 +0000001234.00 +0000001234.00" );
s = std::format("{0:+#0{1}g} {0:+#0{2}g} {0:+#0{2}g}", 1234.5, 12, 14);
VERIFY( s == "+00001234.50 +0000001234.50 +0000001234.50" );
s = std::format("{:#.2g}", -0.0);
VERIFY( s == "-0.0" );
}
struct euro_punc : std::numpunct<char>
{
std::string do_grouping() const override { return "\3\3"; }
char do_thousands_sep() const override { return '.'; }
char do_decimal_point() const override { return ','; }
};
void
test_locale()
{
// The default C locale.
std::locale cloc = std::locale::classic();
// A custom locale using comma digit separators.
std::locale bloc(cloc, new brit_punc);
// A custom locale using period digit separators.
std::locale eloc(cloc, new euro_punc);
std::string s;
// Change the global locale:
std::locale::global(bloc);
// Format using the global locale:
s = std::format("{0:L} {0:Lx} {0:Lb}", 12345);
VERIFY( s == "12,345 3,039 11,000,000,111,001" );
s = std::format("{0:L} {0:.7Lg} {0:La}", 12345.6789);
VERIFY( s == "12,345.6789 12,345.68 1.81cd6e631f8a1p+13" );
s = std::format("{0:s} {0:L} {1:Ls} {0:Ld}", true, false);
VERIFY( s == "true yes mate nah bruv 1" );
// Format using a specific locale:
s = std::format(eloc, "{0:L} {0:Lx} {0:Lb}", 12345);
VERIFY( s == "12.345 3.039 11.000.000.111.001" );
s = std::format(eloc, "{0:L} {0:.7LG} {0:La}", 12345.6789);
VERIFY( s == "12.345,6789 12.345,68 1,81cd6e631f8a1p+13" );
s = std::format(eloc, "{0:#Lg} {0:+#.3Lg} {0:#08.4Lg}", -1234.);
VERIFY( s == "-1.234,00 -1,23e+03 -01.234," );
// Restore
std::locale::global(cloc);
}
void
test_width()
{
std::string s;
s = std::format("{:4}", "");
VERIFY( s == " " );
s = std::format("{:{}}", "", 3);
VERIFY( s == " " );
s = std::format("{1:{0}}", 2, "");
VERIFY( s == " " );
s = std::format("{:03}", 9);
VERIFY( s == "009" );
s = std::format("DR {0:{1}}: allow width {1} from arg-id", 3721, 0);
VERIFY( s == "DR 3721: allow width 0 from arg-id" );
try {
s = std::format("Negative width is an error: {0:{1}}", 123, -1);
VERIFY(false);
} catch (const std::format_error&) {
}
try {
auto args = std::make_format_args(false, true);
s = std::vformat("DR 3720: restrict type of width arg-id {0:{1}}", args);
VERIFY(false);
} catch (const std::format_error&) {
}
try {
auto args = std::make_format_args('?', '!');
s = std::vformat("DR 3720: restrict type of width arg-id {0:{1}}", args);
VERIFY(false);
} catch (const std::format_error&) {
}
}
void
test_wchar()
{
using namespace std::literals;
std::wstring s;
s = std::format(L"{} {} {} {} {} {}", L'0', 1, 2LL, 3.4, L"five", L"six"s);
VERIFY( s == L"0 1 2 3.4 five six" );
std::locale loc;
s = std::format(loc, L"{:L} {:.3s}{:Lc}", true, L"data"sv, '.');
VERIFY( s == L"true dat." );
}
void
test_minmax()
{
auto check = []<typename T, typename U = std::make_unsigned_t<T>>(T, U = 0) {
const int digits = std::numeric_limits<T>::digits;
const std::string zeros(digits, '0');
const std::string ones(digits, '1');
auto s = std::format("{:b}" , std::numeric_limits<T>::min());
VERIFY( s == "-1" + zeros );
s = std::format("{:b}" , std::numeric_limits<T>::max());
VERIFY( s == ones );
s = std::format("{:0{}b}" , std::numeric_limits<U>::min(), digits + 1);
VERIFY( s == '0' + zeros );
s = std::format("{:b}" , std::numeric_limits<U>::max());
VERIFY( s == '1' + ones );
};
check(std::int8_t(0));
check(std::int16_t(0));
check(std::int32_t(0));
check(std::int64_t(0));
#ifdef __SIZEOF_INT128__
// std::make_unsigned_t<__int128> is invalid for strict -std=c++20 mode,
// so pass a second argument of the unsigned type.
check(__int128(0), (unsigned __int128)(0));
#endif
}
void
test_p1652r1() // printf corner cases in std::format
{
std::string s;
// Problem 1: "#o" specification should not print 0 as "00"
s = std::format("{:#o}", 0);
VERIFY( s == "0" );
// Problem 2: 'c' should be able to print 65 as "A" (ASCII)
int c = 'A';
s = std::format("{:c}", c);
VERIFY( s == "A" );
// Problem 3: "-000nan" is not a floating point value
double nan = std::numeric_limits<double>::quiet_NaN();
try {
s = std::vformat("{:0=6}", std::make_format_args(nan));
VERIFY( false );
} catch (const std::format_error&) {
}
s = std::format("{:06}", nan);
VERIFY( s == " nan" );
// Problem 4: bool needs a type format specifier
s = std::format("{:s}", true);
VERIFY( s == "true" );
// Problem 5: double does not roundtrip float
s = std::format("{}", 3.31f);
VERIFY( s == "3.31" );
}
template<typename T>
bool format_float()
{
auto s = std::format("{:#} != {:<+7.3f}", (T)-0.0, (T)0.5);
return s == "-0. != +0.500 ";
}
#if __cplusplus > 202002L
template<typename T>
concept formattable = std::formattable<T, char>;
#else
template<typename T>
concept formattable = requires (T t, char* p) { std::to_chars(p, p, t); };
#endif
void
test_float128()
{
#ifdef __SIZEOF_FLOAT128__
if constexpr (formattable<__float128>)
VERIFY( format_float<__float128>() );
else
std::puts("Cannot format __float128 on this target");
#endif
#if __FLT128_DIG__
if constexpr (formattable<_Float128>)
VERIFY( format_float<_Float128>() );
else
std::puts("Cannot format _Float128 on this target");
#endif
}
int main()
{
test_no_args();
test_unescaped();
test_std_examples();
test_alternate_forms();
test_locale();
test_width();
test_wchar();
test_minmax();
test_p1652r1();
test_float128();
}
|