This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add quadruple precision tests, verified by Craig Berry.
[perl5.git] / t / op / sprintf2.t
CommitLineData
1d917b39
RGS
1#!./perl -w
2
6715405f
FC
3# Tests for sprintf that do not fit the format of sprintf.t.
4
1d917b39
RGS
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8 require './test.pl';
9}
10
43d7f0da
AB
11# We'll run 12 extra tests (see below) if $Q is false.
12eval { my $q = pack "q", 0 };
13my $Q = $@ eq '';
14
40bca5ae
JH
15# %a and %A depend on the floating point config
16# This totally doesn't test non-IEEE-754 float formats.
17my @hexfloat;
18print "# uvsize = $Config{uvsize}\n";
19print "# nvsize = $Config{nvsize}\n";
20print "# nv_preserves_uv_bits = $Config{nv_preserves_uv_bits}\n";
21print "# d_quad = $Config{d_quad}\n";
22if ($Config{nvsize} == 8 &&
23 (
f7a48a8c 24 # IEEE-754 64-bit ("double precision"), the most common out there
40bca5ae
JH
25 ($Config{uvsize} == 8 && $Config{nv_preserves_uv_bits} == 53)
26 ||
c1b97dd3
JH
27 # If we have a quad we can still get the mantissa bits.
28 ($Config{uvsize} == 4 && $Config{d_quad})
40bca5ae
JH
29 )
30 ) {
f7a48a8c 31 @hexfloat = (
40bca5ae
JH
32 [ '%a', '0', '0x0p+0' ],
33 [ '%a', '1', '0x1p+0' ],
34 [ '%a', '1.0', '0x1p+0' ],
35 [ '%a', '0.5', '0x1p-1' ],
36 [ '%a', '0.25', '0x1p-2' ],
37 [ '%a', '0.75', '0x1.8p-1' ],
38 [ '%a', '3.14', '0x1.91eb851eb851fp+1' ],
39 [ '%a', '-1.0', '-0x1p+0' ],
40 [ '%a', '-3.14', '-0x1.91eb851eb851fp+1' ],
41 [ '%a', '0.1', '0x1.999999999999ap-4' ],
42 [ '%a', '1/7', '0x1.2492492492492p-3' ],
43 [ '%a', 'sqrt(2)', '0x1.6a09e667f3bcdp+0' ],
44 [ '%a', 'exp(1)', '0x1.5bf0a8b145769p+1' ],
45 [ '%a', '2**-10', '0x1p-10' ],
46 [ '%a', '2**10', '0x1p+10' ],
47 [ '%a', '1e-9', '0x1.12e0be826d695p-30' ],
48 [ '%a', '1e9', '0x1.dcd65p+29' ],
49
50 [ '%#a', '1', '0x1.p+0' ],
51 [ '%+a', '1', '+0x1p+0' ],
52 [ '%+a', '-1', '-0x1p+0' ],
53 [ '% a', ' 1', ' 0x1p+0' ],
54 [ '% a', '-1', '-0x1p+0' ],
55
56 [ '%8a', '3.14', '0x1.91eb851eb851fp+1' ],
57 [ '%13a', '3.14', '0x1.91eb851eb851fp+1' ],
58 [ '%20a', '3.14', '0x1.91eb851eb851fp+1' ],
59 [ '%.4a', '3.14', '0x1.91ecp+1' ],
60 [ '%.5a', '3.14', '0x1.91eb8p+1' ],
61 [ '%.6a', '3.14', '0x1.91eb85p+1' ],
62 [ '%.20a', '3.14', '0x1.91eb851eb851f0000000p+1' ],
63 [ '%20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
64 [ '%20.15a', '3.14', '0x1.91eb851eb851f00p+1' ],
65 [ '% 20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
66 [ '%020.10a', '3.14', '0x0001.91eb851eb8p+1' ],
67
68 [ '%30a', '3.14', ' 0x1.91eb851eb851fp+1' ],
69 [ '%-30a', '3.14', '0x1.91eb851eb851fp+1 ' ],
70 [ '%030a', '3.14', '0x00000000001.91eb851eb851fp+1' ],
71 [ '%-030a', '3.14', '0x1.91eb851eb851fp+1 ' ],
72
f7a48a8c
JH
73 [ '%.40a', '3.14',
74 '0x1.91eb851eb851f000000000000000000000000000p+1' ],
75
40bca5ae
JH
76 [ '%A', '3.14', '0X1.91EB851EB851FP+1' ],
77 );
c1b97dd3 78} elsif (($Config{nvsize} == 16 || $Config{nvsize} == 12) &&
f7a48a8c
JH
79 # 80-bit ("extended precision") long double, pack F is the NV
80 # cd cc cc cc cc cc cc cc fb bf 00 00 00 00 00 00
81 # cd cc cc cc cc cc cc cc fb bf 00 00
c1b97dd3
JH
82 (pack("F", 0.1) =~ /^\xCD/ || # LE
83 pack("F", 0.1) =~ /\xCD$/)) { # BE (if this ever happens)
f7a48a8c 84 @hexfloat = (
40bca5ae
JH
85 [ '%a', '0', '0x0p+0' ],
86 [ '%a', '1', '0x8p-3' ],
87 [ '%a', '1.0', '0x8p-3' ],
88 [ '%a', '0.5', '0x8p-4' ],
89 [ '%a', '0.25', '0x8p-5' ],
90 [ '%a', '0.75', '0xcp-4' ],
91 [ '%a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
92 [ '%a', '-1.0', '-0x8p-3' ],
93 [ '%a', '-3.14', '-0xc.8f5c28f5c28f5c3p-2' ],
94 [ '%a', '0.1', '0xc.ccccccccccccccdp-7' ],
95 [ '%a', '1/7', '0x9.249249249249249p-6' ],
96 [ '%a', 'sqrt(2)', '0xb.504f333f9de6484p-3' ],
97 [ '%a', 'exp(1)', '0xa.df85458a2bb4a9bp-2' ],
98 [ '%a', '2**-10', '0x8p-13' ],
99 [ '%a', '2**10', '0x8p+7' ],
100 [ '%a', '1e-9', '0x8.9705f4136b4a597p-33' ],
101 [ '%a', '1e9', '0xe.e6b28p+26' ],
102
103 [ '%#a', '1', '0x8.p-3' ],
104 [ '%+a', '1', '+0x8p-3' ],
105 [ '%+a', '-1', '-0x8p-3' ],
106 [ '% a', ' 1', ' 0x8p-3' ],
107 [ '% a', '-1', '-0x8p-3' ],
108
109 [ '%8a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
110 [ '%13a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
111 [ '%20a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
112 [ '%.4a', '3.14', '0xc.8f5cp-2' ],
113 [ '%.5a', '3.14', '0xc.8f5c3p-2' ],
114 [ '%.6a', '3.14', '0xc.8f5c29p-2' ],
115 [ '%.20a', '3.14', '0xc.8f5c28f5c28f5c300000p-2' ],
116 [ '%20.10a', '3.14', ' 0xc.8f5c28f5c3p-2' ],
117 [ '%20.15a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
118 [ '% 20.10a', '3.14', ' 0xc.8f5c28f5c3p-2' ],
119 [ '%020.10a', '3.14', '0x000c.8f5c28f5c3p-2' ],
120
121 [ '%30a', '3.14', ' 0xc.8f5c28f5c28f5c3p-2' ],
122 [ '%-30a', '3.14', '0xc.8f5c28f5c28f5c3p-2 ' ],
123 [ '%030a', '3.14', '0x00000000c.8f5c28f5c28f5c3p-2' ],
124 [ '%-030a', '3.14', '0xc.8f5c28f5c28f5c3p-2 ' ],
125
f7a48a8c
JH
126 [ '%.40a', '3.14',
127 '0xc.8f5c28f5c28f5c30000000000000000000000000p-2' ],
128
40bca5ae
JH
129 [ '%A', '3.14', '0XC.8F5C28F5C28F5C3P-2' ],
130 );
f7a48a8c
JH
131} elsif (
132 # IEEE 754 128-bit ("quadruple precision"), e.g. IA-64 (Itanium) in VMS
133 $Config{nvsize} == 16 &&
134 # 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb 3f (LE), pack F is the NV
135 # (compare this with "double-double")
136 (pack("F", 0.1) =~ /^\x9A\x99{6}/ || # LE
137 pack("F", 0.1) =~ /\x99{6}x9A$/) # BE
138 ) {
139 @hexfloat = (
140 [ '%a', '0', '0x1p-1' ],
141 [ '%a', '1', '0x1p+0' ],
142 [ '%a', '1.0', '0x1p+0' ],
143 [ '%a', '0.5', '0x1p-1' ],
144 [ '%a', '0.25', '0x1p-2' ],
145 [ '%a', '0.75', '0x1.8p-1' ],
146 [ '%a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
147 [ '%a', '-1', '-0x1p+0' ],
148 [ '%a', '-3.14', '-0x1.91eb851eb851eb851eb851eb851fp+1' ],
149 [ '%a', '0.1', '0x1.999999999999999999999999999ap-4' ],
150 [ '%a', '1/7', '0x1.2492492492492492492492492492p-3' ],
151 [ '%a', 'sqrt(2)', '0x1.6a09e667f3bcc908b2fb1366ea95p+0' ],
152 [ '%a', 'exp(1)', '0x1.5bf0a8b1457695355fb8ac404e7ap+1' ],
153 [ '%a', '2**-10', '0x1p-10' ],
154 [ '%a', '2**10', '0x1p+10' ],
155 [ '%a', '1e-09', '0x1.12e0be826d694b2e62d01511f12ap-30' ],
156 [ '%a', '1e9', '0x1.dcd65p+29' ],
157
158 [ '%#a', '1', '0x1.p+0' ],
159 [ '%+a', '1', '+0x1p+0' ],
160 [ '%+a', '-1', '-0x1p+0' ],
161 [ '% a', '1', ' 0x1p+0' ],
162 [ '% a', '-1', '-0x1p+0' ],
163
164 [ '%8a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
165 [ '%13a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
166 [ '%20a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
167 [ '%.4a', '3.14', '0x1.91ecp+1' ],
168 [ '%.5a', '3.14', '0x1.91eb8p+1' ],
169 [ '%.6a', '3.14', '0x1.91eb85p+1' ],
170 [ '%.20a', '3.14', '0x1.91eb851eb851eb851eb8p+1' ],
171 [ '%20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
172 [ '%20.15a', '3.14', '0x1.91eb851eb851eb8p+1' ],
173 [ '% 20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
174 [ '%020.10a', '3.14', '0x0001.91eb851eb8p+1' ],
175
176 [ '%30a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
177 [ '%-30a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
178 [ '%030a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
179 [ '%-030a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
180
181 [ '%.40a', '3.14',
182 '0x1.91eb851eb851eb851eb851eb851f000000000000p+1' ],
183
184 [ '%A', '3.14', '0X1.91EB851EB851EB851EB851EB851FP+1' ],
185 );
186} elsif (
187 # "double-double", two 64-bit doubles end to end
188 $Config{nvsize} == 16 &&
189 # bf b9 99 99 99 99 99 9a 3c 59 99 99 99 99 99 9a (BE), pack F is the NV
190 # (compare this with "quadruple precision")
191 (pack("F", 0.1) =~ /^\x9A\x99{5}\x59\x3C/ || # LE
192 pack("F", 0.1) =~ /\x3C\x59\x99{5}\x9A$/) # BE
193 ) {
194 # XXX these values are probably slightly wrong, even if
195 # the double-double extraction code gets fixed, the exact
196 # truncation/rounding effects are unknown.
197 @hexfloat = (
198 [ '%a', '0', '0x1p-1' ],
199 [ '%a', '1', '0x1p+0' ],
200 [ '%a', '1.0', '0x1p+0' ],
201 [ '%a', '0.5', '0x1p-1' ],
202 [ '%a', '0.25', '0x1p-2' ],
203 [ '%a', '0.75', '0x1.8p-1' ],
204 [ '%a', '3.14', '0x1.91eb851eb851eb851eb851eb852p+1' ],
205 [ '%a', '-1', '-0x1p+0' ],
206 [ '%a', '-3.14', '-0x1.91eb851eb851eb851eb851eb852p+1' ],
207 [ '%a', '0.1', '0x1.99999999999999999999999999ap-4' ],
208 [ '%a', '1/7', '0x1.249249249249249249249249249p-3' ],
209 [ '%a', 'sqrt(2)', '0x1.6a09e667f3bcc908b2fb1366ea9p+0' ],
210 [ '%a', 'exp(1)', '0x1.5bf0a8b1457695355fb8ac404e8p+1' ],
211 [ '%a', '2**-10', '0x1p-10' ],
212 [ '%a', '2**10', '0x1p+10' ],
213 [ '%a', '1e-09', '0x1.12e0be826d694b2e62d01511f13p-30' ],
214 [ '%a', '1e9', '0x1.dcd65p+29' ],
215
216 [ '%#a', '1', '0x1.p+0' ],
217 [ '%+a', '1', '+0x1p+0' ],
218 [ '%+a', '-1', '-0x1p+0' ],
219 [ '% a', '1', ' 0x1p+0' ],
220 [ '% a', '-1', '-0x1p+0' ],
221
222 [ '%8a', '3.14', '0x1.91eb851eb851eb851eb851eb852p+1' ],
223 [ '%13a', '3.14', '0x1.91eb851eb851eb851eb851eb852p+1' ],
224 [ '%20a', '3.14', '0x1.91eb851eb851eb851eb851eb852p+1' ],
225 [ '%.4a', '3.14', '0x1.91ecp+1' ],
226 [ '%.5a', '3.14', '0x1.91eb8p+1' ],
227 [ '%.6a', '3.14', '0x1.91eb85p+1' ],
228 [ '%.20a', '3.14', '0x1.91eb851eb851eb851eb8p+1' ],
229 [ '%20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
230 [ '%20.15a', '3.14', '0x1.91eb851eb851eb8p+1' ],
231 [ '% 20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
232 [ '%020.10a', '3.14', '0x0001.91eb851eb8p+1' ],
233
234 [ '%30a', '3.14', '0x1.91eb851eb851eb851eb851eb852p+1' ],
235 [ '%-30a', '3.14', '0x1.91eb851eb851eb851eb851eb852p+1' ],
236 [ '%030a', '3.14', '0x1.91eb851eb851eb851eb851eb852p+1' ],
237 [ '%-030a', '3.14', '0x1.91eb851eb851eb851eb851eb852p+1' ],
238
239 [ '%.40a', '3.14',
240 '0x1.91eb851eb851eb851eb851eb8520000000000000p+1' ],
241
242 [ '%A', '3.14', '0X1.91EB851EB851EB851EB851EB852P+1' ],
243 );
40bca5ae
JH
244} else {
245 print "# no hexfloat tests\n";
246}
247
248plan tests => 1406 + ($Q ? 0 : 12) + @hexfloat;
53f65a9e
HS
249
250use strict;
251use Config;
1d917b39
RGS
252
253is(
254 sprintf("%.40g ",0.01),
255 sprintf("%.40g", 0.01)." ",
256 q(the sprintf "%.<number>g" optimization)
257);
258is(
259 sprintf("%.40f ",0.01),
260 sprintf("%.40f", 0.01)." ",
261 q(the sprintf "%.<number>f" optimization)
262);
cc61b222
ST
263
264# cases of $i > 1 are against [perl #39126]
265for my $i (1, 5, 10, 20, 50, 100) {
266 chop(my $utf8_format = "%-*s\x{100}");
267 my $string = "\xB4"x$i; # latin1 ACUTE or ebcdic COPYRIGHT
268 my $expect = $string." "x$i; # followed by 2*$i spaces
269 is(sprintf($utf8_format, 3*$i, $string), $expect,
270 "width calculation under utf8 upgrade, length=$i");
6c94ec8b 271}
fc7325bb 272
59b61096
AV
273# check simultaneous width & precision with wide characters
274for my $i (1, 3, 5, 10) {
275 my $string = "\x{0410}"x($i+10); # cyrillic capital A
276 my $expect = "\x{0410}"x$i; # cut down to exactly $i characters
277 my $format = "%$i.${i}s";
278 is(sprintf($format, $string), $expect,
279 "width & precision interplay with utf8 strings, length=$i");
280}
281
fc7325bb 282# Used to mangle PL_sv_undef
7baa4690 283fresh_perl_like(
fc7325bb 284 'print sprintf "xxx%n\n"; print undef',
aaa63dae 285 qr/Modification of a read-only value attempted at - line 1\./,
fc7325bb
GA
286 { switches => [ '-w' ] },
287 q(%n should not be able to modify read-only constants),
863811b2
DM
288);
289
2fba7546 290# check overflows
45e52d63 291for (int(~0/2+1), ~0, "9999999999999999999") {
2fba7546
GA
292 is(eval {sprintf "%${_}d", 0}, undef, "no sprintf result expected %${_}d");
293 like($@, qr/^Integer overflow in format string for sprintf /, "overflow in sprintf");
294 is(eval {printf "%${_}d\n", 0}, undef, "no printf result expected %${_}d");
94bbb3f4 295 like($@, qr/^Integer overflow in format string for printf /, "overflow in printf");
2fba7546 296}
863811b2 297
2fba7546 298# check %NNN$ for range bounds
863811b2
DM
299{
300 my ($warn, $bad) = (0,0);
301 local $SIG{__WARN__} = sub {
7baa4690 302 if ($_[0] =~ /missing argument/i) {
863811b2
DM
303 $warn++
304 }
305 else {
306 $bad++
307 }
308 };
2fba7546 309
45e52d63 310 my $fmt = join('', map("%$_\$s%" . ((1 << 31)-$_) . '$s', 1..20));
2fba7546
GA
311 my $result = sprintf $fmt, qw(a b c d);
312 is($result, "abcd", "only four valid values in $fmt");
863811b2
DM
313 is($warn, 36, "expected warnings");
314 is($bad, 0, "unexpected warnings");
315}
316
4077a6bc
AB
317# Tests for "missing argument" and "redundant argument" warnings
318{
319 my ($warn_missing, $warn_redundant, $warn_bad) = (0,0,0);
320 local $SIG{__WARN__} = sub {
321 if ($_[0] =~ /missing argument/i) {
322 $warn_missing++
323 }
324 elsif ($_[0] =~ /redundant argument/i) {
325 $warn_redundant++
326 }
327 else {
328 $warn_bad++
329 }
330 };
331
332 my @tests = (
333 # The "", "%s", and "%-p" formats have special-case handling
334 # in sv.c
335 {
336 fmt => "",
337 args => [ qw( x ) ],
338 res => "",
339 m => 0,
340 r => 1,
341 },
342 {
343 fmt => "%s",
344 args => [ qw( x y ) ],
345 res => "x",
346 m => 0,
347 r => 1,
348 },
349 {
350 fmt => "%-p",
351 args => [ qw( x y ) ],
352 res => qr/^[0-9a-f]+$/as,
353 m => 0,
354 r => 1,
355 },
356 # Other non-specialcased patterns
357 {
358 fmt => "%s : %s",
359 args => [ qw( a b c ) ],
360 res => "a : b",
361 m => 0,
362 r => 1,
363 },
364 {
365 fmt => "%s : %s : %s",
366 args => [ qw( a b c d e ) ],
367 res => "a : b : c",
368 m => 0,
369 # Note how we'll only warn about redundant arguments once,
370 # even though both "d" and "e" are redundant...
371 r => 1,
372 },
373 {
374 fmt => "%s : %s : %s",
375 args => [ ],
376 res => " : : ",
377 # ...But when arguments are missing we'll warn about every
378 # missing argument. This difference between the two
379 # warnings is a feature.
380 m => 3,
381 r => 0,
382 },
383
384 # Tests for format parameter indexes.
385 #
386 # Deciding what to do about these is a bit tricky, and so is
387 # "correctly" warning about missing arguments on them.
388 #
389 # Should we warn if you supply 4 arguments but only use
390 # argument 1,3 & 4? Or only if you supply 5 arguments and your
391 # highest used argument is 4?
392 #
393 # For some uses of this printf feature (e.g. i18n systems)
394 # it's a always a logic error to not print out every provided
395 # argument, but for some other uses skipping some might be a
396 # feature (although you could argue that then printf should be
397 # called as e.g:
398 #
399 # printf q[%1$s %3$s], x(), undef, z();
400 #
401 # Instead of:
402 #
403 # printf q[%1$s %3$s], x(), y(), z();
404 #
405 # Since calling the (possibly expensive) y() function is
406 # completely redundant there.
407 #
408 # We deal with all these potential problems by not even
409 # trying. If the pattern contains any format parameter indexes
410 # whatsoever we'll never warn about redundant arguments.
411 {
412 fmt => '%1$s : %2$s',
413 args => [ qw( x y z ) ],
414 res => "x : y",
415 m => 0,
416 r => 0,
417 },
418 {
419 fmt => '%2$s : %4$s : %5$s',
420 args => [ qw( a b c d )],
421 res => "b : d : ",
422 m => 1,
423 r => 0,
424 },
425 {
426 fmt => '%s : %1$s : %s',
427 args => [ qw( x y z ) ],
428 res => "x : x : y",
429 m => 0,
430 r => 0,
431 },
432
433 );
434
435 for my $i (0..$#tests) {
436 my $test = $tests[$i];
437 my $result = sprintf $test->{fmt}, @{$test->{args}};
438
439 my $prefix = "For format '$test->{fmt}' and arguments/result '@{$test->{args}}'/'$result'";
440 if (ref $test->{res} eq 'Regexp') {
441 like($result, $test->{res}, "$prefix got the right result");
442 } else {
443 is($result, $test->{res}, "$prefix got the right result");
444 }
445 is($warn_missing, $test->{m}, "$prefix got '$test->{m}' 'missing argument' warnings");
446 is($warn_redundant, $test->{r}, "$prefix got '$test->{r}' 'redundant argument' warnings");
447 is($warn_bad, 0, "$prefix No unknown warnings");
448
449 ($warn_missing, $warn_redundant, $warn_bad) = (0,0,0);
450 }
451}
452
343ef749
NC
453{
454 foreach my $ord (0 .. 255) {
455 my $bad = 0;
456 local $SIG{__WARN__} = sub {
457 if ($_[0] !~ /^Invalid conversion in sprintf/) {
458 warn $_[0];
459 $bad++;
460 }
461 };
462 my $r = eval {sprintf '%v' . chr $ord};
463 is ($bad, 0, "pattern '%v' . chr $ord");
464 }
465}
9911cee9
ST
466
467sub mysprintf_int_flags {
468 my ($fmt, $num) = @_;
469 die "wrong format $fmt" if $fmt !~ /^%([-+ 0]+)([1-9][0-9]*)d\z/;
470 my $flag = $1;
471 my $width = $2;
472 my $sign = $num < 0 ? '-' :
473 $flag =~ /\+/ ? '+' :
474 $flag =~ /\ / ? ' ' :
475 '';
476 my $abs = abs($num);
477 my $padlen = $width - length($sign.$abs);
478 return
479 $flag =~ /0/ && $flag !~ /-/ # do zero padding
480 ? $sign . '0' x $padlen . $abs
481 : $flag =~ /-/ # left or right
482 ? $sign . $abs . ' ' x $padlen
483 : ' ' x $padlen . $sign . $abs;
484}
485
486# Whole tests for "%4d" with 2 to 4 flags;
487# total counts: 3 * (4**2 + 4**3 + 4**4) == 1008
488
489my @flags = ("-", "+", " ", "0");
490for my $num (0, -1, 1) {
491 for my $f1 (@flags) {
492 for my $f2 (@flags) {
493 for my $f3 ('', @flags) { # '' for doubled flags
494 my $flag = $f1.$f2.$f3;
495 my $width = 4;
496 my $fmt = '%'."${flag}${width}d";
497 my $result = sprintf($fmt, $num);
498 my $expect = mysprintf_int_flags($fmt, $num);
499 is($result, $expect, qq/sprintf("$fmt",$num)/);
500
501 next if $f3 eq '';
502
503 for my $f4 (@flags) { # quadrupled flags
504 my $flag = $f1.$f2.$f3.$f4;
505 my $fmt = '%'."${flag}${width}d";
506 my $result = sprintf($fmt, $num);
507 my $expect = mysprintf_int_flags($fmt, $num);
508 is($result, $expect, qq/sprintf("$fmt",$num)/);
509 }
510 }
511 }
512 }
513}
514
9c2a5cfe
B
515# test that %f doesn't panic with +Inf, -Inf, NaN [perl #45383]
516foreach my $n (2**1e100, -2**1e100, 2**1e100/2**1e100) { # +Inf, -Inf, NaN
517 eval { my $f = sprintf("%f", $n); };
518 is $@, "", "sprintf(\"%f\", $n)";
519}
53f65a9e
HS
520
521# test %ll formats with and without HAS_QUAD
53f65a9e 522my @tests = (
8ff953de
MHM
523 [ '%lld' => [qw( 4294967296 -100000000000000 )] ],
524 [ '%lli' => [qw( 4294967296 -100000000000000 )] ],
525 [ '%llu' => [qw( 4294967296 100000000000000 )] ],
526 [ '%Ld' => [qw( 4294967296 -100000000000000 )] ],
527 [ '%Li' => [qw( 4294967296 -100000000000000 )] ],
528 [ '%Lu' => [qw( 4294967296 100000000000000 )] ],
53f65a9e
HS
529);
530
531for my $t (@tests) {
8ff953de
MHM
532 my($fmt, $nums) = @$t;
533 for my $num (@$nums) {
826af139
AB
534 my $w = '';
535 local $SIG{__WARN__} = sub { $w .= shift };
536 my $sprintf_got = sprintf($fmt, $num);
537 if ($Q) {
538 is($sprintf_got, $num, "quad: $fmt -> $num");
539 is($w, '', "no warnings for: $fmt -> $num");
540 } else {
541 is($sprintf_got, $fmt, "quad unsupported: $fmt -> $fmt");
542 like($w, qr/Invalid conversion in sprintf: "$fmt"/, "got warning about invalid conversion from fmt : $fmt");
3101bb05 543 like($w, qr/Redundant argument in sprintf/, "got warning about redundant argument in sprintf from fmt : $fmt");
826af139 544 }
53f65a9e
HS
545 }
546}
547
9ef5ed94
AV
548# Check unicode vs byte length
549for my $width (1,2,3,4,5,6,7) {
550 for my $precis (1,2,3,4,5,6,7) {
551 my $v = "\x{20ac}\x{20ac}";
552 my $format = "%" . $width . "." . $precis . "s";
553 my $chars = ($precis > 2 ? 2 : $precis);
554 my $space = ($width < 2 ? 0 : $width - $chars);
555 fresh_perl_is(
556 'my $v = "\x{20ac}\x{20ac}"; my $x = sprintf "'.$format.'", $v; $x =~ /^(\s*)(\S*)$/; print "$_" for map {length} $1, $2',
557 "$space$chars",
558 {},
559 q(sprintf ").$format.q(", "\x{20ac}\x{20ac}"),
560 );
561 }
562}
667f6f15
FC
563
564# Overload count
565package o { use overload '""', sub { ++our $count; $_[0][0]; } }
566my $o = bless ["\x{100}"], o::;
567() = sprintf "%1s", $o;
568is $o::count, '1', 'sprinf %1s overload count';
569$o::count = 0;
570() = sprintf "%.1s", $o;
571is $o::count, '1', 'sprinf %.1s overload count';
40bca5ae
JH
572
573for my $t (@hexfloat) {
574 my ($format, $arg, $expected) = @$t;
575 $arg = eval $arg;
576 my $result = sprintf($format, $arg);
577 is($result, $expected, "'$format' '$arg' -> '$result' cf '$expected'");
578}