Move the $data and $result munging into the test preparation loop.
[p5sagit/p5-mst-13.2.git] / t / op / sprintf.t
CommitLineData
8d063cd8 1#!./perl
2
be3174d2 3# Tests sprintf, excluding handling of 64-bit integers or long
4# doubles (if supported), of machine-specific short and long
5# integers, machine-specific floating point exceptions (infinity,
6# not-a-number ...), of the effects of locale, and of features
169da838 7# specific to multi-byte characters (under the utf8 pragma and such).
8d063cd8 8
9f1b1f2d 9BEGIN {
10 chdir 't' if -d 't';
20822f61 11 @INC = '../lib';
e24bffee 12}
9f1b1f2d 13use warnings;
e24bffee 14use Config;
0a52d15b 15use strict;
8234e14b 16
8234e14b 17my @tests = ();
18my ($i, $template, $data, $result, $comment, $w, $x, $evalData, $n, $p);
9f1b1f2d 19
8234e14b 20my $Is_VMS_VAX = 0;
2fba3065 21# We use HW_MODEL since ARCH_NAME was not in VMS V5.*
22if ($^O eq 'VMS') {
23 my $hw_model;
24 chomp($hw_model = `write sys\$output f\$getsyi("HW_MODEL")`);
25 $Is_VMS_VAX = $hw_model < 1024 ? 1 : 0;
8234e14b 26}
27
eaf637cf 28# No %Config.
29my $Is_Ultrix_VAX = $^O eq 'ultrix' && `uname -m` =~ /^VAX$/;
30
0a52d15b 31while (<DATA>) {
32 s/^\s*>//; s/<\s*$//;
33 ($template, $data, $result, $comment) = split(/<\s*>/, $_, 4);
e95e2653 34 if ($^O eq 'os390' || $^O eq 's390') { # non-IEEE (s390 is UTS)
12ebcc11 35 $data =~ s/([eE])96$/${1}63/; # smaller exponents
36 $result =~ s/([eE]\+)102$/${1}69/; # " "
37 $data =~ s/([eE])\-101$/${1}-56/; # larger exponents
38 $result =~ s/([eE])\-102$/${1}-57/; # " "
39 }
eaf637cf 40 if ($Is_VMS_VAX || $Is_Ultrix_VAX) {
e24bffee 41 # VAX DEC C 5.3 at least since there is no
eaf637cf 42 # ccflags =~ /float=ieee/ on VAX.
43 # AXP is unaffected whether or not it's using ieee.
8234e14b 44 $data =~ s/([eE])96$/${1}26/; # smaller exponents
45 $result =~ s/([eE]\+)102$/${1}32/; # " "
46 $data =~ s/([eE])\-101$/${1}-24/; # larger exponents
47 $result =~ s/([eE])\-102$/${1}-25/; # " "
48 }
0a52d15b 49 push @tests, [$template, $data, $result, $comment];
50}
51
52print '1..', scalar @tests, "\n";
53
54$SIG{__WARN__} = sub {
55 if ($_[0] =~ /^Invalid conversion/) {
56 $w = ' INVALID';
57 } elsif ($_[0] =~ /^Use of uninitialized value/) {
58 $w = ' UNINIT';
59 } else {
60 warn @_;
61 }
62};
63
64for ($i = 1; @tests; $i++) {
65 ($template, $data, $result, $comment) = @{shift @tests};
be3174d2 66 $evalData = eval $data;
67 $w = undef;
68 $x = sprintf(">$template<",
69 defined @$evalData ? @$evalData : $evalData);
70 substr($x, -1, 0) = $w if $w;
48237dde 71 # $x may have 3 exponent digits, not 2
72 my $y = $x;
73 if ($y =~ s/([Ee][-+])0(\d)/$1$2/) {
74 # if result is left-adjusted, append extra space
75 if ($template =~ /%\+?\-/ and $result =~ / $/) {
76 $y =~ s/<$/ </;
77 }
78 # if result is zero-filled, add extra zero
79 elsif ($template =~ /%\+?0/ and $result =~ /^0/) {
80 $y =~ s/^>0/>00/;
81 }
82 # if result is right-adjusted, prepend extra space
83 elsif ($result =~ /^ /) {
84 $y =~ s/^>/> /;
85 }
65c97e0f 86 }
87
e24bffee 88 my $skip = 0;
89 if ($comment =~ s/\s+skip:\s*(.*)//) {
90 my $os = $1;
91 my $osv = exists $Config{osvers} ? $Config{osvers} : "0";
92 # >comment skip: all<
93 if ($os =~ /\ball\b/i) {
94 $skip = 1;
95 # >comment skip: VMS hpux:10.20<
96 } elsif ($os =~ /\b$^O(?::(\S+))?\b/i) {
97 my $vsn = defined $1 ? $1 : "0";
98 # Only compare on the the first pair of digits, as numeric
99 # compares don't like 2.6.10-3mdksmp or 2.6.8-24.10-default
100 s/^(\d+(\.\d+)?).*/$1/ for $osv, $vsn;
6f1f3b4a 101 $skip = $vsn ? ($osv <= $vsn ? 1 : 0) : 1;
e24bffee 102 }
103 $skip and $comment =~ s/$/, failure expected on $^O $osv/;
104 }
105
be3174d2 106 if ($x eq ">$result<") {
107 print "ok $i\n";
108 }
e24bffee 109 elsif ($skip) {
110 print "ok $i # skip $comment\n";
111 }
48237dde 112 elsif ($y eq ">$result<") # Some C libraries always give
65c97e0f 113 { # three-digit exponent
00b6c67e 114 print("ok $i # >$result< $x three-digit exponent accepted\n");
65c97e0f 115 }
f7137e37 116 elsif ($result =~ /[-+]\d{3}$/ &&
117 # Suppress tests with modulo of exponent >= 100 on platforms
118 # which can't handle such magnitudes (or where we can't tell).
119 ((!eval {require POSIX}) || # Costly: only do this if we must!
120 (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3))
121 {
00b6c67e 122 print("ok $i # >$template< >$data< >$result<",
e24bffee 123 " Suppressed: exponent out of range?\n");
f7137e37 124 }
be3174d2 125 else {
48237dde 126 $y = ($x eq $y ? "" : " => $y");
127 print("not ok $i >$template< >$data< >$result< $x$y",
65c97e0f 128 $comment ? " # $comment\n" : "\n");
fb73857a 129 }
130}
48237dde 131
a6d05634 132# In each of the following lines, there are three required fields:
be3174d2 133# printf template, data to be formatted (as a Perl expression), and
134# expected result of formatting. An optional fourth field can contain
135# a comment. Each field is delimited by a starting '>' and a
136# finishing '<'; any whitespace outside these start and end marks is
137# not part of the field. If formatting requires more than one data
138# item (for example, if variable field widths are used), the Perl data
139# expression should return a reference to an array having the requisite
140# number of elements. Even so, subterfuge is sometimes required: see
141# tests for %n and %p.
142#
e24bffee 143# Tests that are expected to fail on a certain OS can be marked as such
144# by trailing the comment with a skip: section. Skips are tags separated
145# bu space consisting of a $^O optionally trailed with :osvers. In the
146# latter case, all os-levels below that are expected to fail. A special
147# tag 'all' is allowed for todo tests that should fail on any system
148#
149# >%G< >1234567e96< >1.23457E+102< >exponent too big skip: os390<
6f1f3b4a 150# >%.0g< >-0.0< >-0< >No minus skip: MSWin32 VMS hpux:10.20<
e24bffee 151# >%d< >4< >1< >4 != 1 skip: all<
152#
02a4ca6d 153# The following tests are not currently run, for the reasons stated:
154
155=pod
156
157=begin problematic
158
02a4ca6d 159>%.0f< >1.5< >2< >Standard vague: no rounding rules<
160>%.0f< >2.5< >2< >Standard vague: no rounding rules<
161
162=end problematic
163
164=cut
165
be3174d2 166# template data result
167__END__
be3174d2 168>%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
c2e66d9e 169>%6 .6s< >''< >%6 .6s INVALID<
be3174d2 170>%6.6 s< >''< >%6.6 s INVALID<
c2e66d9e 171>%A< >''< >%A INVALID<
be3174d2 172>%B< >''< >%B INVALID<
173>%C< >''< >%C INVALID<
174>%D< >0x7fffffff< >2147483647< >Synonym for %ld<
175>%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
176>%F< >123456.789< >123456.789000< >Synonym for %f<
177>%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
c2e66d9e 178>%G< >1234567e96< >1.23457E+102<
179>%G< >.1234567e-101< >1.23457E-102<
be3174d2 180>%G< >12345.6789< >12345.7<
e24bffee 181>%G< >1234567e96< >1.23457E+102< >exponent too big skip: os390<
182>%G< >.1234567e-101< >1.23457E-102< >exponent too small skip: os390<
be3174d2 183>%H< >''< >%H INVALID<
184>%I< >''< >%I INVALID<
185>%J< >''< >%J INVALID<
186>%K< >''< >%K INVALID<
187>%L< >''< >%L INVALID<
188>%M< >''< >%M INVALID<
189>%N< >''< >%N INVALID<
8234e14b 190>%O< >2**32-1< >37777777777< >Synonym for %lo<
be3174d2 191>%P< >''< >%P INVALID<
192>%Q< >''< >%Q INVALID<
193>%R< >''< >%R INVALID<
194>%S< >''< >%S INVALID<
195>%T< >''< >%T INVALID<
8234e14b 196>%U< >2**32-1< >4294967295< >Synonym for %lu<
be3174d2 197>%V< >''< >%V INVALID<
198>%W< >''< >%W INVALID<
199>%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
200>%#X< >2**32-1< >0XFFFFFFFF<
201>%Y< >''< >%Y INVALID<
202>%Z< >''< >%Z INVALID<
203>%a< >''< >%a INVALID<
204>%b< >2**32-1< >11111111111111111111111111111111<
205>%+b< >2**32-1< >11111111111111111111111111111111<
206>%#b< >2**32-1< >0b11111111111111111111111111111111<
207>%34b< >2**32-1< > 11111111111111111111111111111111<
208>%034b< >2**32-1< >0011111111111111111111111111111111<
209>%-34b< >2**32-1< >11111111111111111111111111111111 <
210>%-034b< >2**32-1< >11111111111111111111111111111111 <
211>%c< >ord('A')< >A<
212>%10c< >ord('A')< > A<
213>%#10c< >ord('A')< > A< ># modifier: no effect<
214>%010c< >ord('A')< >000000000A<
215>%10lc< >ord('A')< > A< >l modifier: no effect<
216>%10hc< >ord('A')< > A< >h modifier: no effect<
217>%10.5c< >ord('A')< > A< >precision: no effect<
218>%-10c< >ord('A')< >A <
219>%d< >123456.789< >123456<
220>%d< >-123456.789< >-123456<
221>%d< >0< >0<
222>%+d< >0< >+0<
223>%0d< >0< >0<
224>%.0d< >0< ><
225>%+.0d< >0< >+<
226>%.0d< >1< >1<
227>%d< >1< >1<
228>%+d< >1< >+1<
229>%#3.2d< >1< > 01< ># modifier: no effect<
230>%3.2d< >1< > 01<
231>%03.2d< >1< >001<
232>%-3.2d< >1< >01 <
233>%-03.2d< >1< >01 < >zero pad + left just.: no effect<
234>%d< >-1< >-1<
235>%+d< >-1< >-1<
236>%hd< >1< >1< >More extensive testing of<
237>%ld< >1< >1< >length modifiers would be<
238>%Vd< >1< >1< >platform-specific<
239>%vd< >chr(1)< >1<
240>%+vd< >chr(1)< >+1<
241>%#vd< >chr(1)< >1<
242>%vd< >"\01\02\03"< >1.2.3<
243>%v.3d< >"\01\02\03"< >001.002.003<
211dfcf1 244>%0v3d< >"\01\02\03"< >001.002.003<
245>%-v3d< >"\01\02\03"< >1 .2 .3 <
246>%+-v3d< >"\01\02\03"< >+1 .2 .3 <
be3174d2 247>%v4.3d< >"\01\02\03"< > 001. 002. 003<
211dfcf1 248>%0v4.3d< >"\01\02\03"< >0001.0002.0003<
249>%0*v2d< >['-', "\0\7\14"]< >00-07-12<
250>%v.*d< >["\01\02\03", 3]< >001.002.003<
251>%0v*d< >["\01\02\03", 3]< >001.002.003<
252>%-v*d< >["\01\02\03", 3]< >1 .2 .3 <
253>%+-v*d< >["\01\02\03", 3]< >+1 .2 .3 <
254>%v*.*d< >["\01\02\03", 4, 3]< > 001. 002. 003<
255>%0v*.*d< >["\01\02\03", 4, 3]< >0001.0002.0003<
256>%0*v*d< >['-', "\0\7\13", 2]< >00-07-11<
be3174d2 257>%e< >1234.875< >1.234875e+03<
c2e66d9e 258>%e< >0.000012345< >1.234500e-05<
259>%e< >1234567E96< >1.234567e+102<
260>%e< >0< >0.000000e+00<
261>%e< >.1234567E-101< >1.234567e-102<
be3174d2 262>%+e< >1234.875< >+1.234875e+03<
263>%#e< >1234.875< >1.234875e+03<
264>%e< >-1234.875< >-1.234875e+03<
265>%+e< >-1234.875< >-1.234875e+03<
266>%#e< >-1234.875< >-1.234875e+03<
267>%.0e< >1234.875< >1e+03<
02a4ca6d 268>%#.0e< >1234.875< >1.e+03<
20f6aaab 269>%.0e< >1.875< >2e+00<
270>%.0e< >0.875< >9e-01<
be3174d2 271>%.*e< >[0, 1234.875]< >1e+03<
272>%.1e< >1234.875< >1.2e+03<
273>%-12.4e< >1234.875< >1.2349e+03 <
274>%12.4e< >1234.875< > 1.2349e+03<
275>%+-12.4e< >1234.875< >+1.2349e+03 <
276>%+12.4e< >1234.875< > +1.2349e+03<
277>%+-12.4e< >-1234.875< >-1.2349e+03 <
278>%+12.4e< >-1234.875< > -1.2349e+03<
e24bffee 279>%e< >1234567E96< >1.234567e+102< >exponent too big skip: os390<
280>%e< >.1234567E-101< >1.234567e-102< >exponent too small skip: os390<
be3174d2 281>%f< >1234.875< >1234.875000<
282>%+f< >1234.875< >+1234.875000<
283>%#f< >1234.875< >1234.875000<
284>%f< >-1234.875< >-1234.875000<
285>%+f< >-1234.875< >-1234.875000<
286>%#f< >-1234.875< >-1234.875000<
287>%6f< >1234.875< >1234.875000<
288>%*f< >[6, 1234.875]< >1234.875000<
e24bffee 289>%.0f< >-0.1< >-0< >C library bug: no minus skip: VMS<
be3174d2 290>%.0f< >1234.875< >1235<
291>%.1f< >1234.875< >1234.9<
292>%-8.1f< >1234.875< >1234.9 <
293>%8.1f< >1234.875< > 1234.9<
294>%+-8.1f< >1234.875< >+1234.9 <
295>%+8.1f< >1234.875< > +1234.9<
296>%+-8.1f< >-1234.875< >-1234.9 <
297>%+8.1f< >-1234.875< > -1234.9<
298>%*.*f< >[5, 2, 12.3456]< >12.35<
c2e66d9e 299>%f< >0< >0.000000<
300>%.0f< >0< >0<
301>%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
d5365ef1 302>%.0f< >0.1< >0<
20f6aaab 303>%.0f< >0.6< >1< >Known to fail with sfio, (irix|nonstop-ux|powerux); -DHAS_LDBL_SPRINTF_BUG may fix<
304>%.0f< >-0.6< >-1< >Known to fail with sfio, (irix|nonstop-ux|powerux); -DHAS_LDBL_SPRINTF_BUG may fix<
305>%.0f< >1.6< >2<
306>%.0f< >-1.6< >-2<
02a4ca6d 307>%.0f< >1< >1<
308>%#.0f< >1< >1.<
00e17364 309>%.0lf< >1< >1< >'l' should have no effect<
310>%.0hf< >1< >%.0hf INVALID< >'h' should be rejected<
be3174d2 311>%g< >12345.6789< >12345.7<
312>%+g< >12345.6789< >+12345.7<
313>%#g< >12345.6789< >12345.7<
65ff66fc 314>%.0g< >-0.0< >-0< >C99 standard mandates minus sign but C89 does not skip: MSWin32 VMS hpux:10.20 openbsd netbsd:1.5 irix<
be3174d2 315>%.0g< >12345.6789< >1e+04<
02a4ca6d 316>%#.0g< >12345.6789< >1.e+04<
be3174d2 317>%.2g< >12345.6789< >1.2e+04<
318>%.*g< >[2, 12345.6789]< >1.2e+04<
319>%.9g< >12345.6789< >12345.6789<
320>%12.9g< >12345.6789< > 12345.6789<
321>%012.9g< >12345.6789< >0012345.6789<
322>%-12.9g< >12345.6789< >12345.6789 <
323>%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
324>%-012.9g< >12345.6789< >12345.6789 <
325>%g< >-12345.6789< >-12345.7<
326>%+g< >-12345.6789< >-12345.7<
327>%g< >1234567.89< >1.23457e+06<
328>%+g< >1234567.89< >+1.23457e+06<
329>%#g< >1234567.89< >1.23457e+06<
330>%g< >-1234567.89< >-1.23457e+06<
331>%+g< >-1234567.89< >-1.23457e+06<
332>%#g< >-1234567.89< >-1.23457e+06<
c2e66d9e 333>%g< >0.00012345< >0.00012345<
334>%g< >0.000012345< >1.2345e-05<
335>%g< >1234567E96< >1.23457e+102<
336>%g< >.1234567E-101< >1.23457e-102<
337>%g< >0< >0<
be3174d2 338>%13g< >1234567.89< > 1.23457e+06<
339>%+13g< >1234567.89< > +1.23457e+06<
e24bffee 340>%013g< >1234567.89< >001.23457e+06<
341>%-13g< >1234567.89< >1.23457e+06 <
342>%g< >.1234567E-101< >1.23457e-102< >exponent too small skip: os390<
343>%g< >1234567E96< >1.23457e+102< >exponent too big skip: os390<
be3174d2 344>%h< >''< >%h INVALID<
345>%i< >123456.789< >123456< >Synonym for %d<
346>%j< >''< >%j INVALID<
347>%k< >''< >%k INVALID<
348>%l< >''< >%l INVALID<
349>%m< >''< >%m INVALID<
350>%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
351>%o< >2**32-1< >37777777777<
352>%+o< >2**32-1< >37777777777<
353>%#o< >2**32-1< >037777777777<
8234e14b 354>%o< >642< >1202< >check smaller octals across platforms<
355>%+o< >642< >1202<
356>%#o< >642< >01202<
be3174d2 357>%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
0dbb1585 358>%d< >$p=sprintf('%-8p',$p);$p=~/^[0-9a-f]+\s*$/< >1< >Coarse hack: hex from %p?<
d5365ef1 359>%#p< >''< >%#p INVALID<
be3174d2 360>%q< >''< >%q INVALID<
361>%r< >''< >%r INVALID<
362>%s< >'string'< >string<
363>%10s< >'string'< > string<
364>%+10s< >'string'< > string<
365>%#10s< >'string'< > string<
366>%010s< >'string'< >0000string<
367>%0*s< >[10, 'string']< >0000string<
368>%-10s< >'string'< >string <
369>%3s< >'string'< >string<
370>%.3s< >'string'< >str<
371>%.*s< >[3, 'string']< >str<
372>%t< >''< >%t INVALID<
373>%u< >2**32-1< >4294967295<
374>%+u< >2**32-1< >4294967295<
375>%#u< >2**32-1< >4294967295<
376>%12u< >2**32-1< > 4294967295<
377>%012u< >2**32-1< >004294967295<
378>%-12u< >2**32-1< >4294967295 <
379>%-012u< >2**32-1< >4294967295 <
380>%v< >''< >%v INVALID<
381>%w< >''< >%w INVALID<
382>%x< >2**32-1< >ffffffff<
383>%+x< >2**32-1< >ffffffff<
384>%#x< >2**32-1< >0xffffffff<
385>%10x< >2**32-1< > ffffffff<
386>%010x< >2**32-1< >00ffffffff<
387>%-10x< >2**32-1< >ffffffff <
388>%-010x< >2**32-1< >ffffffff <
389>%0-10x< >2**32-1< >ffffffff <
390>%0*x< >[-10, ,2**32-1]< >ffffffff <
391>%y< >''< >%y INVALID<
392>%z< >''< >%z INVALID<
eb3fce90 393>%2$d %1$d< >[12, 34]< >34 12<
394>%*2$d< >[12, 3]< > 12<
395>%2$d %d< >[12, 34]< >34 12<
396>%2$d %d %d< >[12, 34]< >34 12 34<
397>%3$d %d %d< >[12, 34, 56]< >56 12 34<
398>%2$*3$d %d< >[12, 34, 3]< > 34 12<
58e33a90 399>%*3$2$d %d< >[12, 34, 3]< >%*3$2$d 12 INVALID<
211dfcf1 400>%2$d< >12< >0 UNINIT<
eb3fce90 401>%0$d< >12< >%0$d INVALID<
402>%1$$d< >12< >%1$$d INVALID<
403>%1$1$d< >12< >%1$1$d INVALID<
404>%*2$*2$d< >[12, 3]< >%*2$*2$d INVALID<
405>%*2*2$d< >[12, 3]< >%*2*2$d INVALID<
8896765a 406>%*2$1d< >[12, 3]< >%*2$1d INVALID<
211dfcf1 407>%0v2.2d< >''< ><
26372e71 408>%vc,%d< >[63, 64, 65]< >%vc,63 INVALID<
409>%v%,%d< >[63, 64, 65]< >%v%,63 INVALID<
be75b157 410>%vd,%d< >[1, 2, 3]< >49,2<
26372e71 411>%vf,%d< >[1, 2, 3]< >%vf,1 INVALID<
412>%vF,%d< >[1, 2, 3]< >%vF,1 INVALID<
413>%ve,%d< >[1, 2, 3]< >%ve,1 INVALID<
414>%vE,%d< >[1, 2, 3]< >%vE,1 INVALID<
415>%vg,%d< >[1, 2, 3]< >%vg,1 INVALID<
416>%vG,%d< >[1, 2, 3]< >%vG,1 INVALID<
be75b157 417>%vp< >''< >%vp INVALID<
26372e71 418>%vn< >''< >%vn INVALID<
419>%vs,%d< >[1, 2, 3]< >%vs,1 INVALID<
be75b157 420>%v_< >''< >%v_ INVALID<
f3583277 421>%v#x< >''< >%v#x INVALID<
63c6dcc1 422>%v02x< >"foo\012"< >66.6f.6f.0a<
58e33a90 423>%V-%s< >["Hello"]< >%V-Hello INVALID<
424>%K %d %d< >[13, 29]< >%K 13 29 INVALID<
425>%*.*K %d< >[13, 29, 76]< >%*.*K 13 INVALID<
426>%4$K %d< >[45, 67]< >%4$K 45 INVALID<
427>%d %K %d< >[23, 45]< >23 %K 45 INVALID<
3a7a539e 428>%*v*999\$d %d %d< >[11, 22, 33]< >%*v*999\$d 11 22 INVALID<
ed2b91d2 429>%#b< >0< >0<
430>%#o< >0< >0<
431>%#x< >0< >0<
7ad96abb 432>%2918905856$v2d< >''< ><
433>%*2918905856$v2d< >''< > UNINIT<