SYN SYN
[p5sagit/p5-mst-13.2.git] / t / op / sprintf.t
CommitLineData
8d063cd8 1#!./perl
2
4b19af01 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
ee8c7f54 9BEGIN {
10 chdir 't' if -d 't';
22d4bb9c 11 @INC = '../lib';
ee8c7f54 12}
13use warnings;
14
4b19af01 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/) {
4b19af01 24 $w = ' INVALID'
fb73857a 25 } else {
26 warn @_;
27 }
28};
29
4b19af01 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;
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 }
52 }
fb73857a 53
4b19af01 54 if ($x eq ">$result<") {
55 print "ok $i\n";
56 }
57 elsif ($y eq ">$result<") # Some C libraries always give
58 { # three-digit exponent
22d4bb9c 59 print("ok $i # >$result< $x three-digit exponent accepted\n");
4b19af01 60 }
22d4bb9c 61 elsif ($result =~ /[-+]\d{3}$/ &&
62 # Suppress tests with modulo of exponent >= 100 on platforms
63 # which can't handle such magnitudes (or where we can't tell).
64 ((!eval {require POSIX}) || # Costly: only do this if we must!
65 (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3))
66 {
67 print("ok $i # >$template< >$data< >$result<",
68 " Suppressed: exponent out of range?\n")
69 }
4b19af01 70 else {
71 $y = ($x eq $y ? "" : " => $y");
72 print("not ok $i >$template< >$data< >$result< $x$y",
73 $comment ? " # $comment\n" : "\n");
fb73857a 74 }
75}
4b19af01 76
77# In each of the the following lines, there are three required fields:
78# printf template, data to be formatted (as a Perl expression), and
79# expected result of formatting. An optional fourth field can contain
80# a comment. Each field is delimited by a starting '>' and a
81# finishing '<'; any whitespace outside these start and end marks is
82# not part of the field. If formatting requires more than one data
83# item (for example, if variable field widths are used), the Perl data
84# expression should return a reference to an array having the requisite
85# number of elements. Even so, subterfuge is sometimes required: see
86# tests for %n and %p.
87#
22d4bb9c 88# The following tests are not currently run, for the reasons stated:
89
90=pod
91
92=begin problematic
93
94>%.0f< >-0.1< >-0< >C library bug: no minus on VMS, HP-UX<
95>%.0f< >1.5< >2< >Standard vague: no rounding rules<
96>%.0f< >2.5< >2< >Standard vague: no rounding rules<
97
98=end problematic
99
100=cut
101
4b19af01 102# template data result
103__END__
104>%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
105>%6 .6s< >''< >%6 .6s INVALID<
106>%6.6 s< >''< >%6.6 s INVALID<
107>%A< >''< >%A INVALID<
108>%B< >''< >%B INVALID<
109>%C< >''< >%C INVALID<
110>%D< >0x7fffffff< >2147483647< >Synonym for %ld<
111>%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
112>%F< >123456.789< >123456.789000< >Synonym for %f<
113>%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
114>%G< >1234567e96< >1.23457E+102<
115>%G< >.1234567e-101< >1.23457E-102<
116>%G< >12345.6789< >12345.7<
117>%H< >''< >%H INVALID<
118>%I< >''< >%I INVALID<
119>%J< >''< >%J INVALID<
120>%K< >''< >%K INVALID<
121>%L< >''< >%L INVALID<
122>%M< >''< >%M INVALID<
123>%N< >''< >%N INVALID<
124>%O< >2**32-1< >37777777777< >Synonum for %lo<
125>%P< >''< >%P INVALID<
126>%Q< >''< >%Q INVALID<
127>%R< >''< >%R INVALID<
128>%S< >''< >%S INVALID<
129>%T< >''< >%T INVALID<
130>%U< >2**32-1< >4294967295< >Synonum for %lu<
131>%V< >''< >%V INVALID<
132>%W< >''< >%W INVALID<
133>%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
134>%#X< >2**32-1< >0XFFFFFFFF<
135>%Y< >''< >%Y INVALID<
136>%Z< >''< >%Z INVALID<
137>%a< >''< >%a INVALID<
138>%b< >2**32-1< >11111111111111111111111111111111<
139>%+b< >2**32-1< >11111111111111111111111111111111<
140>%#b< >2**32-1< >0b11111111111111111111111111111111<
141>%34b< >2**32-1< > 11111111111111111111111111111111<
142>%034b< >2**32-1< >0011111111111111111111111111111111<
143>%-34b< >2**32-1< >11111111111111111111111111111111 <
144>%-034b< >2**32-1< >11111111111111111111111111111111 <
145>%c< >ord('A')< >A<
146>%10c< >ord('A')< > A<
147>%#10c< >ord('A')< > A< ># modifier: no effect<
148>%010c< >ord('A')< >000000000A<
149>%10lc< >ord('A')< > A< >l modifier: no effect<
150>%10hc< >ord('A')< > A< >h modifier: no effect<
151>%10.5c< >ord('A')< > A< >precision: no effect<
152>%-10c< >ord('A')< >A <
153>%d< >123456.789< >123456<
154>%d< >-123456.789< >-123456<
155>%d< >0< >0<
156>%+d< >0< >+0<
157>%0d< >0< >0<
158>%.0d< >0< ><
159>%+.0d< >0< >+<
160>%.0d< >1< >1<
161>%d< >1< >1<
162>%+d< >1< >+1<
163>%#3.2d< >1< > 01< ># modifier: no effect<
164>%3.2d< >1< > 01<
165>%03.2d< >1< >001<
166>%-3.2d< >1< >01 <
167>%-03.2d< >1< >01 < >zero pad + left just.: no effect<
168>%d< >-1< >-1<
169>%+d< >-1< >-1<
170>%hd< >1< >1< >More extensive testing of<
171>%ld< >1< >1< >length modifiers would be<
172>%Vd< >1< >1< >platform-specific<
173>%vd< >chr(1)< >1<
174>%+vd< >chr(1)< >+1<
175>%#vd< >chr(1)< >1<
176>%vd< >"\01\02\03"< >1.2.3<
177>%v.3d< >"\01\02\03"< >001.002.003<
178>%v03d< >"\01\02\03"< >001.002.003<
179>%v-3d< >"\01\02\03"< >1 .2 .3 <
180>%v+-3d< >"\01\02\03"< >+1 .2 .3 <
181>%v4.3d< >"\01\02\03"< > 001. 002. 003<
182>%v04.3d< >"\01\02\03"< >0001.0002.0003<
183>%*v02d< >['-', "\0\7\14"]< >00-07-12<
184>%v.*d< >[3, "\01\02\03"]< >001.002.003<
185>%v0*d< >[3, "\01\02\03"]< >001.002.003<
186>%v-*d< >[3, "\01\02\03"]< >1 .2 .3 <
187>%v+-*d< >[3, "\01\02\03"]< >+1 .2 .3 <
188>%v*.*d< >[4, 3, "\01\02\03"]< > 001. 002. 003<
189>%v0*.*d< >[4, 3, "\01\02\03"]< >0001.0002.0003<
190>%*v0*d< >['-', 2, "\0\7\13"]< >00-07-11<
191>%e< >1234.875< >1.234875e+03<
192>%e< >0.000012345< >1.234500e-05<
193>%e< >1234567E96< >1.234567e+102<
194>%e< >0< >0.000000e+00<
195>%e< >.1234567E-101< >1.234567e-102<
196>%+e< >1234.875< >+1.234875e+03<
197>%#e< >1234.875< >1.234875e+03<
198>%e< >-1234.875< >-1.234875e+03<
199>%+e< >-1234.875< >-1.234875e+03<
200>%#e< >-1234.875< >-1.234875e+03<
201>%.0e< >1234.875< >1e+03<
22d4bb9c 202>%#.0e< >1234.875< >1.e+03<
4b19af01 203>%.*e< >[0, 1234.875]< >1e+03<
204>%.1e< >1234.875< >1.2e+03<
205>%-12.4e< >1234.875< >1.2349e+03 <
206>%12.4e< >1234.875< > 1.2349e+03<
207>%+-12.4e< >1234.875< >+1.2349e+03 <
208>%+12.4e< >1234.875< > +1.2349e+03<
209>%+-12.4e< >-1234.875< >-1.2349e+03 <
210>%+12.4e< >-1234.875< > -1.2349e+03<
211>%f< >1234.875< >1234.875000<
212>%+f< >1234.875< >+1234.875000<
213>%#f< >1234.875< >1234.875000<
214>%f< >-1234.875< >-1234.875000<
215>%+f< >-1234.875< >-1234.875000<
216>%#f< >-1234.875< >-1234.875000<
217>%6f< >1234.875< >1234.875000<
218>%*f< >[6, 1234.875]< >1234.875000<
219>%.0f< >1234.875< >1235<
220>%.1f< >1234.875< >1234.9<
221>%-8.1f< >1234.875< >1234.9 <
222>%8.1f< >1234.875< > 1234.9<
223>%+-8.1f< >1234.875< >+1234.9 <
224>%+8.1f< >1234.875< > +1234.9<
225>%+-8.1f< >-1234.875< >-1234.9 <
226>%+8.1f< >-1234.875< > -1234.9<
227>%*.*f< >[5, 2, 12.3456]< >12.35<
228>%f< >0< >0.000000<
229>%.0f< >0< >0<
230>%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
231>%.0f< >0.1< >0<
22d4bb9c 232>%.0f< >0.6< >1< >Known to fail with sfio<
233>%.0f< >-0.6< >-1< >Known to fail with sfio<
234>%.0f< >1< >1<
235>%#.0f< >1< >1.<
4b19af01 236>%g< >12345.6789< >12345.7<
237>%+g< >12345.6789< >+12345.7<
238>%#g< >12345.6789< >12345.7<
239>%.0g< >12345.6789< >1e+04<
22d4bb9c 240>%#.0g< >12345.6789< >1.e+04<
4b19af01 241>%.2g< >12345.6789< >1.2e+04<
242>%.*g< >[2, 12345.6789]< >1.2e+04<
243>%.9g< >12345.6789< >12345.6789<
244>%12.9g< >12345.6789< > 12345.6789<
245>%012.9g< >12345.6789< >0012345.6789<
246>%-12.9g< >12345.6789< >12345.6789 <
247>%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
248>%-012.9g< >12345.6789< >12345.6789 <
249>%g< >-12345.6789< >-12345.7<
250>%+g< >-12345.6789< >-12345.7<
251>%g< >1234567.89< >1.23457e+06<
252>%+g< >1234567.89< >+1.23457e+06<
253>%#g< >1234567.89< >1.23457e+06<
254>%g< >-1234567.89< >-1.23457e+06<
255>%+g< >-1234567.89< >-1.23457e+06<
256>%#g< >-1234567.89< >-1.23457e+06<
257>%g< >0.00012345< >0.00012345<
258>%g< >0.000012345< >1.2345e-05<
259>%g< >1234567E96< >1.23457e+102<
260>%g< >.1234567E-101< >1.23457e-102<
261>%g< >0< >0<
262>%13g< >1234567.89< > 1.23457e+06<
263>%+13g< >1234567.89< > +1.23457e+06<
264>%013g< >1234567.89< >001.23457e+06<
265>%-13g< >1234567.89< >1.23457e+06 <
266>%h< >''< >%h INVALID<
267>%i< >123456.789< >123456< >Synonym for %d<
268>%j< >''< >%j INVALID<
269>%k< >''< >%k INVALID<
270>%l< >''< >%l INVALID<
271>%m< >''< >%m INVALID<
272>%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
273>%o< >2**32-1< >37777777777<
274>%+o< >2**32-1< >37777777777<
275>%#o< >2**32-1< >037777777777<
276>%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
277>%#p< >''< >%#p INVALID<
278>%q< >''< >%q INVALID<
279>%r< >''< >%r INVALID<
280>%s< >'string'< >string<
281>%10s< >'string'< > string<
282>%+10s< >'string'< > string<
283>%#10s< >'string'< > string<
284>%010s< >'string'< >0000string<
285>%0*s< >[10, 'string']< >0000string<
286>%-10s< >'string'< >string <
287>%3s< >'string'< >string<
288>%.3s< >'string'< >str<
289>%.*s< >[3, 'string']< >str<
290>%t< >''< >%t INVALID<
291>%u< >2**32-1< >4294967295<
292>%+u< >2**32-1< >4294967295<
293>%#u< >2**32-1< >4294967295<
294>%12u< >2**32-1< > 4294967295<
295>%012u< >2**32-1< >004294967295<
296>%-12u< >2**32-1< >4294967295 <
297>%-012u< >2**32-1< >4294967295 <
298>%v< >''< >%v INVALID<
299>%w< >''< >%w INVALID<
300>%x< >2**32-1< >ffffffff<
301>%+x< >2**32-1< >ffffffff<
302>%#x< >2**32-1< >0xffffffff<
303>%10x< >2**32-1< > ffffffff<
304>%010x< >2**32-1< >00ffffffff<
305>%-10x< >2**32-1< >ffffffff <
306>%-010x< >2**32-1< >ffffffff <
307>%0-10x< >2**32-1< >ffffffff <
308>%0*x< >[-10, ,2**32-1]< >ffffffff <
309>%y< >''< >%y INVALID<
310>%z< >''< >%z INVALID<