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