This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[ID 20000803.001] further regexp counting problems
[perl5.git] / t / op / sprintf.t
CommitLineData
8d063cd8
LW
1#!./perl
2
be3174d2
GS
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
GS
9BEGIN {
10 chdir 't' if -d 't';
11 unshift @INC, '../lib';
12}
13use warnings;
14
be3174d2
GS
15while (<DATA>) {
16 s/^\s*>//; s/<\s*$//;
17 push @tests, [split(/<\s*>/, $_, 4)];
18}
19
20print '1..', scalar @tests, "\n";
8d063cd8 21
fb73857a
PP
22$SIG{__WARN__} = sub {
23 if ($_[0] =~ /^Invalid conversion/) {
65c97e0f 24 $w = ' INVALID'
fb73857a 25 } else {
65c97e0f 26 warn @_;
fb73857a
PP
27 }
28};
29
be3174d2
GS
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
GS
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
GS
52 }
53
be3174d2
GS
54 if ($x eq ">$result<") {
55 print "ok $i\n";
56 }
48237dde 57 elsif ($y eq ">$result<") # Some C libraries always give
65c97e0f
GS
58 { # three-digit exponent
59 print("ok $i >$result< $x # three-digit exponent accepted\n");
60 }
be3174d2 61 else {
48237dde
GS
62 $y = ($x eq $y ? "" : " => $y");
63 print("not ok $i >$template< >$data< >$result< $x$y",
65c97e0f 64 $comment ? " # $comment\n" : "\n");
fb73857a
PP
65 }
66}
48237dde 67
be3174d2
GS
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
GS
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
GS
91>%G< >1234567e96< >1.23457E+102<
92>%G< >.1234567e-101< >1.23457E-102<
be3174d2
GS
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
GS
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
GS
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<
d5365ef1 160>%*v02d< >['-', "\0\7\14"]< >00-07-12<
bb2899ba
GS
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
GS
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
GS
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
GS
204>%f< >0< >0.000000<
205>%.0f< >0< >0<
206>%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
d5365ef1
GS
207>%.0f< >0.1< >0<
208>%.0f< >-0.1< >-0<
209>%.0f< >0.6< >1<
210>%.0f< >-0.6< >-1<
be3174d2
GS
211>%g< >12345.6789< >12345.7<
212>%+g< >12345.6789< >+12345.7<
213>%#g< >12345.6789< >12345.7<
214>%.0g< >12345.6789< >1e+04<
215>%.2g< >12345.6789< >1.2e+04<
216>%.*g< >[2, 12345.6789]< >1.2e+04<
217>%.9g< >12345.6789< >12345.6789<
218>%12.9g< >12345.6789< > 12345.6789<
219>%012.9g< >12345.6789< >0012345.6789<
220>%-12.9g< >12345.6789< >12345.6789 <
221>%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
222>%-012.9g< >12345.6789< >12345.6789 <
223>%g< >-12345.6789< >-12345.7<
224>%+g< >-12345.6789< >-12345.7<
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<
229>%+g< >-1234567.89< >-1.23457e+06<
230>%#g< >-1234567.89< >-1.23457e+06<
c2e66d9e
GS
231>%g< >0.00012345< >0.00012345<
232>%g< >0.000012345< >1.2345e-05<
233>%g< >1234567E96< >1.23457e+102<
234>%g< >.1234567E-101< >1.23457e-102<
235>%g< >0< >0<
be3174d2
GS
236>%13g< >1234567.89< > 1.23457e+06<
237>%+13g< >1234567.89< > +1.23457e+06<
238>%013g< >1234567.89< >001.23457e+06<
239>%-13g< >1234567.89< >1.23457e+06 <
240>%h< >''< >%h INVALID<
241>%i< >123456.789< >123456< >Synonym for %d<
242>%j< >''< >%j INVALID<
243>%k< >''< >%k INVALID<
244>%l< >''< >%l INVALID<
245>%m< >''< >%m INVALID<
246>%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
247>%o< >2**32-1< >37777777777<
248>%+o< >2**32-1< >37777777777<
249>%#o< >2**32-1< >037777777777<
250>%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
d5365ef1 251>%#p< >''< >%#p INVALID<
be3174d2
GS
252>%q< >''< >%q INVALID<
253>%r< >''< >%r INVALID<
254>%s< >'string'< >string<
255>%10s< >'string'< > string<
256>%+10s< >'string'< > string<
257>%#10s< >'string'< > string<
258>%010s< >'string'< >0000string<
259>%0*s< >[10, 'string']< >0000string<
260>%-10s< >'string'< >string <
261>%3s< >'string'< >string<
262>%.3s< >'string'< >str<
263>%.*s< >[3, 'string']< >str<
264>%t< >''< >%t INVALID<
265>%u< >2**32-1< >4294967295<
266>%+u< >2**32-1< >4294967295<
267>%#u< >2**32-1< >4294967295<
268>%12u< >2**32-1< > 4294967295<
269>%012u< >2**32-1< >004294967295<
270>%-12u< >2**32-1< >4294967295 <
271>%-012u< >2**32-1< >4294967295 <
272>%v< >''< >%v INVALID<
273>%w< >''< >%w INVALID<
274>%x< >2**32-1< >ffffffff<
275>%+x< >2**32-1< >ffffffff<
276>%#x< >2**32-1< >0xffffffff<
277>%10x< >2**32-1< > ffffffff<
278>%010x< >2**32-1< >00ffffffff<
279>%-10x< >2**32-1< >ffffffff <
280>%-010x< >2**32-1< >ffffffff <
281>%0-10x< >2**32-1< >ffffffff <
282>%0*x< >[-10, ,2**32-1]< >ffffffff <
283>%y< >''< >%y INVALID<
284>%z< >''< >%z INVALID<