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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
#!./perl -w
# Tests for sprintf that do not fit the format of sprintf.t.
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl';
}
# We'll run 12 extra tests (see below) if $Q is false.
eval { my $q = pack "q", 0 };
my $Q = $@ eq '';
# %a and %A depend on the floating point config
# This totally doesn't test non-IEEE-754 float formats.
my @hexfloat;
print "# uvsize = $Config{uvsize}\n";
print "# nvsize = $Config{nvsize}\n";
print "# nv_preserves_uv_bits = $Config{nv_preserves_uv_bits}\n";
print "# d_quad = $Config{d_quad}\n";
if ($Config{nvsize} == 8 &&
(
# IEEE-754 64-bit ("double precision"), the most common out there
($Config{uvsize} == 8 && $Config{nv_preserves_uv_bits} == 53)
||
# If we have a quad we can still get the mantissa bits.
($Config{uvsize} == 4 && $Config{d_quad})
)
) {
@hexfloat = (
[ '%a', '0', '0x0p+0' ],
[ '%a', '1', '0x1p+0' ],
[ '%a', '1.0', '0x1p+0' ],
[ '%a', '0.5', '0x1p-1' ],
[ '%a', '0.25', '0x1p-2' ],
[ '%a', '0.75', '0x1.8p-1' ],
[ '%a', '3.14', '0x1.91eb851eb851fp+1' ],
[ '%a', '-1.0', '-0x1p+0' ],
[ '%a', '-3.14', '-0x1.91eb851eb851fp+1' ],
[ '%a', '0.1', '0x1.999999999999ap-4' ],
[ '%a', '1/7', '0x1.2492492492492p-3' ],
[ '%a', 'sqrt(2)', '0x1.6a09e667f3bcdp+0' ],
[ '%a', 'exp(1)', '0x1.5bf0a8b145769p+1' ],
[ '%a', '2**-10', '0x1p-10' ],
[ '%a', '2**10', '0x1p+10' ],
[ '%a', '1e-9', '0x1.12e0be826d695p-30' ],
[ '%a', '1e9', '0x1.dcd65p+29' ],
[ '%#a', '1', '0x1.p+0' ],
[ '%+a', '1', '+0x1p+0' ],
[ '%+a', '-1', '-0x1p+0' ],
[ '% a', ' 1', ' 0x1p+0' ],
[ '% a', '-1', '-0x1p+0' ],
[ '%8a', '3.14', '0x1.91eb851eb851fp+1' ],
[ '%13a', '3.14', '0x1.91eb851eb851fp+1' ],
[ '%20a', '3.14', '0x1.91eb851eb851fp+1' ],
[ '%.4a', '3.14', '0x1.91ecp+1' ],
[ '%.5a', '3.14', '0x1.91eb8p+1' ],
[ '%.6a', '3.14', '0x1.91eb85p+1' ],
[ '%.20a', '3.14', '0x1.91eb851eb851f0000000p+1' ],
[ '%20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
[ '%20.15a', '3.14', '0x1.91eb851eb851f00p+1' ],
[ '% 20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
[ '%020.10a', '3.14', '0x0001.91eb851eb8p+1' ],
[ '%30a', '3.14', ' 0x1.91eb851eb851fp+1' ],
[ '%-30a', '3.14', '0x1.91eb851eb851fp+1 ' ],
[ '%030a', '3.14', '0x00000000001.91eb851eb851fp+1' ],
[ '%-030a', '3.14', '0x1.91eb851eb851fp+1 ' ],
[ '%.40a', '3.14',
'0x1.91eb851eb851f000000000000000000000000000p+1' ],
[ '%A', '3.14', '0X1.91EB851EB851FP+1' ],
);
} elsif (($Config{nvsize} == 16 || $Config{nvsize} == 12) &&
# 80-bit ("extended precision") long double, pack F is the NV
# cd cc cc cc cc cc cc cc fb bf 00 00 00 00 00 00
# cd cc cc cc cc cc cc cc fb bf 00 00
(pack("F", 0.1) =~ /^\xCD/ || # LE
pack("F", 0.1) =~ /\xCD$/)) { # BE (if this ever happens)
@hexfloat = (
[ '%a', '0', '0x0p+0' ],
[ '%a', '1', '0x8p-3' ],
[ '%a', '1.0', '0x8p-3' ],
[ '%a', '0.5', '0x8p-4' ],
[ '%a', '0.25', '0x8p-5' ],
[ '%a', '0.75', '0xcp-4' ],
[ '%a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
[ '%a', '-1.0', '-0x8p-3' ],
[ '%a', '-3.14', '-0xc.8f5c28f5c28f5c3p-2' ],
[ '%a', '0.1', '0xc.ccccccccccccccdp-7' ],
[ '%a', '1/7', '0x9.249249249249249p-6' ],
[ '%a', 'sqrt(2)', '0xb.504f333f9de6484p-3' ],
[ '%a', 'exp(1)', '0xa.df85458a2bb4a9bp-2' ],
[ '%a', '2**-10', '0x8p-13' ],
[ '%a', '2**10', '0x8p+7' ],
[ '%a', '1e-9', '0x8.9705f4136b4a597p-33' ],
[ '%a', '1e9', '0xe.e6b28p+26' ],
[ '%#a', '1', '0x8.p-3' ],
[ '%+a', '1', '+0x8p-3' ],
[ '%+a', '-1', '-0x8p-3' ],
[ '% a', ' 1', ' 0x8p-3' ],
[ '% a', '-1', '-0x8p-3' ],
[ '%8a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
[ '%13a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
[ '%20a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
[ '%.4a', '3.14', '0xc.8f5cp-2' ],
[ '%.5a', '3.14', '0xc.8f5c3p-2' ],
[ '%.6a', '3.14', '0xc.8f5c29p-2' ],
[ '%.20a', '3.14', '0xc.8f5c28f5c28f5c300000p-2' ],
[ '%20.10a', '3.14', ' 0xc.8f5c28f5c3p-2' ],
[ '%20.15a', '3.14', '0xc.8f5c28f5c28f5c3p-2' ],
[ '% 20.10a', '3.14', ' 0xc.8f5c28f5c3p-2' ],
[ '%020.10a', '3.14', '0x000c.8f5c28f5c3p-2' ],
[ '%30a', '3.14', ' 0xc.8f5c28f5c28f5c3p-2' ],
[ '%-30a', '3.14', '0xc.8f5c28f5c28f5c3p-2 ' ],
[ '%030a', '3.14', '0x00000000c.8f5c28f5c28f5c3p-2' ],
[ '%-030a', '3.14', '0xc.8f5c28f5c28f5c3p-2 ' ],
[ '%.40a', '3.14',
'0xc.8f5c28f5c28f5c30000000000000000000000000p-2' ],
[ '%A', '3.14', '0XC.8F5C28F5C28F5C3P-2' ],
);
} elsif (
# IEEE 754 128-bit ("quadruple precision"), e.g. IA-64 (Itanium) in VMS
$Config{nvsize} == 16 &&
# 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb 3f (LE), pack F is the NV
(pack("F", 0.1) =~ /^\x9A\x99{6}/ || # LE
pack("F", 0.1) =~ /\x99{6}\x9A$/) # BE
) {
@hexfloat = (
[ '%a', '0', '0x1p-1' ],
[ '%a', '1', '0x1p+0' ],
[ '%a', '1.0', '0x1p+0' ],
[ '%a', '0.5', '0x1p-1' ],
[ '%a', '0.25', '0x1p-2' ],
[ '%a', '0.75', '0x1.8p-1' ],
[ '%a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%a', '-1', '-0x1p+0' ],
[ '%a', '-3.14', '-0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%a', '0.1', '0x1.999999999999999999999999999ap-4' ],
[ '%a', '1/7', '0x1.2492492492492492492492492492p-3' ],
[ '%a', 'sqrt(2)', '0x1.6a09e667f3bcc908b2fb1366ea95p+0' ],
[ '%a', 'exp(1)', '0x1.5bf0a8b1457695355fb8ac404e7ap+1' ],
[ '%a', '2**-10', '0x1p-10' ],
[ '%a', '2**10', '0x1p+10' ],
[ '%a', '1e-09', '0x1.12e0be826d694b2e62d01511f12ap-30' ],
[ '%a', '1e9', '0x1.dcd65p+29' ],
[ '%#a', '1', '0x1.p+0' ],
[ '%+a', '1', '+0x1p+0' ],
[ '%+a', '-1', '-0x1p+0' ],
[ '% a', '1', ' 0x1p+0' ],
[ '% a', '-1', '-0x1p+0' ],
[ '%8a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%13a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%20a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%.4a', '3.14', '0x1.91ecp+1' ],
[ '%.5a', '3.14', '0x1.91eb8p+1' ],
[ '%.6a', '3.14', '0x1.91eb85p+1' ],
[ '%.20a', '3.14', '0x1.91eb851eb851eb851eb8p+1' ],
[ '%20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
[ '%20.15a', '3.14', '0x1.91eb851eb851eb8p+1' ],
[ '% 20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
[ '%020.10a', '3.14', '0x0001.91eb851eb8p+1' ],
[ '%30a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%-30a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%030a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%-030a', '3.14', '0x1.91eb851eb851eb851eb851eb851fp+1' ],
[ '%.40a', '3.14',
'0x1.91eb851eb851eb851eb851eb851f000000000000p+1' ],
[ '%A', '3.14', '0X1.91EB851EB851EB851EB851EB851FP+1' ],
);
} elsif (
# "double-double", two 64-bit doubles end to end
$Config{nvsize} == 16 &&
# bf b9 99 99 99 99 99 9a bc 59 99 99 99 99 99 9a (BE), pack F is the NV
(pack("F", 0.1) =~ /^\x9A\x99{5}\x59\xBC/ || # LE
pack("F", 0.1) =~ /\xBC\x59\x99{5}\x9A$/) # BE
) {
# XXX these values are actually only "single-double" since
# we currently do not know how to exactly handle the second
# double. See the discussion in sv.c:S_hextract().
@hexfloat = (
[ '%a', '0', '0x1p-1' ],
[ '%a', '1', '0x1p+0' ],
[ '%a', '1.0', '0x1p+0' ],
[ '%a', '0.5', '0x1p-1' ],
[ '%a', '0.25', '0x1p-2' ],
[ '%a', '0.75', '0x1.8p-1' ],
[ '%a', '3.14', '0x1.91eb851eb851fp+1' ],
[ '%a', '-1', '-0x1p+0' ],
[ '%a', '-3.14', '-0x1.91eb851eb851fp+1' ],
[ '%a', '0.1', '0x1.999999999999ap-4' ],
[ '%a', '1/7', '0x1.2492492492492p-3' ],
[ '%a', 'sqrt(2)', '0x1.6a09e661366ebp+0' ],
[ '%a', 'exp(1)', '0x1.5bf0a8b145769p+1' ],
[ '%a', '2**-10', '0x1p-10' ],
[ '%a', '2**10', '0x1p+10' ],
[ '%a', '1e-09', '0x1.12e0be826d695p-30' ],
[ '%a', '1e9', '0x1.dcd65p+29' ],
[ '%#a', '1', '0x1.p+0' ],
[ '%+a', '1', '+0x1p+0' ],
[ '%+a', '-1', '-0x1p+0' ],
[ '% a', '1', ' 0x1p+0' ],
[ '% a', '-1', '-0x1p+0' ],
[ '%8a', '3.14', '0x1.91eb851eb851fp+1' ],
[ '%13a', '3.14', '0x1.91eb851eb851fp+1' ],
[ '%20a', '3.14', '0x1.91eb851eb851fp+1' ],
[ '%.4a', '3.14', '0x1.91ecp+1' ],
[ '%.5a', '3.14', '0x1.91eb8p+1' ],
[ '%.6a', '3.14', '0x1.91eb85p+1' ],
[ '%.20a', '3.14', '0x1.91eb851eb851f0000000p+1' ],
[ '%20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
[ '%20.15a', '3.14', '0x1.91eb851eb851f00p+1' ],
[ '% 20.10a', '3.14', ' 0x1.91eb851eb8p+1' ],
[ '%020.10a', '3.14', '0x0001.91eb851eb8p+1' ],
[ '%30a', '3.14', ' 0x1.91eb851eb851fp+1' ],
[ '%-30a', '3.14', '0x1.91eb851eb851fp+1 ' ],
[ '%030a', '3.14', '0x00000000001.91eb851eb851fp+1' ],
[ '%-030a', '3.14', '0x1.91eb851eb851fp+1 ' ],
[ '%.40a', '3.14',
'0x1.91eb851eb851f000000000000000000000000000p+1' ],
[ '%A', '3.14', '0X1.91EB851EB851FP+1' ],
);
} else {
print "# no hexfloat tests\n";
}
plan tests => 1406 + ($Q ? 0 : 12) + @hexfloat;
use strict;
use Config;
is(
sprintf("%.40g ",0.01),
sprintf("%.40g", 0.01)." ",
q(the sprintf "%.<number>g" optimization)
);
is(
sprintf("%.40f ",0.01),
sprintf("%.40f", 0.01)." ",
q(the sprintf "%.<number>f" optimization)
);
# cases of $i > 1 are against [perl #39126]
for my $i (1, 5, 10, 20, 50, 100) {
chop(my $utf8_format = "%-*s\x{100}");
my $string = "\xB4"x$i; # latin1 ACUTE or ebcdic COPYRIGHT
my $expect = $string." "x$i; # followed by 2*$i spaces
is(sprintf($utf8_format, 3*$i, $string), $expect,
"width calculation under utf8 upgrade, length=$i");
}
# check simultaneous width & precision with wide characters
for my $i (1, 3, 5, 10) {
my $string = "\x{0410}"x($i+10); # cyrillic capital A
my $expect = "\x{0410}"x$i; # cut down to exactly $i characters
my $format = "%$i.${i}s";
is(sprintf($format, $string), $expect,
"width & precision interplay with utf8 strings, length=$i");
}
# Used to mangle PL_sv_undef
fresh_perl_like(
'print sprintf "xxx%n\n"; print undef',
qr/Modification of a read-only value attempted at - line 1\./,
{ switches => [ '-w' ] },
q(%n should not be able to modify read-only constants),
);
# check overflows
for (int(~0/2+1), ~0, "9999999999999999999") {
is(eval {sprintf "%${_}d", 0}, undef, "no sprintf result expected %${_}d");
like($@, qr/^Integer overflow in format string for sprintf /, "overflow in sprintf");
is(eval {printf "%${_}d\n", 0}, undef, "no printf result expected %${_}d");
like($@, qr/^Integer overflow in format string for printf /, "overflow in printf");
}
# check %NNN$ for range bounds
{
my ($warn, $bad) = (0,0);
local $SIG{__WARN__} = sub {
if ($_[0] =~ /missing argument/i) {
$warn++
}
else {
$bad++
}
};
my $fmt = join('', map("%$_\$s%" . ((1 << 31)-$_) . '$s', 1..20));
my $result = sprintf $fmt, qw(a b c d);
is($result, "abcd", "only four valid values in $fmt");
is($warn, 36, "expected warnings");
is($bad, 0, "unexpected warnings");
}
# Tests for "missing argument" and "redundant argument" warnings
{
my ($warn_missing, $warn_redundant, $warn_bad) = (0,0,0);
local $SIG{__WARN__} = sub {
if ($_[0] =~ /missing argument/i) {
$warn_missing++
}
elsif ($_[0] =~ /redundant argument/i) {
$warn_redundant++
}
else {
$warn_bad++
}
};
my @tests = (
# The "", "%s", and "%-p" formats have special-case handling
# in sv.c
{
fmt => "",
args => [ qw( x ) ],
res => "",
m => 0,
r => 1,
},
{
fmt => "%s",
args => [ qw( x y ) ],
res => "x",
m => 0,
r => 1,
},
{
fmt => "%-p",
args => [ qw( x y ) ],
res => qr/^[0-9a-f]+$/as,
m => 0,
r => 1,
},
# Other non-specialcased patterns
{
fmt => "%s : %s",
args => [ qw( a b c ) ],
res => "a : b",
m => 0,
r => 1,
},
{
fmt => "%s : %s : %s",
args => [ qw( a b c d e ) ],
res => "a : b : c",
m => 0,
# Note how we'll only warn about redundant arguments once,
# even though both "d" and "e" are redundant...
r => 1,
},
{
fmt => "%s : %s : %s",
args => [ ],
res => " : : ",
# ...But when arguments are missing we'll warn about every
# missing argument. This difference between the two
# warnings is a feature.
m => 3,
r => 0,
},
# Tests for format parameter indexes.
#
# Deciding what to do about these is a bit tricky, and so is
# "correctly" warning about missing arguments on them.
#
# Should we warn if you supply 4 arguments but only use
# argument 1,3 & 4? Or only if you supply 5 arguments and your
# highest used argument is 4?
#
# For some uses of this printf feature (e.g. i18n systems)
# it's a always a logic error to not print out every provided
# argument, but for some other uses skipping some might be a
# feature (although you could argue that then printf should be
# called as e.g:
#
# printf q[%1$s %3$s], x(), undef, z();
#
# Instead of:
#
# printf q[%1$s %3$s], x(), y(), z();
#
# Since calling the (possibly expensive) y() function is
# completely redundant there.
#
# We deal with all these potential problems by not even
# trying. If the pattern contains any format parameter indexes
# whatsoever we'll never warn about redundant arguments.
{
fmt => '%1$s : %2$s',
args => [ qw( x y z ) ],
res => "x : y",
m => 0,
r => 0,
},
{
fmt => '%2$s : %4$s : %5$s',
args => [ qw( a b c d )],
res => "b : d : ",
m => 1,
r => 0,
},
{
fmt => '%s : %1$s : %s',
args => [ qw( x y z ) ],
res => "x : x : y",
m => 0,
r => 0,
},
);
for my $i (0..$#tests) {
my $test = $tests[$i];
my $result = sprintf $test->{fmt}, @{$test->{args}};
my $prefix = "For format '$test->{fmt}' and arguments/result '@{$test->{args}}'/'$result'";
if (ref $test->{res} eq 'Regexp') {
like($result, $test->{res}, "$prefix got the right result");
} else {
is($result, $test->{res}, "$prefix got the right result");
}
is($warn_missing, $test->{m}, "$prefix got '$test->{m}' 'missing argument' warnings");
is($warn_redundant, $test->{r}, "$prefix got '$test->{r}' 'redundant argument' warnings");
is($warn_bad, 0, "$prefix No unknown warnings");
($warn_missing, $warn_redundant, $warn_bad) = (0,0,0);
}
}
{
foreach my $ord (0 .. 255) {
my $bad = 0;
local $SIG{__WARN__} = sub {
if ($_[0] !~ /^Invalid conversion in sprintf/) {
warn $_[0];
$bad++;
}
};
my $r = eval {sprintf '%v' . chr $ord};
is ($bad, 0, "pattern '%v' . chr $ord");
}
}
sub mysprintf_int_flags {
my ($fmt, $num) = @_;
die "wrong format $fmt" if $fmt !~ /^%([-+ 0]+)([1-9][0-9]*)d\z/;
my $flag = $1;
my $width = $2;
my $sign = $num < 0 ? '-' :
$flag =~ /\+/ ? '+' :
$flag =~ /\ / ? ' ' :
'';
my $abs = abs($num);
my $padlen = $width - length($sign.$abs);
return
$flag =~ /0/ && $flag !~ /-/ # do zero padding
? $sign . '0' x $padlen . $abs
: $flag =~ /-/ # left or right
? $sign . $abs . ' ' x $padlen
: ' ' x $padlen . $sign . $abs;
}
# Whole tests for "%4d" with 2 to 4 flags;
# total counts: 3 * (4**2 + 4**3 + 4**4) == 1008
my @flags = ("-", "+", " ", "0");
for my $num (0, -1, 1) {
for my $f1 (@flags) {
for my $f2 (@flags) {
for my $f3 ('', @flags) { # '' for doubled flags
my $flag = $f1.$f2.$f3;
my $width = 4;
my $fmt = '%'."${flag}${width}d";
my $result = sprintf($fmt, $num);
my $expect = mysprintf_int_flags($fmt, $num);
is($result, $expect, qq/sprintf("$fmt",$num)/);
next if $f3 eq '';
for my $f4 (@flags) { # quadrupled flags
my $flag = $f1.$f2.$f3.$f4;
my $fmt = '%'."${flag}${width}d";
my $result = sprintf($fmt, $num);
my $expect = mysprintf_int_flags($fmt, $num);
is($result, $expect, qq/sprintf("$fmt",$num)/);
}
}
}
}
}
# test that %f doesn't panic with +Inf, -Inf, NaN [perl #45383]
foreach my $n (2**1e100, -2**1e100, 2**1e100/2**1e100) { # +Inf, -Inf, NaN
eval { my $f = sprintf("%f", $n); };
is $@, "", "sprintf(\"%f\", $n)";
}
# test %ll formats with and without HAS_QUAD
my @tests = (
[ '%lld' => [qw( 4294967296 -100000000000000 )] ],
[ '%lli' => [qw( 4294967296 -100000000000000 )] ],
[ '%llu' => [qw( 4294967296 100000000000000 )] ],
[ '%Ld' => [qw( 4294967296 -100000000000000 )] ],
[ '%Li' => [qw( 4294967296 -100000000000000 )] ],
[ '%Lu' => [qw( 4294967296 100000000000000 )] ],
);
for my $t (@tests) {
my($fmt, $nums) = @$t;
for my $num (@$nums) {
my $w = '';
local $SIG{__WARN__} = sub { $w .= shift };
my $sprintf_got = sprintf($fmt, $num);
if ($Q) {
is($sprintf_got, $num, "quad: $fmt -> $num");
is($w, '', "no warnings for: $fmt -> $num");
} else {
is($sprintf_got, $fmt, "quad unsupported: $fmt -> $fmt");
like($w, qr/Invalid conversion in sprintf: "$fmt"/, "got warning about invalid conversion from fmt : $fmt");
like($w, qr/Redundant argument in sprintf/, "got warning about redundant argument in sprintf from fmt : $fmt");
}
}
}
# Check unicode vs byte length
for my $width (1,2,3,4,5,6,7) {
for my $precis (1,2,3,4,5,6,7) {
my $v = "\x{20ac}\x{20ac}";
my $format = "%" . $width . "." . $precis . "s";
my $chars = ($precis > 2 ? 2 : $precis);
my $space = ($width < 2 ? 0 : $width - $chars);
fresh_perl_is(
'my $v = "\x{20ac}\x{20ac}"; my $x = sprintf "'.$format.'", $v; $x =~ /^(\s*)(\S*)$/; print "$_" for map {length} $1, $2',
"$space$chars",
{},
q(sprintf ").$format.q(", "\x{20ac}\x{20ac}"),
);
}
}
# Overload count
package o { use overload '""', sub { ++our $count; $_[0][0]; } }
my $o = bless ["\x{100}"], o::;
() = sprintf "%1s", $o;
is $o::count, '1', 'sprinf %1s overload count';
$o::count = 0;
() = sprintf "%.1s", $o;
is $o::count, '1', 'sprinf %.1s overload count';
for my $t (@hexfloat) {
my ($format, $arg, $expected) = @$t;
$arg = eval $arg;
my $result = sprintf($format, $arg);
my $ok = $result eq $expected;
unless ($ok) {
# It seems that there can be difference in the last bits:
# [perl #122578]
# got "0x1.5bf0a8b14576ap+1"
# expected "0x1.5bf0a8b145769p+1"
# (Android on ARM)
#
# Exact cause unknown but suspecting different fp rounding modes,
# (towards zero? towards +inf? towards -inf?) about which Perl
# is blissfully unaware.
#
# Try extracting one (or sometimes two) last mantissa
# hexdigits, and see if they differ in value by one.
my ($rh, $eh) = ($result, $expected);
sub extract_prefix {
($_[0] =~ s/(-?0x[0-9a-fA-F]+\.)//) && return $1;
}
my $rp = extract_prefix($rh);
my $ep = extract_prefix($eh);
print "# rp = $rp, ep = $ep (rh $rh, eh $eh)\n";
if ($rp eq $ep) { # If prefixes match.
sub extract_exponent {
($_[0] =~ s/([pP][+-]?\d+)//) && return $1;
}
my $re = extract_exponent($rh);
my $ee = extract_exponent($eh);
print "# re = $re, ee = $ee (rh $rh, eh $eh)\n";
if ($re eq $ee) { # If exponents match.
# Remove the common prefix of the mantissa bits.
my $la = length($rh);
my $lb = length($eh);
my $i;
for ($i = 0; $i < $la && $i < $lb; $i++) {
last if substr($rh, $i, 1) ne substr($eh, $i, 1);
}
$rh = substr($rh, $i);
$eh = substr($eh, $i);
print "# (rh $rh, eh $eh)\n";
if ($rh ne $eh) {
# If necessary, pad the shorter one on the right
# with one zero (for example "...1f" vs "...2",
# we want to compare "1f" to "20").
if (length $rh < length $eh) {
$rh .= '0';
} elsif (length $eh < length $rh) {
$eh .= '0';
}
print "# (rh $rh, eh $eh)\n";
if (length $eh == length $rh) {
if (abs(hex($eh) - hex($rh)) == 1) {
$ok = 1;
}
}
}
}
}
}
ok($ok, "'$format' '$arg' -> '$result' cf '$expected'");
}
|