change#6174 needs corresponding change in win32/config_h.PL,
[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
7# specific to multi-byte characters (under use utf8 and such).
8d063cd8 8
9f1b1f2d 9BEGIN {
10 chdir 't' if -d 't';
11 unshift @INC, '../lib';
12}
13use warnings;
14
be3174d2 15while (<DATA>) {
16 s/^\s*>//; s/<\s*$//;
17 push @tests, [split(/<\s*>/, $_, 4)];
18}
19
20print '1..', scalar @tests, "\n";
8d063cd8 21
fb73857a 22$SIG{__WARN__} = sub {
23 if ($_[0] =~ /^Invalid conversion/) {
65c97e0f 24 $w = ' INVALID'
fb73857a 25 } else {
65c97e0f 26 warn @_;
fb73857a 27 }
28};
29
be3174d2 30for ($i = 1; @tests; $i++) {
31 ($template, $data, $result, $comment) = @{shift @tests};
32 $evalData = eval $data;
33 $w = undef;
34 $x = sprintf(">$template<",
35 defined @$evalData ? @$evalData : $evalData);
36 substr($x, -1, 0) = $w if $w;
65c97e0f 37 # $y may have 3 exponent digits, not 2
38 my $r;
39 if (($y = $x) =~ s/([Ee][-+])0(\d)/$1$2/g) {
40 $y =~ s/^>\s+/>/;
41 $y =~ s/\s+<$/</;
42 $r = $result;
43 $r =~ s/^\s+//;
44 $r =~ s/\s+$//;
45 }
46
be3174d2 47 if ($x eq ">$result<") {
48 print "ok $i\n";
49 }
65c97e0f 50 elsif ($r and $y eq ">$r<") # Some C libraries always give
51 { # three-digit exponent
52 print("ok $i >$result< $x # three-digit exponent accepted\n");
53 }
be3174d2 54 else {
65c97e0f 55 print("not ok $i >$template< >$data< >$result< $x",
56 $comment ? " # $comment\n" : "\n");
fb73857a 57 }
58}
be3174d2 59
60# In each of the the following lines, there are three required fields:
61# printf template, data to be formatted (as a Perl expression), and
62# expected result of formatting. An optional fourth field can contain
63# a comment. Each field is delimited by a starting '>' and a
64# finishing '<'; any whitespace outside these start and end marks is
65# not part of the field. If formatting requires more than one data
66# item (for example, if variable field widths are used), the Perl data
67# expression should return a reference to an array having the requisite
68# number of elements. Even so, subterfuge is sometimes required: see
69# tests for %n and %p.
70#
71# template data result
72__END__
be3174d2 73>%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
c2e66d9e 74>%6 .6s< >''< >%6 .6s INVALID<
be3174d2 75>%6.6 s< >''< >%6.6 s INVALID<
c2e66d9e 76>%A< >''< >%A INVALID<
be3174d2 77>%B< >''< >%B INVALID<
78>%C< >''< >%C INVALID<
79>%D< >0x7fffffff< >2147483647< >Synonym for %ld<
80>%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
81>%F< >123456.789< >123456.789000< >Synonym for %f<
82>%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
c2e66d9e 83>%G< >1234567e96< >1.23457E+102<
84>%G< >.1234567e-101< >1.23457E-102<
be3174d2 85>%G< >12345.6789< >12345.7<
86>%H< >''< >%H INVALID<
87>%I< >''< >%I INVALID<
88>%J< >''< >%J INVALID<
89>%K< >''< >%K INVALID<
90>%L< >''< >%L INVALID<
91>%M< >''< >%M INVALID<
92>%N< >''< >%N INVALID<
c2e66d9e 93>%O< >2**32-1< >37777777777< >Synonum for %lo<
be3174d2 94>%P< >''< >%P INVALID<
95>%Q< >''< >%Q INVALID<
96>%R< >''< >%R INVALID<
97>%S< >''< >%S INVALID<
98>%T< >''< >%T INVALID<
c2e66d9e 99>%U< >2**32-1< >4294967295< >Synonum for %lu<
be3174d2 100>%V< >''< >%V INVALID<
101>%W< >''< >%W INVALID<
102>%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
103>%#X< >2**32-1< >0XFFFFFFFF<
104>%Y< >''< >%Y INVALID<
105>%Z< >''< >%Z INVALID<
106>%a< >''< >%a INVALID<
107>%b< >2**32-1< >11111111111111111111111111111111<
108>%+b< >2**32-1< >11111111111111111111111111111111<
109>%#b< >2**32-1< >0b11111111111111111111111111111111<
110>%34b< >2**32-1< > 11111111111111111111111111111111<
111>%034b< >2**32-1< >0011111111111111111111111111111111<
112>%-34b< >2**32-1< >11111111111111111111111111111111 <
113>%-034b< >2**32-1< >11111111111111111111111111111111 <
114>%c< >ord('A')< >A<
115>%10c< >ord('A')< > A<
116>%#10c< >ord('A')< > A< ># modifier: no effect<
117>%010c< >ord('A')< >000000000A<
118>%10lc< >ord('A')< > A< >l modifier: no effect<
119>%10hc< >ord('A')< > A< >h modifier: no effect<
120>%10.5c< >ord('A')< > A< >precision: no effect<
121>%-10c< >ord('A')< >A <
122>%d< >123456.789< >123456<
123>%d< >-123456.789< >-123456<
124>%d< >0< >0<
125>%+d< >0< >+0<
126>%0d< >0< >0<
127>%.0d< >0< ><
128>%+.0d< >0< >+<
129>%.0d< >1< >1<
130>%d< >1< >1<
131>%+d< >1< >+1<
132>%#3.2d< >1< > 01< ># modifier: no effect<
133>%3.2d< >1< > 01<
134>%03.2d< >1< >001<
135>%-3.2d< >1< >01 <
136>%-03.2d< >1< >01 < >zero pad + left just.: no effect<
137>%d< >-1< >-1<
138>%+d< >-1< >-1<
139>%hd< >1< >1< >More extensive testing of<
140>%ld< >1< >1< >length modifiers would be<
141>%Vd< >1< >1< >platform-specific<
142>%vd< >chr(1)< >1<
143>%+vd< >chr(1)< >+1<
144>%#vd< >chr(1)< >1<
145>%vd< >"\01\02\03"< >1.2.3<
146>%v.3d< >"\01\02\03"< >001.002.003<
147>%v03d< >"\01\02\03"< >001.002.003<
148>%v-3d< >"\01\02\03"< >1 .2 .3 <
149>%v+-3d< >"\01\02\03"< >+1 .2 .3 <
150>%v4.3d< >"\01\02\03"< > 001. 002. 003<
151>%v04.3d< >"\01\02\03"< >0001.0002.0003<
c2e66d9e 152>%*v02d< >['-', "\0\7\13"]< >00-07-11<
be3174d2 153>%e< >1234.875< >1.234875e+03<
c2e66d9e 154>%e< >0.000012345< >1.234500e-05<
155>%e< >1234567E96< >1.234567e+102<
156>%e< >0< >0.000000e+00<
157>%e< >.1234567E-101< >1.234567e-102<
be3174d2 158>%+e< >1234.875< >+1.234875e+03<
159>%#e< >1234.875< >1.234875e+03<
160>%e< >-1234.875< >-1.234875e+03<
161>%+e< >-1234.875< >-1.234875e+03<
162>%#e< >-1234.875< >-1.234875e+03<
163>%.0e< >1234.875< >1e+03<
164>%.*e< >[0, 1234.875]< >1e+03<
165>%.1e< >1234.875< >1.2e+03<
166>%-12.4e< >1234.875< >1.2349e+03 <
167>%12.4e< >1234.875< > 1.2349e+03<
168>%+-12.4e< >1234.875< >+1.2349e+03 <
169>%+12.4e< >1234.875< > +1.2349e+03<
170>%+-12.4e< >-1234.875< >-1.2349e+03 <
171>%+12.4e< >-1234.875< > -1.2349e+03<
172>%f< >1234.875< >1234.875000<
173>%+f< >1234.875< >+1234.875000<
174>%#f< >1234.875< >1234.875000<
175>%f< >-1234.875< >-1234.875000<
176>%+f< >-1234.875< >-1234.875000<
177>%#f< >-1234.875< >-1234.875000<
178>%6f< >1234.875< >1234.875000<
179>%*f< >[6, 1234.875]< >1234.875000<
180>%.0f< >1234.875< >1235<
181>%.1f< >1234.875< >1234.9<
182>%-8.1f< >1234.875< >1234.9 <
183>%8.1f< >1234.875< > 1234.9<
184>%+-8.1f< >1234.875< >+1234.9 <
185>%+8.1f< >1234.875< > +1234.9<
186>%+-8.1f< >-1234.875< >-1234.9 <
187>%+8.1f< >-1234.875< > -1234.9<
188>%*.*f< >[5, 2, 12.3456]< >12.35<
c2e66d9e 189>%f< >0< >0.000000<
190>%.0f< >0< >0<
191>%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
192>%.0f< >0.5< >0<
193>%.0f< >-0.5< >-0<
be3174d2 194>%g< >12345.6789< >12345.7<
195>%+g< >12345.6789< >+12345.7<
196>%#g< >12345.6789< >12345.7<
197>%.0g< >12345.6789< >1e+04<
198>%.2g< >12345.6789< >1.2e+04<
199>%.*g< >[2, 12345.6789]< >1.2e+04<
200>%.9g< >12345.6789< >12345.6789<
201>%12.9g< >12345.6789< > 12345.6789<
202>%012.9g< >12345.6789< >0012345.6789<
203>%-12.9g< >12345.6789< >12345.6789 <
204>%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
205>%-012.9g< >12345.6789< >12345.6789 <
206>%g< >-12345.6789< >-12345.7<
207>%+g< >-12345.6789< >-12345.7<
208>%g< >1234567.89< >1.23457e+06<
209>%+g< >1234567.89< >+1.23457e+06<
210>%#g< >1234567.89< >1.23457e+06<
211>%g< >-1234567.89< >-1.23457e+06<
212>%+g< >-1234567.89< >-1.23457e+06<
213>%#g< >-1234567.89< >-1.23457e+06<
c2e66d9e 214>%g< >0.00012345< >0.00012345<
215>%g< >0.000012345< >1.2345e-05<
216>%g< >1234567E96< >1.23457e+102<
217>%g< >.1234567E-101< >1.23457e-102<
218>%g< >0< >0<
be3174d2 219>%13g< >1234567.89< > 1.23457e+06<
220>%+13g< >1234567.89< > +1.23457e+06<
221>%013g< >1234567.89< >001.23457e+06<
222>%-13g< >1234567.89< >1.23457e+06 <
223>%h< >''< >%h INVALID<
224>%i< >123456.789< >123456< >Synonym for %d<
225>%j< >''< >%j INVALID<
226>%k< >''< >%k INVALID<
227>%l< >''< >%l INVALID<
228>%m< >''< >%m INVALID<
229>%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
230>%o< >2**32-1< >37777777777<
231>%+o< >2**32-1< >37777777777<
232>%#o< >2**32-1< >037777777777<
233>%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
234>%q< >''< >%q INVALID<
235>%r< >''< >%r INVALID<
236>%s< >'string'< >string<
237>%10s< >'string'< > string<
238>%+10s< >'string'< > string<
239>%#10s< >'string'< > string<
240>%010s< >'string'< >0000string<
241>%0*s< >[10, 'string']< >0000string<
242>%-10s< >'string'< >string <
243>%3s< >'string'< >string<
244>%.3s< >'string'< >str<
245>%.*s< >[3, 'string']< >str<
246>%t< >''< >%t INVALID<
247>%u< >2**32-1< >4294967295<
248>%+u< >2**32-1< >4294967295<
249>%#u< >2**32-1< >4294967295<
250>%12u< >2**32-1< > 4294967295<
251>%012u< >2**32-1< >004294967295<
252>%-12u< >2**32-1< >4294967295 <
253>%-012u< >2**32-1< >4294967295 <
254>%v< >''< >%v INVALID<
255>%w< >''< >%w INVALID<
256>%x< >2**32-1< >ffffffff<
257>%+x< >2**32-1< >ffffffff<
258>%#x< >2**32-1< >0xffffffff<
259>%10x< >2**32-1< > ffffffff<
260>%010x< >2**32-1< >00ffffffff<
261>%-10x< >2**32-1< >ffffffff <
262>%-010x< >2**32-1< >ffffffff <
263>%0-10x< >2**32-1< >ffffffff <
264>%0*x< >[-10, ,2**32-1]< >ffffffff <
265>%y< >''< >%y INVALID<
266>%z< >''< >%z INVALID<