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 if ($^O eq 'os390' || $^O eq 's390') { # non-IEEE (s390 is UTS)
35 $data =~ s/([eE])96$/${1}63/; # smaller exponents
36 $result =~ s/([eE]\+)102$/${1}69/; # " "
37 $data =~ s/([eE])\-101$/${1}-56/; # larger exponents
38 $result =~ s/([eE])\-102$/${1}-57/; # " "
40 $evalData = eval $data;
42 $x = sprintf(">$template<",
43 defined @$evalData ? @$evalData : $evalData);
44 substr($x, -1, 0) = $w if $w;
45 # $x may have 3 exponent digits, not 2
47 if ($y =~ s/([Ee][-+])0(\d)/$1$2/) {
48 # if result is left-adjusted, append extra space
49 if ($template =~ /%\+?\-/ and $result =~ / $/) {
52 # if result is zero-filled, add extra zero
53 elsif ($template =~ /%\+?0/ and $result =~ /^0/) {
56 # if result is right-adjusted, prepend extra space
57 elsif ($result =~ /^ /) {
62 if ($x eq ">$result<") {
65 elsif ($y eq ">$result<") # Some C libraries always give
66 { # three-digit exponent
67 print("ok $i # >$result< $x three-digit exponent accepted\n");
69 elsif ($result =~ /[-+]\d{3}$/ &&
70 # Suppress tests with modulo of exponent >= 100 on platforms
71 # which can't handle such magnitudes (or where we can't tell).
72 ((!eval {require POSIX}) || # Costly: only do this if we must!
73 (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3))
75 print("ok $i # >$template< >$data< >$result<",
76 " Suppressed: exponent out of range?\n")
79 $y = ($x eq $y ? "" : " => $y");
80 print("not ok $i >$template< >$data< >$result< $x$y",
81 $comment ? " # $comment\n" : "\n");
85 # In each of the the following lines, there are three required fields:
86 # printf template, data to be formatted (as a Perl expression), and
87 # expected result of formatting. An optional fourth field can contain
88 # a comment. Each field is delimited by a starting '>' and a
89 # finishing '<'; any whitespace outside these start and end marks is
90 # not part of the field. If formatting requires more than one data
91 # item (for example, if variable field widths are used), the Perl data
92 # expression should return a reference to an array having the requisite
93 # number of elements. Even so, subterfuge is sometimes required: see
94 # tests for %n and %p.
96 # The following tests are not currently run, for the reasons stated:
102 >%.0f< >-0.1< >-0< >C library bug: no minus on VMS, HP-UX<
103 >%.0f< >1.5< >2< >Standard vague: no rounding rules<
104 >%.0f< >2.5< >2< >Standard vague: no rounding rules<
105 >%G< >1234567e96< >1.23457E+102< >exponent too big for OS/390<
106 >%G< >.1234567e-101< >1.23457E-102< >exponent too small for OS/390<
107 >%e< >1234567E96< >1.234567e+102< >exponent too big for OS/390<
108 >%e< >.1234567E-101< >1.234567e-102< >exponent too small for OS/390<
109 >%g< >.1234567E-101< >1.23457e-102< >exponent too small for OS/390<
110 >%g< >1234567E96< >1.23457e+102< >exponent too big for OS/390<
116 # template data result
118 >%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
119 >%6 .6s< >''< >%6 .6s INVALID<
120 >%6.6 s< >''< >%6.6 s INVALID<
121 >%A< >''< >%A INVALID<
122 >%B< >''< >%B INVALID<
123 >%C< >''< >%C INVALID<
124 >%D< >0x7fffffff< >2147483647< >Synonym for %ld<
125 >%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
126 >%F< >123456.789< >123456.789000< >Synonym for %f<
127 >%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
128 >%G< >1234567e96< >1.23457E+102<
129 >%G< >.1234567e-101< >1.23457E-102<
130 >%G< >12345.6789< >12345.7<
131 >%H< >''< >%H INVALID<
132 >%I< >''< >%I INVALID<
133 >%J< >''< >%J INVALID<
134 >%K< >''< >%K INVALID<
135 >%L< >''< >%L INVALID<
136 >%M< >''< >%M INVALID<
137 >%N< >''< >%N INVALID<
138 >%O< >2**32-1< >37777777777< >Synonum for %lo<
139 >%P< >''< >%P INVALID<
140 >%Q< >''< >%Q INVALID<
141 >%R< >''< >%R INVALID<
142 >%S< >''< >%S INVALID<
143 >%T< >''< >%T INVALID<
144 >%U< >2**32-1< >4294967295< >Synonum for %lu<
145 >%V< >''< >%V INVALID<
146 >%W< >''< >%W INVALID<
147 >%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
148 >%#X< >2**32-1< >0XFFFFFFFF<
149 >%Y< >''< >%Y INVALID<
150 >%Z< >''< >%Z INVALID<
151 >%a< >''< >%a INVALID<
152 >%b< >2**32-1< >11111111111111111111111111111111<
153 >%+b< >2**32-1< >11111111111111111111111111111111<
154 >%#b< >2**32-1< >0b11111111111111111111111111111111<
155 >%34b< >2**32-1< > 11111111111111111111111111111111<
156 >%034b< >2**32-1< >0011111111111111111111111111111111<
157 >%-34b< >2**32-1< >11111111111111111111111111111111 <
158 >%-034b< >2**32-1< >11111111111111111111111111111111 <
160 >%10c< >ord('A')< > A<
161 >%#10c< >ord('A')< > A< ># modifier: no effect<
162 >%010c< >ord('A')< >000000000A<
163 >%10lc< >ord('A')< > A< >l modifier: no effect<
164 >%10hc< >ord('A')< > A< >h modifier: no effect<
165 >%10.5c< >ord('A')< > A< >precision: no effect<
166 >%-10c< >ord('A')< >A <
167 >%d< >123456.789< >123456<
168 >%d< >-123456.789< >-123456<
177 >%#3.2d< >1< > 01< ># modifier: no effect<
181 >%-03.2d< >1< >01 < >zero pad + left just.: no effect<
184 >%hd< >1< >1< >More extensive testing of<
185 >%ld< >1< >1< >length modifiers would be<
186 >%Vd< >1< >1< >platform-specific<
190 >%vd< >"\01\02\03"< >1.2.3<
191 >%v.3d< >"\01\02\03"< >001.002.003<
192 >%0v3d< >"\01\02\03"< >001.002.003<
193 >%-v3d< >"\01\02\03"< >1 .2 .3 <
194 >%+-v3d< >"\01\02\03"< >+1 .2 .3 <
195 >%v4.3d< >"\01\02\03"< > 001. 002. 003<
196 >%0v4.3d< >"\01\02\03"< >0001.0002.0003<
197 >%0*v2d< >['-', "\0\7\14"]< >00-07-12<
198 >%v.*d< >["\01\02\03", 3]< >001.002.003<
199 >%0v*d< >["\01\02\03", 3]< >001.002.003<
200 >%-v*d< >["\01\02\03", 3]< >1 .2 .3 <
201 >%+-v*d< >["\01\02\03", 3]< >+1 .2 .3 <
202 >%v*.*d< >["\01\02\03", 4, 3]< > 001. 002. 003<
203 >%0v*.*d< >["\01\02\03", 4, 3]< >0001.0002.0003<
204 >%0*v*d< >['-', "\0\7\13", 2]< >00-07-11<
205 >%e< >1234.875< >1.234875e+03<
206 >%e< >0.000012345< >1.234500e-05<
207 >%e< >1234567E96< >1.234567e+102<
208 >%e< >0< >0.000000e+00<
209 >%e< >.1234567E-101< >1.234567e-102<
210 >%+e< >1234.875< >+1.234875e+03<
211 >%#e< >1234.875< >1.234875e+03<
212 >%e< >-1234.875< >-1.234875e+03<
213 >%+e< >-1234.875< >-1.234875e+03<
214 >%#e< >-1234.875< >-1.234875e+03<
215 >%.0e< >1234.875< >1e+03<
216 >%#.0e< >1234.875< >1.e+03<
217 >%.*e< >[0, 1234.875]< >1e+03<
218 >%.1e< >1234.875< >1.2e+03<
219 >%-12.4e< >1234.875< >1.2349e+03 <
220 >%12.4e< >1234.875< > 1.2349e+03<
221 >%+-12.4e< >1234.875< >+1.2349e+03 <
222 >%+12.4e< >1234.875< > +1.2349e+03<
223 >%+-12.4e< >-1234.875< >-1.2349e+03 <
224 >%+12.4e< >-1234.875< > -1.2349e+03<
225 >%f< >1234.875< >1234.875000<
226 >%+f< >1234.875< >+1234.875000<
227 >%#f< >1234.875< >1234.875000<
228 >%f< >-1234.875< >-1234.875000<
229 >%+f< >-1234.875< >-1234.875000<
230 >%#f< >-1234.875< >-1234.875000<
231 >%6f< >1234.875< >1234.875000<
232 >%*f< >[6, 1234.875]< >1234.875000<
233 >%.0f< >1234.875< >1235<
234 >%.1f< >1234.875< >1234.9<
235 >%-8.1f< >1234.875< >1234.9 <
236 >%8.1f< >1234.875< > 1234.9<
237 >%+-8.1f< >1234.875< >+1234.9 <
238 >%+8.1f< >1234.875< > +1234.9<
239 >%+-8.1f< >-1234.875< >-1234.9 <
240 >%+8.1f< >-1234.875< > -1234.9<
241 >%*.*f< >[5, 2, 12.3456]< >12.35<
244 >%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
246 >%.0f< >0.6< >1< >Known to fail with sfio and (irix|nonstop-ux|powerux)<
247 >%.0f< >-0.6< >-1< >Known to fail with sfio and (irix|nonstop-ux|powerux)<
250 >%g< >12345.6789< >12345.7<
251 >%+g< >12345.6789< >+12345.7<
252 >%#g< >12345.6789< >12345.7<
253 >%.0g< >12345.6789< >1e+04<
254 >%#.0g< >12345.6789< >1.e+04<
255 >%.2g< >12345.6789< >1.2e+04<
256 >%.*g< >[2, 12345.6789]< >1.2e+04<
257 >%.9g< >12345.6789< >12345.6789<
258 >%12.9g< >12345.6789< > 12345.6789<
259 >%012.9g< >12345.6789< >0012345.6789<
260 >%-12.9g< >12345.6789< >12345.6789 <
261 >%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
262 >%-012.9g< >12345.6789< >12345.6789 <
263 >%g< >-12345.6789< >-12345.7<
264 >%+g< >-12345.6789< >-12345.7<
265 >%g< >1234567.89< >1.23457e+06<
266 >%+g< >1234567.89< >+1.23457e+06<
267 >%#g< >1234567.89< >1.23457e+06<
268 >%g< >-1234567.89< >-1.23457e+06<
269 >%+g< >-1234567.89< >-1.23457e+06<
270 >%#g< >-1234567.89< >-1.23457e+06<
271 >%g< >0.00012345< >0.00012345<
272 >%g< >0.000012345< >1.2345e-05<
273 >%g< >1234567E96< >1.23457e+102<
274 >%g< >.1234567E-101< >1.23457e-102<
276 >%13g< >1234567.89< > 1.23457e+06<
277 >%+13g< >1234567.89< > +1.23457e+06<
278 >%013g< >1234567.89< >001.23457e+06<
279 >%-13g< >1234567.89< >1.23457e+06 <
280 >%h< >''< >%h INVALID<
281 >%i< >123456.789< >123456< >Synonym for %d<
282 >%j< >''< >%j INVALID<
283 >%k< >''< >%k INVALID<
284 >%l< >''< >%l INVALID<
285 >%m< >''< >%m INVALID<
286 >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
287 >%o< >2**32-1< >37777777777<
288 >%+o< >2**32-1< >37777777777<
289 >%#o< >2**32-1< >037777777777<
290 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
291 >%#p< >''< >%#p INVALID<
292 >%q< >''< >%q INVALID<
293 >%r< >''< >%r INVALID<
294 >%s< >'string'< >string<
295 >%10s< >'string'< > string<
296 >%+10s< >'string'< > string<
297 >%#10s< >'string'< > string<
298 >%010s< >'string'< >0000string<
299 >%0*s< >[10, 'string']< >0000string<
300 >%-10s< >'string'< >string <
301 >%3s< >'string'< >string<
302 >%.3s< >'string'< >str<
303 >%.*s< >[3, 'string']< >str<
304 >%t< >''< >%t INVALID<
305 >%u< >2**32-1< >4294967295<
306 >%+u< >2**32-1< >4294967295<
307 >%#u< >2**32-1< >4294967295<
308 >%12u< >2**32-1< > 4294967295<
309 >%012u< >2**32-1< >004294967295<
310 >%-12u< >2**32-1< >4294967295 <
311 >%-012u< >2**32-1< >4294967295 <
312 >%v< >''< >%v INVALID<
313 >%w< >''< >%w INVALID<
314 >%x< >2**32-1< >ffffffff<
315 >%+x< >2**32-1< >ffffffff<
316 >%#x< >2**32-1< >0xffffffff<
317 >%10x< >2**32-1< > ffffffff<
318 >%010x< >2**32-1< >00ffffffff<
319 >%-10x< >2**32-1< >ffffffff <
320 >%-010x< >2**32-1< >ffffffff <
321 >%0-10x< >2**32-1< >ffffffff <
322 >%0*x< >[-10, ,2**32-1]< >ffffffff <
323 >%y< >''< >%y INVALID<
324 >%z< >''< >%z INVALID<
325 >%2$d %1$d< >[12, 34]< >34 12<
326 >%*2$d< >[12, 3]< > 12<
327 >%2$d %d< >[12, 34]< >34 12<
328 >%2$d %d %d< >[12, 34]< >34 12 34<
329 >%3$d %d %d< >[12, 34, 56]< >56 12 34<
330 >%2$*3$d %d< >[12, 34, 3]< > 34 12<
331 >%*3$2$d %d< >[12, 34, 3]< >%*3$2$d 34 INVALID<
332 >%2$d< >12< >0 UNINIT<
333 >%0$d< >12< >%0$d INVALID<
334 >%1$$d< >12< >%1$$d INVALID<
335 >%1$1$d< >12< >%1$1$d INVALID<
336 >%*2$*2$d< >[12, 3]< >%*2$*2$d INVALID<
337 >%*2*2$d< >[12, 3]< >%*2*2$d INVALID<