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