This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Allow @+ and @- to be doublequoted, from Simon Cozens.
[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';
20822f61 11 @INC = '../lib';
9f1b1f2d
GS
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#
02a4ca6d
DD
79# The following tests are not currently run, for the reasons stated:
80
81=pod
82
83=begin problematic
84
85>%.0f< >-0.1< >-0< >C library bug: no minus on VMS, HP-UX<
86>%.0f< >1.5< >2< >Standard vague: no rounding rules<
87>%.0f< >2.5< >2< >Standard vague: no rounding rules<
88
89=end problematic
90
91=cut
92
be3174d2
GS
93# template data result
94__END__
be3174d2 95>%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
c2e66d9e 96>%6 .6s< >''< >%6 .6s INVALID<
be3174d2 97>%6.6 s< >''< >%6.6 s INVALID<
c2e66d9e 98>%A< >''< >%A INVALID<
be3174d2
GS
99>%B< >''< >%B INVALID<
100>%C< >''< >%C INVALID<
101>%D< >0x7fffffff< >2147483647< >Synonym for %ld<
102>%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
103>%F< >123456.789< >123456.789000< >Synonym for %f<
104>%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
c2e66d9e
GS
105>%G< >1234567e96< >1.23457E+102<
106>%G< >.1234567e-101< >1.23457E-102<
be3174d2
GS
107>%G< >12345.6789< >12345.7<
108>%H< >''< >%H INVALID<
109>%I< >''< >%I INVALID<
110>%J< >''< >%J INVALID<
111>%K< >''< >%K INVALID<
112>%L< >''< >%L INVALID<
113>%M< >''< >%M INVALID<
114>%N< >''< >%N INVALID<
c2e66d9e 115>%O< >2**32-1< >37777777777< >Synonum for %lo<
be3174d2
GS
116>%P< >''< >%P INVALID<
117>%Q< >''< >%Q INVALID<
118>%R< >''< >%R INVALID<
119>%S< >''< >%S INVALID<
120>%T< >''< >%T INVALID<
c2e66d9e 121>%U< >2**32-1< >4294967295< >Synonum for %lu<
be3174d2
GS
122>%V< >''< >%V INVALID<
123>%W< >''< >%W INVALID<
124>%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
125>%#X< >2**32-1< >0XFFFFFFFF<
126>%Y< >''< >%Y INVALID<
127>%Z< >''< >%Z INVALID<
128>%a< >''< >%a INVALID<
129>%b< >2**32-1< >11111111111111111111111111111111<
130>%+b< >2**32-1< >11111111111111111111111111111111<
131>%#b< >2**32-1< >0b11111111111111111111111111111111<
132>%34b< >2**32-1< > 11111111111111111111111111111111<
133>%034b< >2**32-1< >0011111111111111111111111111111111<
134>%-34b< >2**32-1< >11111111111111111111111111111111 <
135>%-034b< >2**32-1< >11111111111111111111111111111111 <
136>%c< >ord('A')< >A<
137>%10c< >ord('A')< > A<
138>%#10c< >ord('A')< > A< ># modifier: no effect<
139>%010c< >ord('A')< >000000000A<
140>%10lc< >ord('A')< > A< >l modifier: no effect<
141>%10hc< >ord('A')< > A< >h modifier: no effect<
142>%10.5c< >ord('A')< > A< >precision: no effect<
143>%-10c< >ord('A')< >A <
144>%d< >123456.789< >123456<
145>%d< >-123456.789< >-123456<
146>%d< >0< >0<
147>%+d< >0< >+0<
148>%0d< >0< >0<
149>%.0d< >0< ><
150>%+.0d< >0< >+<
151>%.0d< >1< >1<
152>%d< >1< >1<
153>%+d< >1< >+1<
154>%#3.2d< >1< > 01< ># modifier: no effect<
155>%3.2d< >1< > 01<
156>%03.2d< >1< >001<
157>%-3.2d< >1< >01 <
158>%-03.2d< >1< >01 < >zero pad + left just.: no effect<
159>%d< >-1< >-1<
160>%+d< >-1< >-1<
161>%hd< >1< >1< >More extensive testing of<
162>%ld< >1< >1< >length modifiers would be<
163>%Vd< >1< >1< >platform-specific<
164>%vd< >chr(1)< >1<
165>%+vd< >chr(1)< >+1<
166>%#vd< >chr(1)< >1<
167>%vd< >"\01\02\03"< >1.2.3<
168>%v.3d< >"\01\02\03"< >001.002.003<
169>%v03d< >"\01\02\03"< >001.002.003<
170>%v-3d< >"\01\02\03"< >1 .2 .3 <
171>%v+-3d< >"\01\02\03"< >+1 .2 .3 <
172>%v4.3d< >"\01\02\03"< > 001. 002. 003<
173>%v04.3d< >"\01\02\03"< >0001.0002.0003<
d5365ef1 174>%*v02d< >['-', "\0\7\14"]< >00-07-12<
bb2899ba
GS
175>%v.*d< >[3, "\01\02\03"]< >001.002.003<
176>%v0*d< >[3, "\01\02\03"]< >001.002.003<
177>%v-*d< >[3, "\01\02\03"]< >1 .2 .3 <
178>%v+-*d< >[3, "\01\02\03"]< >+1 .2 .3 <
179>%v*.*d< >[4, 3, "\01\02\03"]< > 001. 002. 003<
180>%v0*.*d< >[4, 3, "\01\02\03"]< >0001.0002.0003<
181>%*v0*d< >['-', 2, "\0\7\13"]< >00-07-11<
be3174d2 182>%e< >1234.875< >1.234875e+03<
c2e66d9e
GS
183>%e< >0.000012345< >1.234500e-05<
184>%e< >1234567E96< >1.234567e+102<
185>%e< >0< >0.000000e+00<
186>%e< >.1234567E-101< >1.234567e-102<
be3174d2
GS
187>%+e< >1234.875< >+1.234875e+03<
188>%#e< >1234.875< >1.234875e+03<
189>%e< >-1234.875< >-1.234875e+03<
190>%+e< >-1234.875< >-1.234875e+03<
191>%#e< >-1234.875< >-1.234875e+03<
192>%.0e< >1234.875< >1e+03<
02a4ca6d 193>%#.0e< >1234.875< >1.e+03<
be3174d2
GS
194>%.*e< >[0, 1234.875]< >1e+03<
195>%.1e< >1234.875< >1.2e+03<
196>%-12.4e< >1234.875< >1.2349e+03 <
197>%12.4e< >1234.875< > 1.2349e+03<
198>%+-12.4e< >1234.875< >+1.2349e+03 <
199>%+12.4e< >1234.875< > +1.2349e+03<
200>%+-12.4e< >-1234.875< >-1.2349e+03 <
201>%+12.4e< >-1234.875< > -1.2349e+03<
202>%f< >1234.875< >1234.875000<
203>%+f< >1234.875< >+1234.875000<
204>%#f< >1234.875< >1234.875000<
205>%f< >-1234.875< >-1234.875000<
206>%+f< >-1234.875< >-1234.875000<
207>%#f< >-1234.875< >-1234.875000<
208>%6f< >1234.875< >1234.875000<
209>%*f< >[6, 1234.875]< >1234.875000<
210>%.0f< >1234.875< >1235<
211>%.1f< >1234.875< >1234.9<
212>%-8.1f< >1234.875< >1234.9 <
213>%8.1f< >1234.875< > 1234.9<
214>%+-8.1f< >1234.875< >+1234.9 <
215>%+8.1f< >1234.875< > +1234.9<
216>%+-8.1f< >-1234.875< >-1234.9 <
217>%+8.1f< >-1234.875< > -1234.9<
218>%*.*f< >[5, 2, 12.3456]< >12.35<
c2e66d9e
GS
219>%f< >0< >0.000000<
220>%.0f< >0< >0<
221>%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
d5365ef1 222>%.0f< >0.1< >0<
02a4ca6d
DD
223>%.0f< >0.6< >1< >Known to fail with sfio<
224>%.0f< >-0.6< >-1< >Known to fail with sfio<
225>%.0f< >1< >1<
226>%#.0f< >1< >1.<
be3174d2
GS
227>%g< >12345.6789< >12345.7<
228>%+g< >12345.6789< >+12345.7<
229>%#g< >12345.6789< >12345.7<
230>%.0g< >12345.6789< >1e+04<
02a4ca6d 231>%#.0g< >12345.6789< >1.e+04<
be3174d2
GS
232>%.2g< >12345.6789< >1.2e+04<
233>%.*g< >[2, 12345.6789]< >1.2e+04<
234>%.9g< >12345.6789< >12345.6789<
235>%12.9g< >12345.6789< > 12345.6789<
236>%012.9g< >12345.6789< >0012345.6789<
237>%-12.9g< >12345.6789< >12345.6789 <
238>%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
239>%-012.9g< >12345.6789< >12345.6789 <
240>%g< >-12345.6789< >-12345.7<
241>%+g< >-12345.6789< >-12345.7<
242>%g< >1234567.89< >1.23457e+06<
243>%+g< >1234567.89< >+1.23457e+06<
244>%#g< >1234567.89< >1.23457e+06<
245>%g< >-1234567.89< >-1.23457e+06<
246>%+g< >-1234567.89< >-1.23457e+06<
247>%#g< >-1234567.89< >-1.23457e+06<
c2e66d9e
GS
248>%g< >0.00012345< >0.00012345<
249>%g< >0.000012345< >1.2345e-05<
250>%g< >1234567E96< >1.23457e+102<
251>%g< >.1234567E-101< >1.23457e-102<
252>%g< >0< >0<
be3174d2
GS
253>%13g< >1234567.89< > 1.23457e+06<
254>%+13g< >1234567.89< > +1.23457e+06<
255>%013g< >1234567.89< >001.23457e+06<
256>%-13g< >1234567.89< >1.23457e+06 <
257>%h< >''< >%h INVALID<
258>%i< >123456.789< >123456< >Synonym for %d<
259>%j< >''< >%j INVALID<
260>%k< >''< >%k INVALID<
261>%l< >''< >%l INVALID<
262>%m< >''< >%m INVALID<
263>%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
264>%o< >2**32-1< >37777777777<
265>%+o< >2**32-1< >37777777777<
266>%#o< >2**32-1< >037777777777<
267>%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
d5365ef1 268>%#p< >''< >%#p INVALID<
be3174d2
GS
269>%q< >''< >%q INVALID<
270>%r< >''< >%r INVALID<
271>%s< >'string'< >string<
272>%10s< >'string'< > string<
273>%+10s< >'string'< > string<
274>%#10s< >'string'< > string<
275>%010s< >'string'< >0000string<
276>%0*s< >[10, 'string']< >0000string<
277>%-10s< >'string'< >string <
278>%3s< >'string'< >string<
279>%.3s< >'string'< >str<
280>%.*s< >[3, 'string']< >str<
281>%t< >''< >%t INVALID<
282>%u< >2**32-1< >4294967295<
283>%+u< >2**32-1< >4294967295<
284>%#u< >2**32-1< >4294967295<
285>%12u< >2**32-1< > 4294967295<
286>%012u< >2**32-1< >004294967295<
287>%-12u< >2**32-1< >4294967295 <
288>%-012u< >2**32-1< >4294967295 <
289>%v< >''< >%v INVALID<
290>%w< >''< >%w INVALID<
291>%x< >2**32-1< >ffffffff<
292>%+x< >2**32-1< >ffffffff<
293>%#x< >2**32-1< >0xffffffff<
294>%10x< >2**32-1< > ffffffff<
295>%010x< >2**32-1< >00ffffffff<
296>%-10x< >2**32-1< >ffffffff <
297>%-010x< >2**32-1< >ffffffff <
298>%0-10x< >2**32-1< >ffffffff <
299>%0*x< >[-10, ,2**32-1]< >ffffffff <
300>%y< >''< >%y INVALID<
301>%z< >''< >%z INVALID<