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