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).
11 unshift @INC, '../lib';
17 push @tests, [split(/<\s*>/, $_, 4)];
20 print '1..', scalar @tests, "\n";
22 $SIG{__WARN__} = sub {
23 if ($_[0] =~ /^Invalid conversion/) {
30 for ($i = 1; @tests; $i++) {
31 ($template, $data, $result, $comment) = @{shift @tests};
32 $evalData = eval $data;
34 $x = sprintf(">$template<",
35 defined @$evalData ? @$evalData : $evalData);
36 substr($x, -1, 0) = $w if $w;
37 ($y = $x) =~ s/([Ee][-+])0(\d)/$1$2/g; # $y has 3 exponent digits, not 2
38 if ($x eq ">$result<") {
41 elsif ($y eq ">$result<") # Some C libraries always give
42 { # three-digit exponent
43 print("ok $i >$result< $x # three-digit exponent accepted\n");
46 print("not ok $i >$template< >$data< >$result< $x",
47 $comment ? " # $comment\n" : "\n");
51 # In each of the the following lines, there are three required fields:
52 # printf template, data to be formatted (as a Perl expression), and
53 # expected result of formatting. An optional fourth field can contain
54 # a comment. Each field is delimited by a starting '>' and a
55 # finishing '<'; any whitespace outside these start and end marks is
56 # not part of the field. If formatting requires more than one data
57 # item (for example, if variable field widths are used), the Perl data
58 # expression should return a reference to an array having the requisite
59 # number of elements. Even so, subterfuge is sometimes required: see
60 # tests for %n and %p.
62 # template data result
64 >%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
65 >%6 .6s< >''< >%6 .6s INVALID<
66 >%6.6 s< >''< >%6.6 s INVALID<
67 >%A< >''< >%A INVALID<
68 >%B< >''< >%B INVALID<
69 >%C< >''< >%C INVALID<
70 >%D< >0x7fffffff< >2147483647< >Synonym for %ld<
71 >%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
72 >%F< >123456.789< >123456.789000< >Synonym for %f<
73 >%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
74 >%G< >1234567e96< >1.23457E+102<
75 >%G< >.1234567e-101< >1.23457E-102<
76 >%G< >12345.6789< >12345.7<
77 >%H< >''< >%H INVALID<
78 >%I< >''< >%I INVALID<
79 >%J< >''< >%J INVALID<
80 >%K< >''< >%K INVALID<
81 >%L< >''< >%L INVALID<
82 >%M< >''< >%M INVALID<
83 >%N< >''< >%N INVALID<
84 >%O< >2**32-1< >37777777777< >Synonum for %lo<
85 >%P< >''< >%P INVALID<
86 >%Q< >''< >%Q INVALID<
87 >%R< >''< >%R INVALID<
88 >%S< >''< >%S INVALID<
89 >%T< >''< >%T INVALID<
90 >%U< >2**32-1< >4294967295< >Synonum for %lu<
91 >%V< >''< >%V INVALID<
92 >%W< >''< >%W INVALID<
93 >%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
94 >%#X< >2**32-1< >0XFFFFFFFF<
95 >%Y< >''< >%Y INVALID<
96 >%Z< >''< >%Z INVALID<
97 >%a< >''< >%a INVALID<
98 >%b< >2**32-1< >11111111111111111111111111111111<
99 >%+b< >2**32-1< >11111111111111111111111111111111<
100 >%#b< >2**32-1< >0b11111111111111111111111111111111<
101 >%34b< >2**32-1< > 11111111111111111111111111111111<
102 >%034b< >2**32-1< >0011111111111111111111111111111111<
103 >%-34b< >2**32-1< >11111111111111111111111111111111 <
104 >%-034b< >2**32-1< >11111111111111111111111111111111 <
106 >%10c< >ord('A')< > A<
107 >%#10c< >ord('A')< > A< ># modifier: no effect<
108 >%010c< >ord('A')< >000000000A<
109 >%10lc< >ord('A')< > A< >l modifier: no effect<
110 >%10hc< >ord('A')< > A< >h modifier: no effect<
111 >%10.5c< >ord('A')< > A< >precision: no effect<
112 >%-10c< >ord('A')< >A <
113 >%d< >123456.789< >123456<
114 >%d< >-123456.789< >-123456<
123 >%#3.2d< >1< > 01< ># modifier: no effect<
127 >%-03.2d< >1< >01 < >zero pad + left just.: no effect<
130 >%hd< >1< >1< >More extensive testing of<
131 >%ld< >1< >1< >length modifiers would be<
132 >%Vd< >1< >1< >platform-specific<
136 >%vd< >"\01\02\03"< >1.2.3<
137 >%v.3d< >"\01\02\03"< >001.002.003<
138 >%v03d< >"\01\02\03"< >001.002.003<
139 >%v-3d< >"\01\02\03"< >1 .2 .3 <
140 >%v+-3d< >"\01\02\03"< >+1 .2 .3 <
141 >%v4.3d< >"\01\02\03"< > 001. 002. 003<
142 >%v04.3d< >"\01\02\03"< >0001.0002.0003<
143 >%*v02d< >['-', "\0\7\13"]< >00-07-11<
144 >%e< >1234.875< >1.234875e+03<
145 >%e< >0.000012345< >1.234500e-05<
146 >%e< >1234567E96< >1.234567e+102<
147 >%e< >0< >0.000000e+00<
148 >%e< >.1234567E-101< >1.234567e-102<
149 >%+e< >1234.875< >+1.234875e+03<
150 >%#e< >1234.875< >1.234875e+03<
151 >%e< >-1234.875< >-1.234875e+03<
152 >%+e< >-1234.875< >-1.234875e+03<
153 >%#e< >-1234.875< >-1.234875e+03<
154 >%.0e< >1234.875< >1e+03<
155 >%.*e< >[0, 1234.875]< >1e+03<
156 >%.1e< >1234.875< >1.2e+03<
157 >%-12.4e< >1234.875< >1.2349e+03 <
158 >%12.4e< >1234.875< > 1.2349e+03<
159 >%+-12.4e< >1234.875< >+1.2349e+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 >%f< >1234.875< >1234.875000<
164 >%+f< >1234.875< >+1234.875000<
165 >%#f< >1234.875< >1234.875000<
166 >%f< >-1234.875< >-1234.875000<
167 >%+f< >-1234.875< >-1234.875000<
168 >%#f< >-1234.875< >-1234.875000<
169 >%6f< >1234.875< >1234.875000<
170 >%*f< >[6, 1234.875]< >1234.875000<
171 >%.0f< >1234.875< >1235<
172 >%.1f< >1234.875< >1234.9<
173 >%-8.1f< >1234.875< >1234.9 <
174 >%8.1f< >1234.875< > 1234.9<
175 >%+-8.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 >%*.*f< >[5, 2, 12.3456]< >12.35<
182 >%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
185 >%g< >12345.6789< >12345.7<
186 >%+g< >12345.6789< >+12345.7<
187 >%#g< >12345.6789< >12345.7<
188 >%.0g< >12345.6789< >1e+04<
189 >%.2g< >12345.6789< >1.2e+04<
190 >%.*g< >[2, 12345.6789]< >1.2e+04<
191 >%.9g< >12345.6789< >12345.6789<
192 >%12.9g< >12345.6789< > 12345.6789<
193 >%012.9g< >12345.6789< >0012345.6789<
194 >%-12.9g< >12345.6789< >12345.6789 <
195 >%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
196 >%-012.9g< >12345.6789< >12345.6789 <
197 >%g< >-12345.6789< >-12345.7<
198 >%+g< >-12345.6789< >-12345.7<
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 >%+g< >-1234567.89< >-1.23457e+06<
204 >%#g< >-1234567.89< >-1.23457e+06<
205 >%g< >0.00012345< >0.00012345<
206 >%g< >0.000012345< >1.2345e-05<
207 >%g< >1234567E96< >1.23457e+102<
208 >%g< >.1234567E-101< >1.23457e-102<
210 >%13g< >1234567.89< > 1.23457e+06<
211 >%+13g< >1234567.89< > +1.23457e+06<
212 >%013g< >1234567.89< >001.23457e+06<
213 >%-13g< >1234567.89< >1.23457e+06 <
214 >%h< >''< >%h INVALID<
215 >%i< >123456.789< >123456< >Synonym for %d<
216 >%j< >''< >%j INVALID<
217 >%k< >''< >%k INVALID<
218 >%l< >''< >%l INVALID<
219 >%m< >''< >%m INVALID<
220 >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
221 >%o< >2**32-1< >37777777777<
222 >%+o< >2**32-1< >37777777777<
223 >%#o< >2**32-1< >037777777777<
224 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
225 >%q< >''< >%q INVALID<
226 >%r< >''< >%r INVALID<
227 >%s< >'string'< >string<
228 >%10s< >'string'< > string<
229 >%+10s< >'string'< > string<
230 >%#10s< >'string'< > string<
231 >%010s< >'string'< >0000string<
232 >%0*s< >[10, 'string']< >0000string<
233 >%-10s< >'string'< >string <
234 >%3s< >'string'< >string<
235 >%.3s< >'string'< >str<
236 >%.*s< >[3, 'string']< >str<
237 >%t< >''< >%t INVALID<
238 >%u< >2**32-1< >4294967295<
239 >%+u< >2**32-1< >4294967295<
240 >%#u< >2**32-1< >4294967295<
241 >%12u< >2**32-1< > 4294967295<
242 >%012u< >2**32-1< >004294967295<
243 >%-12u< >2**32-1< >4294967295 <
244 >%-012u< >2**32-1< >4294967295 <
245 >%v< >''< >%v INVALID<
246 >%w< >''< >%w INVALID<
247 >%x< >2**32-1< >ffffffff<
248 >%+x< >2**32-1< >ffffffff<
249 >%#x< >2**32-1< >0xffffffff<
250 >%10x< >2**32-1< > ffffffff<
251 >%010x< >2**32-1< >00ffffffff<
252 >%-10x< >2**32-1< >ffffffff <
253 >%-010x< >2**32-1< >ffffffff <
254 >%0-10x< >2**32-1< >ffffffff <
255 >%0*x< >[-10, ,2**32-1]< >ffffffff <
256 >%y< >''< >%y INVALID<
257 >%z< >''< >%z INVALID<