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).
17 push @tests, [split(/<\s*>/, $_, 4)];
20 print '1..', scalar @tests, "\n";
22 $SIG{__WARN__} = sub {
23 if ($_[0] =~ /^Invalid conversion/) {
25 } elsif ($_[0] =~ /^Use of uninitialized value/) {
32 for ($i = 1; @tests; $i++) {
33 ($template, $data, $result, $comment) = @{shift @tests};
34 $evalData = eval $data;
36 $x = sprintf(">$template<",
37 defined @$evalData ? @$evalData : $evalData);
38 substr($x, -1, 0) = $w if $w;
39 # $x may have 3 exponent digits, not 2
41 if ($y =~ s/([Ee][-+])0(\d)/$1$2/) {
42 # if result is left-adjusted, append extra space
43 if ($template =~ /%\+?\-/ and $result =~ / $/) {
46 # if result is zero-filled, add extra zero
47 elsif ($template =~ /%\+?0/ and $result =~ /^0/) {
50 # if result is right-adjusted, prepend extra space
51 elsif ($result =~ /^ /) {
56 if ($x eq ">$result<") {
59 elsif ($y eq ">$result<") # Some C libraries always give
60 { # three-digit exponent
61 print("ok $i # >$result< $x three-digit exponent accepted\n");
63 elsif ($result =~ /[-+]\d{3}$/ &&
64 # Suppress tests with modulo of exponent >= 100 on platforms
65 # which can't handle such magnitudes (or where we can't tell).
66 ((!eval {require POSIX}) || # Costly: only do this if we must!
67 (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3))
69 print("ok $i # >$template< >$data< >$result<",
70 " Suppressed: exponent out of range?\n")
73 $y = ($x eq $y ? "" : " => $y");
74 print("not ok $i >$template< >$data< >$result< $x$y",
75 $comment ? " # $comment\n" : "\n");
79 # In each of the the following lines, there are three required fields:
80 # printf template, data to be formatted (as a Perl expression), and
81 # expected result of formatting. An optional fourth field can contain
82 # a comment. Each field is delimited by a starting '>' and a
83 # finishing '<'; any whitespace outside these start and end marks is
84 # not part of the field. If formatting requires more than one data
85 # item (for example, if variable field widths are used), the Perl data
86 # expression should return a reference to an array having the requisite
87 # number of elements. Even so, subterfuge is sometimes required: see
88 # tests for %n and %p.
90 # The following tests are not currently run, for the reasons stated:
96 >%.0f< >-0.1< >-0< >C library bug: no minus on VMS, HP-UX<
97 >%.0f< >1.5< >2< >Standard vague: no rounding rules<
98 >%.0f< >2.5< >2< >Standard vague: no rounding rules<
104 # template data result
106 >%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
107 >%6 .6s< >''< >%6 .6s INVALID<
108 >%6.6 s< >''< >%6.6 s INVALID<
109 >%A< >''< >%A INVALID<
110 >%B< >''< >%B INVALID<
111 >%C< >''< >%C INVALID<
112 >%D< >0x7fffffff< >2147483647< >Synonym for %ld<
113 >%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
114 >%F< >123456.789< >123456.789000< >Synonym for %f<
115 >%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
116 >%G< >1234567e96< >1.23457E+102<
117 >%G< >.1234567e-101< >1.23457E-102<
118 >%G< >12345.6789< >12345.7<
119 >%H< >''< >%H INVALID<
120 >%I< >''< >%I INVALID<
121 >%J< >''< >%J INVALID<
122 >%K< >''< >%K INVALID<
123 >%L< >''< >%L INVALID<
124 >%M< >''< >%M INVALID<
125 >%N< >''< >%N INVALID<
126 >%O< >2**32-1< >37777777777< >Synonum for %lo<
127 >%P< >''< >%P INVALID<
128 >%Q< >''< >%Q INVALID<
129 >%R< >''< >%R INVALID<
130 >%S< >''< >%S INVALID<
131 >%T< >''< >%T INVALID<
132 >%U< >2**32-1< >4294967295< >Synonum for %lu<
133 >%V< >''< >%V INVALID<
134 >%W< >''< >%W INVALID<
135 >%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
136 >%#X< >2**32-1< >0XFFFFFFFF<
137 >%Y< >''< >%Y INVALID<
138 >%Z< >''< >%Z INVALID<
139 >%a< >''< >%a INVALID<
140 >%b< >2**32-1< >11111111111111111111111111111111<
141 >%+b< >2**32-1< >11111111111111111111111111111111<
142 >%#b< >2**32-1< >0b11111111111111111111111111111111<
143 >%34b< >2**32-1< > 11111111111111111111111111111111<
144 >%034b< >2**32-1< >0011111111111111111111111111111111<
145 >%-34b< >2**32-1< >11111111111111111111111111111111 <
146 >%-034b< >2**32-1< >11111111111111111111111111111111 <
148 >%10c< >ord('A')< > A<
149 >%#10c< >ord('A')< > A< ># modifier: no effect<
150 >%010c< >ord('A')< >000000000A<
151 >%10lc< >ord('A')< > A< >l modifier: no effect<
152 >%10hc< >ord('A')< > A< >h modifier: no effect<
153 >%10.5c< >ord('A')< > A< >precision: no effect<
154 >%-10c< >ord('A')< >A <
155 >%d< >123456.789< >123456<
156 >%d< >-123456.789< >-123456<
165 >%#3.2d< >1< > 01< ># modifier: no effect<
169 >%-03.2d< >1< >01 < >zero pad + left just.: no effect<
172 >%hd< >1< >1< >More extensive testing of<
173 >%ld< >1< >1< >length modifiers would be<
174 >%Vd< >1< >1< >platform-specific<
178 >%vd< >"\01\02\03"< >1.2.3<
179 >%v.3d< >"\01\02\03"< >001.002.003<
180 >%0v3d< >"\01\02\03"< >001.002.003<
181 >%-v3d< >"\01\02\03"< >1 .2 .3 <
182 >%+-v3d< >"\01\02\03"< >+1 .2 .3 <
183 >%v4.3d< >"\01\02\03"< > 001. 002. 003<
184 >%0v4.3d< >"\01\02\03"< >0001.0002.0003<
185 >%0*v2d< >['-', "\0\7\14"]< >00-07-12<
186 >%v.*d< >["\01\02\03", 3]< >001.002.003<
187 >%0v*d< >["\01\02\03", 3]< >001.002.003<
188 >%-v*d< >["\01\02\03", 3]< >1 .2 .3 <
189 >%+-v*d< >["\01\02\03", 3]< >+1 .2 .3 <
190 >%v*.*d< >["\01\02\03", 4, 3]< > 001. 002. 003<
191 >%0v*.*d< >["\01\02\03", 4, 3]< >0001.0002.0003<
192 >%0*v*d< >['-', "\0\7\13", 2]< >00-07-11<
193 >%e< >1234.875< >1.234875e+03<
194 >%e< >0.000012345< >1.234500e-05<
195 >%e< >1234567E96< >1.234567e+102<
196 >%e< >0< >0.000000e+00<
197 >%e< >.1234567E-101< >1.234567e-102<
198 >%+e< >1234.875< >+1.234875e+03<
199 >%#e< >1234.875< >1.234875e+03<
200 >%e< >-1234.875< >-1.234875e+03<
201 >%+e< >-1234.875< >-1.234875e+03<
202 >%#e< >-1234.875< >-1.234875e+03<
203 >%.0e< >1234.875< >1e+03<
204 >%#.0e< >1234.875< >1.e+03<
205 >%.*e< >[0, 1234.875]< >1e+03<
206 >%.1e< >1234.875< >1.2e+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 >%+-12.4e< >-1234.875< >-1.2349e+03 <
212 >%+12.4e< >-1234.875< > -1.2349e+03<
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 >%+f< >-1234.875< >-1234.875000<
218 >%#f< >-1234.875< >-1234.875000<
219 >%6f< >1234.875< >1234.875000<
220 >%*f< >[6, 1234.875]< >1234.875000<
221 >%.0f< >1234.875< >1235<
222 >%.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 >%+-8.1f< >-1234.875< >-1234.9 <
228 >%+8.1f< >-1234.875< > -1234.9<
229 >%*.*f< >[5, 2, 12.3456]< >12.35<
232 >%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
234 >%.0f< >0.6< >1< >Known to fail with sfio and (irix|nonstop-ux|powerux)<
235 >%.0f< >-0.6< >-1< >Known to fail with sfio and (irix|nonstop-ux|powerux)<
238 >%g< >12345.6789< >12345.7<
239 >%+g< >12345.6789< >+12345.7<
240 >%#g< >12345.6789< >12345.7<
241 >%.0g< >12345.6789< >1e+04<
242 >%#.0g< >12345.6789< >1.e+04<
243 >%.2g< >12345.6789< >1.2e+04<
244 >%.*g< >[2, 12345.6789]< >1.2e+04<
245 >%.9g< >12345.6789< >12345.6789<
246 >%12.9g< >12345.6789< > 12345.6789<
247 >%012.9g< >12345.6789< >0012345.6789<
248 >%-12.9g< >12345.6789< >12345.6789 <
249 >%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
250 >%-012.9g< >12345.6789< >12345.6789 <
251 >%g< >-12345.6789< >-12345.7<
252 >%+g< >-12345.6789< >-12345.7<
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< >-1234567.89< >-1.23457e+06<
258 >%#g< >-1234567.89< >-1.23457e+06<
259 >%g< >0.00012345< >0.00012345<
260 >%g< >0.000012345< >1.2345e-05<
261 >%g< >1234567E96< >1.23457e+102<
262 >%g< >.1234567E-101< >1.23457e-102<
264 >%13g< >1234567.89< > 1.23457e+06<
265 >%+13g< >1234567.89< > +1.23457e+06<
266 >%013g< >1234567.89< >001.23457e+06<
267 >%-13g< >1234567.89< >1.23457e+06 <
268 >%h< >''< >%h INVALID<
269 >%i< >123456.789< >123456< >Synonym for %d<
270 >%j< >''< >%j INVALID<
271 >%k< >''< >%k INVALID<
272 >%l< >''< >%l INVALID<
273 >%m< >''< >%m INVALID<
274 >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
275 >%o< >2**32-1< >37777777777<
276 >%+o< >2**32-1< >37777777777<
277 >%#o< >2**32-1< >037777777777<
278 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
279 >%#p< >''< >%#p INVALID<
280 >%q< >''< >%q INVALID<
281 >%r< >''< >%r INVALID<
282 >%s< >'string'< >string<
283 >%10s< >'string'< > string<
284 >%+10s< >'string'< > string<
285 >%#10s< >'string'< > string<
286 >%010s< >'string'< >0000string<
287 >%0*s< >[10, 'string']< >0000string<
288 >%-10s< >'string'< >string <
289 >%3s< >'string'< >string<
290 >%.3s< >'string'< >str<
291 >%.*s< >[3, 'string']< >str<
292 >%t< >''< >%t INVALID<
293 >%u< >2**32-1< >4294967295<
294 >%+u< >2**32-1< >4294967295<
295 >%#u< >2**32-1< >4294967295<
296 >%12u< >2**32-1< > 4294967295<
297 >%012u< >2**32-1< >004294967295<
298 >%-12u< >2**32-1< >4294967295 <
299 >%-012u< >2**32-1< >4294967295 <
300 >%v< >''< >%v INVALID<
301 >%w< >''< >%w INVALID<
302 >%x< >2**32-1< >ffffffff<
303 >%+x< >2**32-1< >ffffffff<
304 >%#x< >2**32-1< >0xffffffff<
305 >%10x< >2**32-1< > ffffffff<
306 >%010x< >2**32-1< >00ffffffff<
307 >%-10x< >2**32-1< >ffffffff <
308 >%-010x< >2**32-1< >ffffffff <
309 >%0-10x< >2**32-1< >ffffffff <
310 >%0*x< >[-10, ,2**32-1]< >ffffffff <
311 >%y< >''< >%y INVALID<
312 >%z< >''< >%z INVALID<
313 >%2$d %1$d< >[12, 34]< >34 12<
314 >%*2$d< >[12, 3]< > 12<
315 >%2$d %d< >[12, 34]< >34 12<
316 >%2$d %d %d< >[12, 34]< >34 12 34<
317 >%3$d %d %d< >[12, 34, 56]< >56 12 34<
318 >%2$*3$d %d< >[12, 34, 3]< > 34 12<
319 >%*3$2$d %d< >[12, 34, 3]< >%*3$2$d 34 INVALID<
320 >%2$d< >12< >0 UNINIT<
321 >%0$d< >12< >%0$d INVALID<
322 >%1$$d< >12< >%1$$d INVALID<
323 >%1$1$d< >12< >%1$1$d INVALID<
324 >%*2$*2$d< >[12, 3]< >%*2$*2$d INVALID<
325 >%*2*2$d< >[12, 3]< >%*2*2$d INVALID<