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