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 # $x may have 3 exponent digits, not 2
39 if ($y =~ s/([Ee][-+])0(\d)/$1$2/) {
40 # if result is left-adjusted, append extra space
41 if ($template =~ /%\+?\-/ and $result =~ / $/) {
44 # if result is zero-filled, add extra zero
45 elsif ($template =~ /%\+?0/ and $result =~ /^0/) {
48 # if result is right-adjusted, prepend extra space
49 elsif ($result =~ /^ /) {
54 if ($x eq ">$result<") {
57 elsif ($y eq ">$result<") # Some C libraries always give
58 { # three-digit exponent
59 print("ok $i >$result< $x # three-digit exponent accepted\n");
62 $y = ($x eq $y ? "" : " => $y");
63 print("not ok $i >$template< >$data< >$result< $x$y",
64 $comment ? " # $comment\n" : "\n");
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.
79 # The following tests are not currently run, for the reasons stated:
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<
93 # template data result
95 >%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
96 >%6 .6s< >''< >%6 .6s INVALID<
97 >%6.6 s< >''< >%6.6 s INVALID<
98 >%A< >''< >%A INVALID<
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"<
105 >%G< >1234567e96< >1.23457E+102<
106 >%G< >.1234567e-101< >1.23457E-102<
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<
115 >%O< >2**32-1< >37777777777< >Synonum for %lo<
116 >%P< >''< >%P INVALID<
117 >%Q< >''< >%Q INVALID<
118 >%R< >''< >%R INVALID<
119 >%S< >''< >%S INVALID<
120 >%T< >''< >%T INVALID<
121 >%U< >2**32-1< >4294967295< >Synonum for %lu<
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 <
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<
154 >%#3.2d< >1< > 01< ># modifier: no effect<
158 >%-03.2d< >1< >01 < >zero pad + left just.: no effect<
161 >%hd< >1< >1< >More extensive testing of<
162 >%ld< >1< >1< >length modifiers would be<
163 >%Vd< >1< >1< >platform-specific<
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<
174 >%*v02d< >['-', "\0\7\14"]< >00-07-12<
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<
182 >%e< >1234.875< >1.234875e+03<
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<
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<
193 >%#.0e< >1234.875< >1.e+03<
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 >%+012.4e< >1234.875< >+01.2349e+03<
201 >%+-12.4e< >-1234.875< >-1.2349e+03 <
202 >%+12.4e< >-1234.875< > -1.2349e+03<
203 >%+012.4e< >-1234.875< >-01.2349e+03<
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 >%+f< >-1234.875< >-1234.875000<
209 >%#f< >-1234.875< >-1234.875000<
210 >%6f< >1234.875< >1234.875000<
211 >%*f< >[6, 1234.875]< >1234.875000<
212 >%.0f< >1234.875< >1235<
213 >%.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 >%+08.1f< >1234.875< >+01234.9<
219 >%+-8.1f< >-1234.875< >-1234.9 <
220 >%+8.1f< >-1234.875< > -1234.9<
221 >%+08.1f< >-1234.875< >-01234.9<
222 >%*.*f< >[5, 2, 12.3456]< >12.35<
225 >%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
227 >%.0f< >0.6< >1< >Known to fail with sfio<
228 >%.0f< >-0.6< >-1< >Known to fail with sfio<
231 >%g< >12345.6789< >12345.7<
232 >%+g< >12345.6789< >+12345.7<
233 >%#g< >12345.6789< >12345.7<
234 >%.0g< >12345.6789< >1e+04<
235 >%#.0g< >12345.6789< >1.e+04<
236 >%.2g< >12345.6789< >1.2e+04<
237 >%.*g< >[2, 12345.6789]< >1.2e+04<
238 >%.9g< >12345.6789< >12345.6789<
239 >%12.9g< >12345.6789< > 12345.6789<
240 >%012.9g< >12345.6789< >0012345.6789<
241 >%-12.9g< >12345.6789< >12345.6789 <
242 >%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
243 >%-012.9g< >12345.6789< >12345.6789 <
244 >%g< >-12345.6789< >-12345.7<
245 >%+g< >-12345.6789< >-12345.7<
246 >%g< >1234567.89< >1.23457e+06<
247 >%+g< >1234567.89< >+1.23457e+06<
248 >%#g< >1234567.89< >1.23457e+06<
249 >%g< >-1234567.89< >-1.23457e+06<
250 >%+g< >-1234567.89< >-1.23457e+06<
251 >%#g< >-1234567.89< >-1.23457e+06<
252 >%g< >0.00012345< >0.00012345<
253 >%g< >0.000012345< >1.2345e-05<
254 >%g< >1234567E96< >1.23457e+102<
255 >%g< >.1234567E-101< >1.23457e-102<
257 >%13g< >1234567.89< > 1.23457e+06<
258 >%+13g< >1234567.89< > +1.23457e+06<
259 >%+013g< >1234567.89< >+01.23457e+06<
260 >%013g< >1234567.89< >001.23457e+06<
261 >%-13g< >1234567.89< >1.23457e+06 <
262 >%h< >''< >%h INVALID<
263 >%i< >123456.789< >123456< >Synonym for %d<
264 >%j< >''< >%j INVALID<
265 >%k< >''< >%k INVALID<
266 >%l< >''< >%l INVALID<
267 >%m< >''< >%m INVALID<
268 >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
269 >%o< >2**32-1< >37777777777<
270 >%+o< >2**32-1< >37777777777<
271 >%#o< >2**32-1< >037777777777<
272 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
273 >%#p< >''< >%#p INVALID<
274 >%q< >''< >%q INVALID<
275 >%r< >''< >%r INVALID<
276 >%s< >'string'< >string<
277 >%10s< >'string'< > string<
278 >%+10s< >'string'< > string<
279 >%#10s< >'string'< > string<
280 >%010s< >'string'< >0000string<
281 >%0*s< >[10, 'string']< >0000string<
282 >%-10s< >'string'< >string <
283 >%3s< >'string'< >string<
284 >%.3s< >'string'< >str<
285 >%.*s< >[3, 'string']< >str<
286 >%t< >''< >%t INVALID<
287 >%u< >2**32-1< >4294967295<
288 >%+u< >2**32-1< >4294967295<
289 >%#u< >2**32-1< >4294967295<
290 >%12u< >2**32-1< > 4294967295<
291 >%012u< >2**32-1< >004294967295<
292 >%-12u< >2**32-1< >4294967295 <
293 >%-012u< >2**32-1< >4294967295 <
294 >%v< >''< >%v INVALID<
295 >%w< >''< >%w INVALID<
296 >%x< >2**32-1< >ffffffff<
297 >%+x< >2**32-1< >ffffffff<
298 >%#x< >2**32-1< >0xffffffff<
299 >%10x< >2**32-1< > ffffffff<
300 >%010x< >2**32-1< >00ffffffff<
301 >%-10x< >2**32-1< >ffffffff <
302 >%-010x< >2**32-1< >ffffffff <
303 >%0-10x< >2**32-1< >ffffffff <
304 >%0*x< >[-10, ,2**32-1]< >ffffffff <
305 >%y< >''< >%y INVALID<
306 >%z< >''< >%z INVALID<