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