sprintf now has all but two tests enabled.
[p5sagit/p5-mst-13.2.git] / t / op / sprintf.t
1 #!./perl
2
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 the utf8 pragma and such).
8
9 BEGIN {
10     chdir 't' if -d 't';
11     @INC = '../lib';
12 }
13 use warnings;
14 use Config;
15
16 # strictness
17 my @tests = ();
18 my ($i, $template, $data, $result, $comment, $w, $x, $evalData, $n, $p);
19
20 while (<DATA>) {
21     s/^\s*>//; s/<\s*$//;
22     push @tests, [split(/<\s*>/, $_, 4)];
23 }
24
25 print '1..', scalar @tests, "\n";
26
27 $SIG{__WARN__} = sub {
28     if ($_[0] =~ /^Invalid conversion/) {
29         $w = ' INVALID';
30     } elsif ($_[0] =~ /^Use of uninitialized value/) {
31         $w = ' UNINIT';
32     } else {
33         warn @_;
34     }
35 };
36
37 my $Is_VMS_VAX = 0;
38 # We use HW_MODEL since ARCH_NAME was not in VMS V5.*
39 if ($^O eq 'VMS') {
40     my $hw_model;
41     chomp($hw_model = `write sys\$output f\$getsyi("HW_MODEL")`);
42     $Is_VMS_VAX = $hw_model < 1024 ? 1 : 0;
43 }
44
45 # No %Config.
46 my $Is_Ultrix_VAX = $^O eq 'ultrix' && `uname -m` =~ /^VAX$/;
47
48 for ($i = 1; @tests; $i++) {
49     ($template, $data, $result, $comment) = @{shift @tests};
50     if ($^O eq 'os390' || $^O eq 's390') { # non-IEEE (s390 is UTS)
51         $data   =~ s/([eE])96$/${1}63/;      # smaller exponents
52         $result =~ s/([eE]\+)102$/${1}69/;   #  "       "
53         $data   =~ s/([eE])\-101$/${1}-56/;  # larger exponents
54         $result =~ s/([eE])\-102$/${1}-57/;  #  "       "
55     }
56     if ($Is_VMS_VAX || $Is_Ultrix_VAX) {
57         # VAX DEC C 5.3 at least since there is no
58         # ccflags =~ /float=ieee/ on VAX.
59         # AXP is unaffected whether or not it's using ieee.
60         $data   =~ s/([eE])96$/${1}26/;      # smaller exponents
61         $result =~ s/([eE]\+)102$/${1}32/;   #  "       "
62         $data   =~ s/([eE])\-101$/${1}-24/;  # larger exponents
63         $result =~ s/([eE])\-102$/${1}-25/;  #  "       "
64     }
65     $evalData = eval $data;
66     $w = undef;
67     $x = sprintf(">$template<",
68                  defined @$evalData ? @$evalData : $evalData);
69     substr($x, -1, 0) = $w if $w;
70     # $x may have 3 exponent digits, not 2
71     my $y = $x;
72     if ($y =~ s/([Ee][-+])0(\d)/$1$2/) {
73         # if result is left-adjusted, append extra space
74         if ($template =~ /%\+?\-/ and $result =~ / $/) {
75             $y =~ s/<$/ </;
76         }
77         # if result is zero-filled, add extra zero
78         elsif ($template =~ /%\+?0/ and $result =~ /^0/) {
79             $y =~ s/^>0/>00/;
80         }
81         # if result is right-adjusted, prepend extra space
82         elsif ($result =~ /^ /) {
83             $y =~ s/^>/> /;
84         }
85     }
86
87     my $skip = 0;
88     if ($comment =~ s/\s+skip:\s*(.*)//) {
89         my $os  = $1;
90         my $osv = exists $Config{osvers} ? $Config{osvers} : "0";
91         # >comment skip: all<
92         if ($os =~ /\ball\b/i) {
93             $skip = 1;
94         # >comment skip: VMS hpux:10.20<
95         } elsif ($os =~ /\b$^O(?::(\S+))?\b/i) {
96             my $vsn = defined $1 ? $1 : "0";
97             # Only compare on the the first pair of digits, as numeric
98             # compares don't like 2.6.10-3mdksmp or 2.6.8-24.10-default
99             s/^(\d+(\.\d+)?).*/$1/ for $osv, $vsn;
100             $vsn && $osv <= $vsn and $skip = 1;
101         }
102         $skip and $comment =~ s/$/, failure expected on $^O $osv/;
103     }
104
105     if ($x eq ">$result<") {
106         print "ok $i\n";
107     }
108     elsif ($skip) {
109         print "ok $i # skip $comment\n";
110     }
111     elsif ($y eq ">$result<")   # Some C libraries always give
112     {                           # three-digit exponent
113                 print("ok $i # >$result< $x three-digit exponent accepted\n");
114     }
115         elsif ($result =~ /[-+]\d{3}$/ &&
116                    # Suppress tests with modulo of exponent >= 100 on platforms
117                    # which can't handle such magnitudes (or where we can't tell).
118                    ((!eval {require POSIX}) || # Costly: only do this if we must!
119                         (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3))
120         {
121                 print("ok $i # >$template< >$data< >$result<",
122                           " Suppressed: exponent out of range?\n");
123         }
124     else {
125         $y = ($x eq $y ? "" : " => $y");
126         print("not ok $i >$template< >$data< >$result< $x$y",
127             $comment ? " # $comment\n" : "\n");
128     }
129 }
130
131 # In each of the following lines, there are three required fields:
132 # printf template, data to be formatted (as a Perl expression), and
133 # expected result of formatting.  An optional fourth field can contain
134 # a comment.  Each field is delimited by a starting '>' and a
135 # finishing '<'; any whitespace outside these start and end marks is
136 # not part of the field.  If formatting requires more than one data
137 # item (for example, if variable field widths are used), the Perl data
138 # expression should return a reference to an array having the requisite
139 # number of elements.  Even so, subterfuge is sometimes required: see
140 # tests for %n and %p.
141 #
142 # Tests that are expected to fail on a certain OS can be marked as such
143 # by trailing the comment with a skip: section. Skips are tags separated
144 # bu space consisting of a $^O optionally trailed with :osvers. In the
145 # latter case, all os-levels below that are expected to fail. A special
146 # tag 'all' is allowed for todo tests that should fail on any system
147 #
148 # >%G<   >1234567e96<  >1.23457E+102<   >exponent too big skip: os390<
149 # >%.0g< >-0.0<        >-0<             >No minus skip: VMS hpux:10.20<
150 # >%d<   >4<           >1<              >4 != 1 skip: all<
151 #
152 # The following tests are not currently run, for the reasons stated:
153
154 =pod
155
156 =begin problematic
157
158 >%.0f<      >1.5<         >2<   >Standard vague: no rounding rules<
159 >%.0f<      >2.5<         >2<   >Standard vague: no rounding rules<
160
161 =end problematic
162
163 =cut
164
165 # template    data          result
166 __END__
167 >%6. 6s<    >''<          >%6. 6s INVALID< >(See use of $w in code above)<
168 >%6 .6s<    >''<          >%6 .6s INVALID<
169 >%6.6 s<    >''<          >%6.6 s INVALID<
170 >%A<        >''<          >%A INVALID<
171 >%B<        >''<          >%B INVALID<
172 >%C<        >''<          >%C INVALID<
173 >%D<        >0x7fffffff<  >2147483647<     >Synonym for %ld<
174 >%E<        >123456.789<  >1.234568E+05<   >Like %e, but using upper-case "E"<
175 >%F<        >123456.789<  >123456.789000<  >Synonym for %f<
176 >%G<        >1234567.89<  >1.23457E+06<    >Like %g, but using upper-case "E"<
177 >%G<        >1234567e96<  >1.23457E+102<
178 >%G<        >.1234567e-101< >1.23457E-102<
179 >%G<        >12345.6789<  >12345.7<
180 >%G<        >1234567e96<  >1.23457E+102<        >exponent too big skip: os390<
181 >%G<        >.1234567e-101< >1.23457E-102<      >exponent too small skip: os390<
182 >%H<        >''<          >%H INVALID<
183 >%I<        >''<          >%I INVALID<
184 >%J<        >''<          >%J INVALID<
185 >%K<        >''<          >%K INVALID<
186 >%L<        >''<          >%L INVALID<
187 >%M<        >''<          >%M INVALID<
188 >%N<        >''<          >%N INVALID<
189 >%O<        >2**32-1<     >37777777777<    >Synonym for %lo<
190 >%P<        >''<          >%P INVALID<
191 >%Q<        >''<          >%Q INVALID<
192 >%R<        >''<          >%R INVALID<
193 >%S<        >''<          >%S INVALID<
194 >%T<        >''<          >%T INVALID<
195 >%U<        >2**32-1<     >4294967295<     >Synonym for %lu<
196 >%V<        >''<          >%V INVALID<
197 >%W<        >''<          >%W INVALID<
198 >%X<        >2**32-1<     >FFFFFFFF<       >Like %x, but with u/c letters<
199 >%#X<       >2**32-1<     >0XFFFFFFFF<
200 >%Y<        >''<          >%Y INVALID<
201 >%Z<        >''<          >%Z INVALID<
202 >%a<        >''<          >%a INVALID<
203 >%b<        >2**32-1<     >11111111111111111111111111111111<
204 >%+b<       >2**32-1<     >11111111111111111111111111111111<
205 >%#b<       >2**32-1<     >0b11111111111111111111111111111111<
206 >%34b<      >2**32-1<     >  11111111111111111111111111111111<
207 >%034b<     >2**32-1<     >0011111111111111111111111111111111<
208 >%-34b<     >2**32-1<     >11111111111111111111111111111111  <
209 >%-034b<    >2**32-1<     >11111111111111111111111111111111  <
210 >%c<        >ord('A')<    >A<
211 >%10c<      >ord('A')<    >         A<
212 >%#10c<     >ord('A')<    >         A<     ># modifier: no effect<
213 >%010c<     >ord('A')<    >000000000A<
214 >%10lc<     >ord('A')<    >         A<     >l modifier: no effect<
215 >%10hc<     >ord('A')<    >         A<     >h modifier: no effect<
216 >%10.5c<    >ord('A')<    >         A<     >precision: no effect<
217 >%-10c<     >ord('A')<    >A         <
218 >%d<        >123456.789<  >123456<
219 >%d<        >-123456.789< >-123456<
220 >%d<        >0<           >0<
221 >%+d<       >0<           >+0<
222 >%0d<       >0<           >0<
223 >%.0d<      >0<           ><
224 >%+.0d<     >0<           >+<
225 >%.0d<      >1<           >1<
226 >%d<        >1<           >1<
227 >%+d<       >1<           >+1<
228 >%#3.2d<    >1<           > 01<            ># modifier: no effect<
229 >%3.2d<     >1<           > 01<
230 >%03.2d<    >1<           >001<
231 >%-3.2d<    >1<           >01 <
232 >%-03.2d<   >1<           >01 <            >zero pad + left just.: no effect<
233 >%d<        >-1<          >-1<
234 >%+d<       >-1<          >-1<
235 >%hd<       >1<           >1<              >More extensive testing of<
236 >%ld<       >1<           >1<              >length modifiers would be<
237 >%Vd<       >1<           >1<              >platform-specific<
238 >%vd<       >chr(1)<      >1<
239 >%+vd<      >chr(1)<      >+1<
240 >%#vd<      >chr(1)<      >1<
241 >%vd<       >"\01\02\03"< >1.2.3<
242 >%v.3d<     >"\01\02\03"< >001.002.003<
243 >%0v3d<     >"\01\02\03"< >001.002.003<
244 >%-v3d<     >"\01\02\03"< >1  .2  .3  <
245 >%+-v3d<    >"\01\02\03"< >+1 .2  .3  <
246 >%v4.3d<    >"\01\02\03"< > 001. 002. 003<
247 >%0v4.3d<   >"\01\02\03"< >0001.0002.0003<
248 >%0*v2d<    >['-', "\0\7\14"]< >00-07-12<
249 >%v.*d<     >["\01\02\03", 3]< >001.002.003<
250 >%0v*d<     >["\01\02\03", 3]< >001.002.003<
251 >%-v*d<     >["\01\02\03", 3]< >1  .2  .3  <
252 >%+-v*d<    >["\01\02\03", 3]< >+1 .2  .3  <
253 >%v*.*d<    >["\01\02\03", 4, 3]< > 001. 002. 003<
254 >%0v*.*d<   >["\01\02\03", 4, 3]< >0001.0002.0003<
255 >%0*v*d<    >['-', "\0\7\13", 2]< >00-07-11<
256 >%e<        >1234.875<    >1.234875e+03<
257 >%e<        >0.000012345< >1.234500e-05<
258 >%e<        >1234567E96<  >1.234567e+102<
259 >%e<        >0<           >0.000000e+00<
260 >%e<        >.1234567E-101< >1.234567e-102<
261 >%+e<       >1234.875<    >+1.234875e+03<
262 >%#e<       >1234.875<    >1.234875e+03<
263 >%e<        >-1234.875<   >-1.234875e+03<
264 >%+e<       >-1234.875<   >-1.234875e+03<
265 >%#e<       >-1234.875<   >-1.234875e+03<
266 >%.0e<      >1234.875<    >1e+03<
267 >%#.0e<     >1234.875<    >1.e+03<
268 >%.0e<      >1.875<       >2e+00<
269 >%.0e<      >0.875<       >9e-01<
270 >%.*e<      >[0, 1234.875]< >1e+03<
271 >%.1e<      >1234.875<    >1.2e+03<
272 >%-12.4e<   >1234.875<    >1.2349e+03  <
273 >%12.4e<    >1234.875<    >  1.2349e+03<
274 >%+-12.4e<  >1234.875<    >+1.2349e+03 <
275 >%+12.4e<   >1234.875<    > +1.2349e+03<
276 >%+-12.4e<  >-1234.875<   >-1.2349e+03 <
277 >%+12.4e<   >-1234.875<   > -1.2349e+03<
278 >%e<        >1234567E96<  >1.234567e+102<       >exponent too big skip: os390<
279 >%e<        >.1234567E-101< >1.234567e-102<     >exponent too small skip: os390<
280 >%f<        >1234.875<    >1234.875000<
281 >%+f<       >1234.875<    >+1234.875000<
282 >%#f<       >1234.875<    >1234.875000<
283 >%f<        >-1234.875<   >-1234.875000<
284 >%+f<       >-1234.875<   >-1234.875000<
285 >%#f<       >-1234.875<   >-1234.875000<
286 >%6f<       >1234.875<    >1234.875000<
287 >%*f<       >[6, 1234.875]< >1234.875000<
288 >%.0f<      >-0.1<        >-0<  >C library bug: no minus skip: VMS<
289 >%.0f<      >1234.875<    >1235<
290 >%.1f<      >1234.875<    >1234.9<
291 >%-8.1f<    >1234.875<    >1234.9  <
292 >%8.1f<     >1234.875<    >  1234.9<
293 >%+-8.1f<   >1234.875<    >+1234.9 <
294 >%+8.1f<    >1234.875<    > +1234.9<
295 >%+-8.1f<   >-1234.875<   >-1234.9 <
296 >%+8.1f<    >-1234.875<   > -1234.9<
297 >%*.*f<     >[5, 2, 12.3456]< >12.35<
298 >%f<        >0<           >0.000000<
299 >%.0f<      >0<           >0<
300 >%.0f<      >2**38<       >274877906944<   >Should have exact int'l rep'n<
301 >%.0f<      >0.1<         >0<
302 >%.0f<      >0.6<         >1<              >Known to fail with sfio, (irix|nonstop-ux|powerux); -DHAS_LDBL_SPRINTF_BUG may fix<
303 >%.0f<      >-0.6<        >-1<             >Known to fail with sfio, (irix|nonstop-ux|powerux); -DHAS_LDBL_SPRINTF_BUG may fix<
304 >%.0f<      >1.6<         >2<
305 >%.0f<      >-1.6<        >-2<
306 >%.0f<      >1<           >1<
307 >%#.0f<     >1<           >1.<
308 >%.0lf<     >1<           >1<              >'l' should have no effect<
309 >%.0hf<     >1<           >%.0hf INVALID<  >'h' should be rejected<
310 >%g<        >12345.6789<  >12345.7<
311 >%+g<       >12345.6789<  >+12345.7<
312 >%#g<       >12345.6789<  >12345.7<
313 >%.0g<      >-0.0<        >-0<             >No minus skip: VMS hpux:10.20<
314 >%.0g<      >12345.6789<  >1e+04<
315 >%#.0g<     >12345.6789<  >1.e+04<
316 >%.2g<      >12345.6789<  >1.2e+04<
317 >%.*g<      >[2, 12345.6789]< >1.2e+04<
318 >%.9g<      >12345.6789<  >12345.6789<
319 >%12.9g<    >12345.6789<  >  12345.6789<
320 >%012.9g<   >12345.6789<  >0012345.6789<
321 >%-12.9g<   >12345.6789<  >12345.6789  <
322 >%*.*g<     >[-12, 9, 12345.6789]< >12345.6789  <
323 >%-012.9g<  >12345.6789<  >12345.6789  <
324 >%g<        >-12345.6789< >-12345.7<
325 >%+g<       >-12345.6789< >-12345.7<
326 >%g<        >1234567.89<  >1.23457e+06<
327 >%+g<       >1234567.89<  >+1.23457e+06<
328 >%#g<       >1234567.89<  >1.23457e+06<
329 >%g<        >-1234567.89< >-1.23457e+06<
330 >%+g<       >-1234567.89< >-1.23457e+06<
331 >%#g<       >-1234567.89< >-1.23457e+06<
332 >%g<        >0.00012345<  >0.00012345<
333 >%g<        >0.000012345< >1.2345e-05<
334 >%g<        >1234567E96<  >1.23457e+102<
335 >%g<        >.1234567E-101< >1.23457e-102<
336 >%g<        >0<           >0<
337 >%13g<      >1234567.89<  >  1.23457e+06<
338 >%+13g<     >1234567.89<  > +1.23457e+06<
339 >%013g<     >1234567.89<  >001.23457e+06<
340 >%-13g<     >1234567.89<  >1.23457e+06  <
341 >%g<        >.1234567E-101< >1.23457e-102<      >exponent too small skip: os390<
342 >%g<        >1234567E96<  >1.23457e+102<        >exponent too big skip: os390<
343 >%h<        >''<          >%h INVALID<
344 >%i<        >123456.789<  >123456<         >Synonym for %d<
345 >%j<        >''<          >%j INVALID<
346 >%k<        >''<          >%k INVALID<
347 >%l<        >''<          >%l INVALID<
348 >%m<        >''<          >%m INVALID<
349 >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
350 >%o<        >2**32-1<     >37777777777<
351 >%+o<       >2**32-1<     >37777777777<
352 >%#o<       >2**32-1<     >037777777777<
353 >%o<        >642<         >1202<          >check smaller octals across platforms<
354 >%+o<       >642<         >1202<
355 >%#o<       >642<         >01202<
356 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
357 >%d< >$p=sprintf('%-8p',$p);$p=~/^[0-9a-f]+\s*$/< >1< >Coarse hack: hex from %p?<
358 >%#p<       >''<          >%#p INVALID<
359 >%q<        >''<          >%q INVALID<
360 >%r<        >''<          >%r INVALID<
361 >%s<        >'string'<    >string<
362 >%10s<      >'string'<    >    string<
363 >%+10s<     >'string'<    >    string<
364 >%#10s<     >'string'<    >    string<
365 >%010s<     >'string'<    >0000string<
366 >%0*s<      >[10, 'string']< >0000string<
367 >%-10s<     >'string'<    >string    <
368 >%3s<       >'string'<    >string<
369 >%.3s<      >'string'<    >str<
370 >%.*s<      >[3, 'string']< >str<
371 >%t<        >''<          >%t INVALID<
372 >%u<        >2**32-1<     >4294967295<
373 >%+u<       >2**32-1<     >4294967295<
374 >%#u<       >2**32-1<     >4294967295<
375 >%12u<      >2**32-1<     >  4294967295<
376 >%012u<     >2**32-1<     >004294967295<
377 >%-12u<     >2**32-1<     >4294967295  <
378 >%-012u<    >2**32-1<     >4294967295  <
379 >%v<        >''<          >%v INVALID<
380 >%w<        >''<          >%w INVALID<
381 >%x<        >2**32-1<     >ffffffff<
382 >%+x<       >2**32-1<     >ffffffff<
383 >%#x<       >2**32-1<     >0xffffffff<
384 >%10x<      >2**32-1<     >  ffffffff<
385 >%010x<     >2**32-1<     >00ffffffff<
386 >%-10x<     >2**32-1<     >ffffffff  <
387 >%-010x<    >2**32-1<     >ffffffff  <
388 >%0-10x<    >2**32-1<     >ffffffff  <
389 >%0*x<      >[-10, ,2**32-1]< >ffffffff  <
390 >%y<        >''<          >%y INVALID<
391 >%z<        >''<          >%z INVALID<
392 >%2$d %1$d<     >[12, 34]<      >34 12<
393 >%*2$d<         >[12, 3]<       > 12<
394 >%2$d %d<       >[12, 34]<      >34 12<
395 >%2$d %d %d<    >[12, 34]<      >34 12 34<
396 >%3$d %d %d<    >[12, 34, 56]<  >56 12 34<
397 >%2$*3$d %d<    >[12, 34, 3]<   > 34 12<
398 >%*3$2$d %d<    >[12, 34, 3]<   >%*3$2$d 12 INVALID<
399 >%2$d<          >12<    >0 UNINIT<
400 >%0$d<          >12<    >%0$d INVALID<
401 >%1$$d<         >12<    >%1$$d INVALID<
402 >%1$1$d<        >12<    >%1$1$d INVALID<
403 >%*2$*2$d<      >[12, 3]<       >%*2$*2$d INVALID<
404 >%*2*2$d<       >[12, 3]<       >%*2*2$d INVALID<
405 >%*2$1d<        >[12, 3]<       >%*2$1d INVALID<
406 >%0v2.2d<       >''<    ><
407 >%vc,%d<        >[63, 64, 65]<  >?,64<
408 >%vd,%d<        >[1, 2, 3]<     >49,2<
409 >%vf,%d<        >[1, 2, 3]<     >1.000000,2<
410 >%vp<   >''<    >%vp INVALID<
411 >%vs,%d<        >[1, 2, 3]<     >1,2<
412 >%v_<   >''<    >%v_ INVALID<
413 >%v#x<  >''<    >%v#x INVALID<
414 >%v02x< >"foo\012"<     >66.6f.6f.0a<
415 >%V-%s<         >["Hello"]<     >%V-Hello INVALID<
416 >%K %d %d<      >[13, 29]<      >%K 13 29 INVALID<
417 >%*.*K %d<      >[13, 29, 76]<  >%*.*K 13 INVALID<
418 >%4$K %d<       >[45, 67]<      >%4$K 45 INVALID<
419 >%d %K %d<      >[23, 45]<      >23 %K 45 INVALID<
420 >%*v*999\$d %d %d<      >[11, 22, 33]<  >%*v*999\$d 11 22 INVALID<