]>
gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/testsuite/std/format/functions/format.cc
1 // { dg-options "-std=gnu++20" }
2 // { dg-do run { target c++20 } }
9 #include <testsuite_hooks.h>
15 s
= std::format("disco");
16 VERIFY( s
== "disco" );
18 s
= std::format("}} machine {{ funk }} specialists {{");
19 VERIFY( s
== "} machine { funk } specialists {" );
21 s
= std::format("128bpm }}");
22 VERIFY( s
== "128bpm }" );
28 #ifdef __cpp_exceptions
29 for (auto f
: { "{", "}", "{{{", "{{}", "}{", "{{{{{" })
31 (void) std::vformat(f
, std::make_format_args());
33 } catch (const std::format_error
& e
) {
34 std::string what
= e
.what();
35 VERIFY( what
.find("unmatched") != what
.npos
);
40 struct brit_punc
: std::numpunct
<char>
42 std::string
do_grouping() const override
{ return "\3\3"; }
43 char do_thousands_sep() const override
{ return ','; }
44 std::string
do_truename() const override
{ return "yes mate"; }
45 std::string
do_falsename() const override
{ return "nah bruv"; }
53 string s
= format("{0}-{{", 8); // value of s is "8-{"
59 string s0
= format("{:6}", 42);
61 string s1
= format("{:6}", 'x');
63 string s2
= format("{:*<6}", 'x');
64 VERIFY(s2
== "x*****");
65 string s3
= format("{:*>6}", 'x');
66 VERIFY(s3
== "*****x");
67 string s4
= format("{:*^6}", 'x');
68 VERIFY(s4
== "**x***");
69 string s5
= format("{:6d}", c
);
71 string s6
= format("{:6}", true);
72 VERIFY(s6
== "true ");
77 double inf
= numeric_limits
<double>::infinity();
78 double nan
= numeric_limits
<double>::quiet_NaN();
79 string s0
= format("{0:},{0:+},{0:-},{0: }", 1);
80 VERIFY(s0
== "1,+1,1, 1");
81 string s1
= format("{0:},{0:+},{0:-},{0: }", -1);
82 VERIFY(s1
== "-1,-1,-1,-1");
83 string s2
= format("{0:},{0:+},{0:-},{0: }", inf
);
84 VERIFY(s2
== "inf,+inf,inf, inf");
85 string s3
= format("{0:},{0:+},{0:-},{0: }", nan
);
86 VERIFY(s3
== "nan,+nan,nan, nan");
89 // alternate form and zero fill
92 string s1
= format("{:+06d}", c
);
93 VERIFY(s1
== "+00120");
94 string s2
= format("{:#06x}", 0xa);
95 VERIFY(s2
== "0x000a");
96 string s3
= format("{:<06}", -42);
97 VERIFY(s3
== "-42 "); // 0 is ignored because of < alignment
100 // integer presentation types
102 // Change global locale so "{:L}" adds digit separators.
103 std::locale::global(std::locale({}, new brit_punc
));
105 string s0
= format("{}", 42);
107 string s1
= format("{0:b} {0:d} {0:o} {0:x}", 42);
108 VERIFY(s1
== "101010 42 52 2a");
109 string s2
= format("{0:#x} {0:#X}", 42);
110 VERIFY(s2
== "0x2a 0X2A");
111 string s3
= format("{:L}", 1234);
112 VERIFY(s3
== "1,234");
115 std::locale::global(std::locale::classic());
120 test_alternate_forms()
124 s
= std::format("{0:#b} {0:+#B} {0:#o} {0:#x} {0:+#X} {0: #d}", 42);
125 VERIFY( s
== "0b101010 +0B101010 052 0x2a +0X2A 42" );
126 s
= std::format("{0:#b} {0:+#B} {0:#o} {0:#x} {0:+#X} {0: #d}", 0);
127 VERIFY( s
== "0b0 +0B0 0 0x0 +0X0 0" );
129 s
= std::format("{0:+#012g} {0:+#014g} {0:+#014g}", 1234.0);
130 VERIFY( s
== "+00001234.00 +0000001234.00 +0000001234.00" );
131 s
= std::format("{0:+#0{1}g} {0:+#0{2}g} {0:+#0{2}g}", 1234.5, 12, 14);
132 VERIFY( s
== "+00001234.50 +0000001234.50 +0000001234.50" );
134 s
= std::format("{:#.2g}", -0.0);
135 VERIFY( s
== "-0.0" );
138 struct euro_punc
: std::numpunct
<char>
140 std::string
do_grouping() const override
{ return "\3\3"; }
141 char do_thousands_sep() const override
{ return '.'; }
142 char do_decimal_point() const override
{ return ','; }
148 // The default C locale.
149 std::locale cloc
= std::locale::classic();
150 // A custom locale using comma digit separators.
151 std::locale
bloc(cloc
, new brit_punc
);
152 // A custom locale using period digit separators.
153 std::locale
eloc(cloc
, new euro_punc
);
157 // Change the global locale:
158 std::locale::global(bloc
);
159 // Format using the global locale:
160 s
= std::format("{0:L} {0:Lx} {0:Lb}", 12345);
161 VERIFY( s
== "12,345 3,039 11,000,000,111,001" );
162 s
= std::format("{0:L} {0:.7Lg} {0:La}", 12345.6789);
163 VERIFY( s
== "12,345.6789 12,345.68 1.81cd6e631f8a1p+13" );
165 s
= std::format("{0:s} {0:L} {1:Ls} {0:Ld}", true, false);
166 VERIFY( s
== "true yes mate nah bruv 1" );
168 // Format using a specific locale:
169 s
= std::format(eloc
, "{0:L} {0:Lx} {0:Lb}", 12345);
170 VERIFY( s
== "12.345 3.039 11.000.000.111.001" );
171 s
= std::format(eloc
, "{0:L} {0:.7LG} {0:La}", 12345.6789);
172 VERIFY( s
== "12.345,6789 12.345,68 1,81cd6e631f8a1p+13" );
174 s
= std::format(eloc
, "{0:#Lg} {0:+#.3Lg} {0:#08.4Lg}", -1234.);
175 VERIFY( s
== "-1.234,00 -1,23e+03 -01.234," );
178 std::locale::global(cloc
);
186 s
= std::format("{:4}", "");
188 s
= std::format("{:{}}", "", 3);
190 s
= std::format("{1:{0}}", 2, "");
192 s
= std::format("{:03}", 9);
193 VERIFY( s
== "009" );
195 s
= std::format("DR {0:{1}}: allow width {1} from arg-id", 3721, 0);
196 VERIFY( s
== "DR 3721: allow width 0 from arg-id" );
199 s
= std::format("Negative width is an error: {0:{1}}", 123, -1);
201 } catch (const std::format_error
&) {
205 auto args
= std::make_format_args(false, true);
206 s
= std::vformat("DR 3720: restrict type of width arg-id {0:{1}}", args
);
208 } catch (const std::format_error
&) {
212 auto args
= std::make_format_args('?', '!');
213 s
= std::vformat("DR 3720: restrict type of width arg-id {0:{1}}", args
);
215 } catch (const std::format_error
&) {
222 using namespace std::literals
;
225 s
= std::format(L
"{} {} {} {} {} {}", L
'0', 1, 2LL, 3.4, L
"five", L
"six"s
);
226 VERIFY( s
== L
"0 1 2 3.4 five six" );
229 s
= std::format(loc
, L
"{:L} {:.3s}{:Lc}", true, L
"data"sv
, '.');
230 VERIFY( s
== L
"true dat." );
236 auto check
= []<typename T
>(T
) {
237 const int digits
= std::numeric_limits
<T
>::digits
;
238 const std::string
zeros(digits
, '0');
239 const std::string
ones(digits
, '1');
240 auto s
= std::format("{:b}" , std::numeric_limits
<T
>::min());
241 VERIFY( s
== "-1" + zeros
);
242 s
= std::format("{:b}" , std::numeric_limits
<T
>::max());
244 using U
= std::make_unsigned_t
<T
>;
245 s
= std::format("{:0{}b}" , std::numeric_limits
<U
>::min(), digits
+ 1);
246 VERIFY( s
== '0' + zeros
);
247 s
= std::format("{:b}" , std::numeric_limits
<U
>::max());
248 VERIFY( s
== '1' + ones
);
250 check(std::int8_t(0));
251 check(std::int16_t(0));
252 check(std::int32_t(0));
253 check(std::int64_t(0));
254 #ifdef __SIZEOF_INT128__
260 test_p1652r1() // printf corner cases in std::format
264 // Problem 1: "#o" specification should not print 0 as "00"
265 s
= std::format("{:#o}", 0);
268 // Problem 2: 'c' should be able to print 65 as "A" (ASCII)
270 s
= std::format("{:c}", c
);
273 // Problem 3: "-000nan" is not a floating point value
274 double nan
= std::numeric_limits
<double>::quiet_NaN();
276 s
= std::vformat("{:0=6}", std::make_format_args(nan
));
278 } catch (const std::format_error
&) {
281 s
= std::format("{:06}", nan
);
282 VERIFY( s
== " nan" );
284 // Problem 4: bool needs a type format specifier
285 s
= std::format("{:s}", true);
286 VERIFY( s
== "true" );
288 // Problem 5: double does not roundtrip float
289 s
= std::format("{}", 3.31f
);
290 VERIFY( s
== "3.31" );
296 auto s
= std::format("{:#} != {:<+7.3f}", (T
)-0.0, (T
)0.5);
297 return s
== "-0. != +0.500 ";
303 #ifdef __SIZEOF_FLOAT128__
304 if constexpr (std::formattable
<__float128
, char>)
305 VERIFY( format_float
<__float128
>() );
307 std::puts("Cannot format __float128 on this target");
310 if constexpr (std::formattable
<_Float128
, char>)
311 VERIFY( format_float
<_Float128
>() );
313 std::puts("Cannot format _Float128 on this target");
322 test_alternate_forms();
This page took 0.053624 seconds and 5 git commands to generate.