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).
14 # %Config is needed to obtain archname for VAX (since @INC is now insufficient)
19 my ($i, $template, $data, $result, $comment, $w, $x, $evalData, $n, $p);
23 push @tests, [split(/<\s*>/, $_, 4)];
26 print '1..', scalar @tests, "\n";
28 $SIG{__WARN__} = sub {
29 if ($_[0] =~ /^Invalid conversion/) {
31 } elsif ($_[0] =~ /^Use of uninitialized value/) {
39 # The redundant $^O check might help non VMS platforms avoid %Config load
41 defined($Config{'archname'}) && $Config{'archname'} eq 'VMS_VAX') {
45 for ($i = 1; @tests; $i++) {
46 ($template, $data, $result, $comment) = @{shift @tests};
47 if ($^O eq 'os390' || $^O eq 's390') { # non-IEEE (s390 is UTS)
48 $data =~ s/([eE])96$/${1}63/; # smaller exponents
49 $result =~ s/([eE]\+)102$/${1}69/; # " "
50 $data =~ s/([eE])\-101$/${1}-56/; # larger exponents
51 $result =~ s/([eE])\-102$/${1}-57/; # " "
53 if ($Is_VMS_VAX) { # VAX DEC C 5.3 at least since there is no
54 # ccflags =~ /float=ieee/ on VAX.
55 # AXP is unaffected whether or not it's using ieee.
56 $data =~ s/([eE])96$/${1}26/; # smaller exponents
57 $result =~ s/([eE]\+)102$/${1}32/; # " "
58 $data =~ s/([eE])\-101$/${1}-24/; # larger exponents
59 $result =~ s/([eE])\-102$/${1}-25/; # " "
61 $evalData = eval $data;
63 $x = sprintf(">$template<",
64 defined @$evalData ? @$evalData : $evalData);
65 substr($x, -1, 0) = $w if $w;
66 # $x may have 3 exponent digits, not 2
68 if ($y =~ s/([Ee][-+])0(\d)/$1$2/) {
69 # if result is left-adjusted, append extra space
70 if ($template =~ /%\+?\-/ and $result =~ / $/) {
73 # if result is zero-filled, add extra zero
74 elsif ($template =~ /%\+?0/ and $result =~ /^0/) {
77 # if result is right-adjusted, prepend extra space
78 elsif ($result =~ /^ /) {
83 if ($x eq ">$result<") {
86 elsif ($y eq ">$result<") # Some C libraries always give
87 { # three-digit exponent
88 print("ok $i # >$result< $x three-digit exponent accepted\n");
90 elsif ($result =~ /[-+]\d{3}$/ &&
91 # Suppress tests with modulo of exponent >= 100 on platforms
92 # which can't handle such magnitudes (or where we can't tell).
93 ((!eval {require POSIX}) || # Costly: only do this if we must!
94 (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3))
96 print("ok $i # >$template< >$data< >$result<",
97 " Suppressed: exponent out of range?\n")
100 $y = ($x eq $y ? "" : " => $y");
101 print("not ok $i >$template< >$data< >$result< $x$y",
102 $comment ? " # $comment\n" : "\n");
106 # In each of the the following lines, there are three required fields:
107 # printf template, data to be formatted (as a Perl expression), and
108 # expected result of formatting. An optional fourth field can contain
109 # a comment. Each field is delimited by a starting '>' and a
110 # finishing '<'; any whitespace outside these start and end marks is
111 # not part of the field. If formatting requires more than one data
112 # item (for example, if variable field widths are used), the Perl data
113 # expression should return a reference to an array having the requisite
114 # number of elements. Even so, subterfuge is sometimes required: see
115 # tests for %n and %p.
117 # The following tests are not currently run, for the reasons stated:
123 >%.0f< >-0.1< >-0< >C library bug: no minus on VMS, HP-UX<
124 >%.0f< >1.5< >2< >Standard vague: no rounding rules<
125 >%.0f< >2.5< >2< >Standard vague: no rounding rules<
126 >%G< >1234567e96< >1.23457E+102< >exponent too big for OS/390<
127 >%G< >.1234567e-101< >1.23457E-102< >exponent too small for OS/390<
128 >%e< >1234567E96< >1.234567e+102< >exponent too big for OS/390<
129 >%e< >.1234567E-101< >1.234567e-102< >exponent too small for OS/390<
130 >%g< >.1234567E-101< >1.23457e-102< >exponent too small for OS/390<
131 >%g< >1234567E96< >1.23457e+102< >exponent too big for OS/390<
137 # template data result
139 >%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
140 >%6 .6s< >''< >%6 .6s INVALID<
141 >%6.6 s< >''< >%6.6 s INVALID<
142 >%A< >''< >%A INVALID<
143 >%B< >''< >%B INVALID<
144 >%C< >''< >%C INVALID<
145 >%D< >0x7fffffff< >2147483647< >Synonym for %ld<
146 >%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
147 >%F< >123456.789< >123456.789000< >Synonym for %f<
148 >%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
149 >%G< >1234567e96< >1.23457E+102<
150 >%G< >.1234567e-101< >1.23457E-102<
151 >%G< >12345.6789< >12345.7<
152 >%H< >''< >%H INVALID<
153 >%I< >''< >%I INVALID<
154 >%J< >''< >%J INVALID<
155 >%K< >''< >%K INVALID<
156 >%L< >''< >%L INVALID<
157 >%M< >''< >%M INVALID<
158 >%N< >''< >%N INVALID<
159 >%O< >2**32-1< >37777777777< >Synonym for %lo<
160 >%P< >''< >%P INVALID<
161 >%Q< >''< >%Q INVALID<
162 >%R< >''< >%R INVALID<
163 >%S< >''< >%S INVALID<
164 >%T< >''< >%T INVALID<
165 >%U< >2**32-1< >4294967295< >Synonym for %lu<
166 >%V< >''< >%V INVALID<
167 >%W< >''< >%W INVALID<
168 >%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
169 >%#X< >2**32-1< >0XFFFFFFFF<
170 >%Y< >''< >%Y INVALID<
171 >%Z< >''< >%Z INVALID<
172 >%a< >''< >%a INVALID<
173 >%b< >2**32-1< >11111111111111111111111111111111<
174 >%+b< >2**32-1< >11111111111111111111111111111111<
175 >%#b< >2**32-1< >0b11111111111111111111111111111111<
176 >%34b< >2**32-1< > 11111111111111111111111111111111<
177 >%034b< >2**32-1< >0011111111111111111111111111111111<
178 >%-34b< >2**32-1< >11111111111111111111111111111111 <
179 >%-034b< >2**32-1< >11111111111111111111111111111111 <
181 >%10c< >ord('A')< > A<
182 >%#10c< >ord('A')< > A< ># modifier: no effect<
183 >%010c< >ord('A')< >000000000A<
184 >%10lc< >ord('A')< > A< >l modifier: no effect<
185 >%10hc< >ord('A')< > A< >h modifier: no effect<
186 >%10.5c< >ord('A')< > A< >precision: no effect<
187 >%-10c< >ord('A')< >A <
188 >%d< >123456.789< >123456<
189 >%d< >-123456.789< >-123456<
198 >%#3.2d< >1< > 01< ># modifier: no effect<
202 >%-03.2d< >1< >01 < >zero pad + left just.: no effect<
205 >%hd< >1< >1< >More extensive testing of<
206 >%ld< >1< >1< >length modifiers would be<
207 >%Vd< >1< >1< >platform-specific<
211 >%vd< >"\01\02\03"< >1.2.3<
212 >%v.3d< >"\01\02\03"< >001.002.003<
213 >%0v3d< >"\01\02\03"< >001.002.003<
214 >%-v3d< >"\01\02\03"< >1 .2 .3 <
215 >%+-v3d< >"\01\02\03"< >+1 .2 .3 <
216 >%v4.3d< >"\01\02\03"< > 001. 002. 003<
217 >%0v4.3d< >"\01\02\03"< >0001.0002.0003<
218 >%0*v2d< >['-', "\0\7\14"]< >00-07-12<
219 >%v.*d< >["\01\02\03", 3]< >001.002.003<
220 >%0v*d< >["\01\02\03", 3]< >001.002.003<
221 >%-v*d< >["\01\02\03", 3]< >1 .2 .3 <
222 >%+-v*d< >["\01\02\03", 3]< >+1 .2 .3 <
223 >%v*.*d< >["\01\02\03", 4, 3]< > 001. 002. 003<
224 >%0v*.*d< >["\01\02\03", 4, 3]< >0001.0002.0003<
225 >%0*v*d< >['-', "\0\7\13", 2]< >00-07-11<
226 >%e< >1234.875< >1.234875e+03<
227 >%e< >0.000012345< >1.234500e-05<
228 >%e< >1234567E96< >1.234567e+102<
229 >%e< >0< >0.000000e+00<
230 >%e< >.1234567E-101< >1.234567e-102<
231 >%+e< >1234.875< >+1.234875e+03<
232 >%#e< >1234.875< >1.234875e+03<
233 >%e< >-1234.875< >-1.234875e+03<
234 >%+e< >-1234.875< >-1.234875e+03<
235 >%#e< >-1234.875< >-1.234875e+03<
236 >%.0e< >1234.875< >1e+03<
237 >%#.0e< >1234.875< >1.e+03<
238 >%.*e< >[0, 1234.875]< >1e+03<
239 >%.1e< >1234.875< >1.2e+03<
240 >%-12.4e< >1234.875< >1.2349e+03 <
241 >%12.4e< >1234.875< > 1.2349e+03<
242 >%+-12.4e< >1234.875< >+1.2349e+03 <
243 >%+12.4e< >1234.875< > +1.2349e+03<
244 >%+-12.4e< >-1234.875< >-1.2349e+03 <
245 >%+12.4e< >-1234.875< > -1.2349e+03<
246 >%f< >1234.875< >1234.875000<
247 >%+f< >1234.875< >+1234.875000<
248 >%#f< >1234.875< >1234.875000<
249 >%f< >-1234.875< >-1234.875000<
250 >%+f< >-1234.875< >-1234.875000<
251 >%#f< >-1234.875< >-1234.875000<
252 >%6f< >1234.875< >1234.875000<
253 >%*f< >[6, 1234.875]< >1234.875000<
254 >%.0f< >1234.875< >1235<
255 >%.1f< >1234.875< >1234.9<
256 >%-8.1f< >1234.875< >1234.9 <
257 >%8.1f< >1234.875< > 1234.9<
258 >%+-8.1f< >1234.875< >+1234.9 <
259 >%+8.1f< >1234.875< > +1234.9<
260 >%+-8.1f< >-1234.875< >-1234.9 <
261 >%+8.1f< >-1234.875< > -1234.9<
262 >%*.*f< >[5, 2, 12.3456]< >12.35<
265 >%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
267 >%.0f< >0.6< >1< >Known to fail with sfio and (irix|nonstop-ux|powerux)<
268 >%.0f< >-0.6< >-1< >Known to fail with sfio and (irix|nonstop-ux|powerux)<
271 >%g< >12345.6789< >12345.7<
272 >%+g< >12345.6789< >+12345.7<
273 >%#g< >12345.6789< >12345.7<
274 >%.0g< >12345.6789< >1e+04<
275 >%#.0g< >12345.6789< >1.e+04<
276 >%.2g< >12345.6789< >1.2e+04<
277 >%.*g< >[2, 12345.6789]< >1.2e+04<
278 >%.9g< >12345.6789< >12345.6789<
279 >%12.9g< >12345.6789< > 12345.6789<
280 >%012.9g< >12345.6789< >0012345.6789<
281 >%-12.9g< >12345.6789< >12345.6789 <
282 >%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
283 >%-012.9g< >12345.6789< >12345.6789 <
284 >%g< >-12345.6789< >-12345.7<
285 >%+g< >-12345.6789< >-12345.7<
286 >%g< >1234567.89< >1.23457e+06<
287 >%+g< >1234567.89< >+1.23457e+06<
288 >%#g< >1234567.89< >1.23457e+06<
289 >%g< >-1234567.89< >-1.23457e+06<
290 >%+g< >-1234567.89< >-1.23457e+06<
291 >%#g< >-1234567.89< >-1.23457e+06<
292 >%g< >0.00012345< >0.00012345<
293 >%g< >0.000012345< >1.2345e-05<
294 >%g< >1234567E96< >1.23457e+102<
295 >%g< >.1234567E-101< >1.23457e-102<
297 >%13g< >1234567.89< > 1.23457e+06<
298 >%+13g< >1234567.89< > +1.23457e+06<
299 >%013g< >1234567.89< >001.23457e+06<
300 >%-13g< >1234567.89< >1.23457e+06 <
301 >%h< >''< >%h INVALID<
302 >%i< >123456.789< >123456< >Synonym for %d<
303 >%j< >''< >%j INVALID<
304 >%k< >''< >%k INVALID<
305 >%l< >''< >%l INVALID<
306 >%m< >''< >%m INVALID<
307 >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
308 >%o< >2**32-1< >37777777777<
309 >%+o< >2**32-1< >37777777777<
310 >%#o< >2**32-1< >037777777777<
311 >%o< >642< >1202< >check smaller octals across platforms<
314 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
315 >%#p< >''< >%#p INVALID<
316 >%q< >''< >%q INVALID<
317 >%r< >''< >%r INVALID<
318 >%s< >'string'< >string<
319 >%10s< >'string'< > string<
320 >%+10s< >'string'< > string<
321 >%#10s< >'string'< > string<
322 >%010s< >'string'< >0000string<
323 >%0*s< >[10, 'string']< >0000string<
324 >%-10s< >'string'< >string <
325 >%3s< >'string'< >string<
326 >%.3s< >'string'< >str<
327 >%.*s< >[3, 'string']< >str<
328 >%t< >''< >%t INVALID<
329 >%u< >2**32-1< >4294967295<
330 >%+u< >2**32-1< >4294967295<
331 >%#u< >2**32-1< >4294967295<
332 >%12u< >2**32-1< > 4294967295<
333 >%012u< >2**32-1< >004294967295<
334 >%-12u< >2**32-1< >4294967295 <
335 >%-012u< >2**32-1< >4294967295 <
336 >%v< >''< >%v INVALID<
337 >%w< >''< >%w INVALID<
338 >%x< >2**32-1< >ffffffff<
339 >%+x< >2**32-1< >ffffffff<
340 >%#x< >2**32-1< >0xffffffff<
341 >%10x< >2**32-1< > ffffffff<
342 >%010x< >2**32-1< >00ffffffff<
343 >%-10x< >2**32-1< >ffffffff <
344 >%-010x< >2**32-1< >ffffffff <
345 >%0-10x< >2**32-1< >ffffffff <
346 >%0*x< >[-10, ,2**32-1]< >ffffffff <
347 >%y< >''< >%y INVALID<
348 >%z< >''< >%z INVALID<
349 >%2$d %1$d< >[12, 34]< >34 12<
350 >%*2$d< >[12, 3]< > 12<
351 >%2$d %d< >[12, 34]< >34 12<
352 >%2$d %d %d< >[12, 34]< >34 12 34<
353 >%3$d %d %d< >[12, 34, 56]< >56 12 34<
354 >%2$*3$d %d< >[12, 34, 3]< > 34 12<
355 >%*3$2$d %d< >[12, 34, 3]< >%*3$2$d 34 INVALID<
356 >%2$d< >12< >0 UNINIT<
357 >%0$d< >12< >%0$d INVALID<
358 >%1$$d< >12< >%1$$d INVALID<
359 >%1$1$d< >12< >%1$1$d INVALID<
360 >%*2$*2$d< >[12, 3]< >%*2$*2$d INVALID<
361 >%*2*2$d< >[12, 3]< >%*2*2$d INVALID<