Change 28404 broke the construct s/foo/<<BAR/e. So, try to be more
[p5sagit/p5-mst-13.2.git] / t / op / sprintf.t
index e96c683..3e038d4 100755 (executable)
@@ -4,24 +4,62 @@
 # doubles (if supported), of machine-specific short and long
 # integers, machine-specific floating point exceptions (infinity,
 # not-a-number ...), of the effects of locale, and of features
-# specific to multi-byte characters (under use utf8 and such).
+# specific to multi-byte characters (under the utf8 pragma and such).
 
 BEGIN {
     chdir 't' if -d 't';
-    unshift @INC, '../lib';
-}   
+    @INC = '../lib';
+}
 use warnings;
+use version;
+use Config;
+use strict;
+
+my @tests = ();
+my ($i, $template, $data, $result, $comment, $w, $x, $evalData, $n, $p);
+
+my $Is_VMS_VAX = 0;
+# We use HW_MODEL since ARCH_NAME was not in VMS V5.*
+if ($^O eq 'VMS') {
+    my $hw_model;
+    chomp($hw_model = `write sys\$output f\$getsyi("HW_MODEL")`);
+    $Is_VMS_VAX = $hw_model < 1024 ? 1 : 0;
+}
+
+# No %Config.
+my $Is_Ultrix_VAX = $^O eq 'ultrix' && `uname -m` =~ /^VAX$/;
 
 while (<DATA>) {
     s/^\s*>//; s/<\s*$//;
-    push @tests, [split(/<\s*>/, $_, 4)]; 
+    ($template, $data, $result, $comment) = split(/<\s*>/, $_, 4);
+    if ($^O eq 'os390' || $^O eq 's390') { # non-IEEE (s390 is UTS)
+        $data   =~ s/([eE])96$/${1}63/;      # smaller exponents
+        $result =~ s/([eE]\+)102$/${1}69/;   #  "       "
+        $data   =~ s/([eE])\-101$/${1}-56/;  # larger exponents
+        $result =~ s/([eE])\-102$/${1}-57/;  #  "       "
+    }
+    if ($Is_VMS_VAX || $Is_Ultrix_VAX) {
+       # VAX DEC C 5.3 at least since there is no
+       # ccflags =~ /float=ieee/ on VAX.
+       # AXP is unaffected whether or not it's using ieee.
+        $data   =~ s/([eE])96$/${1}26/;      # smaller exponents
+        $result =~ s/([eE]\+)102$/${1}32/;   #  "       "
+        $data   =~ s/([eE])\-101$/${1}-24/;  # larger exponents
+        $result =~ s/([eE])\-102$/${1}-25/;  #  "       "
+    }
+
+    $evalData = eval $data;
+    $data = ref $evalData ? $evalData : [$evalData];
+    push @tests, [$template, $data, $result, $comment];
 }
 
 print '1..', scalar @tests, "\n";
 
 $SIG{__WARN__} = sub {
     if ($_[0] =~ /^Invalid conversion/) {
-       $w = ' INVALID'
+       $w = ' INVALID';
+    } elsif ($_[0] =~ /^Use of uninitialized value/) {
+       $w = ' UNINIT';
     } else {
        warn @_;
     }
@@ -29,10 +67,8 @@ $SIG{__WARN__} = sub {
 
 for ($i = 1; @tests; $i++) {
     ($template, $data, $result, $comment) = @{shift @tests};
-    $evalData = eval $data;
     $w = undef;
-    $x = sprintf(">$template<",
-                 defined @$evalData ? @$evalData : $evalData);
+    $x = sprintf(">$template<", @$data);
     substr($x, -1, 0) = $w if $w;
     # $x may have 3 exponent digits, not 2
     my $y = $x;
@@ -51,13 +87,43 @@ for ($i = 1; @tests; $i++) {
        }
     }
 
+    my $skip = 0;
+    if ($comment =~ s/\s+skip:\s*(.*)//) {
+       my $os  = $1;
+       my $osv = exists $Config{osvers} ? $Config{osvers} : "0";
+       # >comment skip: all<
+       if ($os =~ /\ball\b/i) {
+           $skip = 1;
+       # >comment skip: VMS hpux:10.20<
+       } elsif ($os =~ /\b$^O(?::(\S+))?\b/i) {
+           my $vsn = defined $1 ? $1 : "0";
+           # Only compare on the the first pair of digits, as numeric
+           # compares don't like 2.6.10-3mdksmp or 2.6.8-24.10-default
+           s/^(\d+(\.\d+)?).*/$1/ for $osv, $vsn;
+           $skip = $vsn ? ($osv <= $vsn ? 1 : 0) : 1;
+       }
+       $skip and $comment =~ s/$/, failure expected on $^O $osv/;
+    }
+
     if ($x eq ">$result<") {
         print "ok $i\n";
     }
+    elsif ($skip) {
+       print "ok $i # skip $comment\n";
+    }
     elsif ($y eq ">$result<")  # Some C libraries always give
     {                          # three-digit exponent
-       print("ok $i >$result< $x # three-digit exponent accepted\n");
+               print("ok $i # >$result< $x three-digit exponent accepted\n");
     }
+       elsif ($result =~ /[-+]\d{3}$/ &&
+                  # Suppress tests with modulo of exponent >= 100 on platforms
+                  # which can't handle such magnitudes (or where we can't tell).
+                  ((!eval {require POSIX}) || # Costly: only do this if we must!
+                       (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3))
+       {
+               print("ok $i # >$template< >$data< >$result<",
+                         " Suppressed: exponent out of range?\n");
+       }
     else {
        $y = ($x eq $y ? "" : " => $y");
        print("not ok $i >$template< >$data< >$result< $x$y",
@@ -65,7 +131,7 @@ for ($i = 1; @tests; $i++) {
     }
 }
 
-# In each of the the following lines, there are three required fields:
+# In each of the following lines, there are three required fields:
 # printf template, data to be formatted (as a Perl expression), and
 # expected result of formatting.  An optional fourth field can contain
 # a comment.  Each field is delimited by a starting '>' and a
@@ -76,13 +142,22 @@ for ($i = 1; @tests; $i++) {
 # number of elements.  Even so, subterfuge is sometimes required: see
 # tests for %n and %p.
 #
+# Tests that are expected to fail on a certain OS can be marked as such
+# by trailing the comment with a skip: section. Skips are tags separated
+# bu space consisting of a $^O optionally trailed with :osvers. In the
+# latter case, all os-levels below that are expected to fail. A special
+# tag 'all' is allowed for todo tests that should fail on any system
+#
+# >%G<   >1234567e96<  >1.23457E+102<   >exponent too big skip: os390<
+# >%.0g< >-0.0<        >-0<             >No minus skip: MSWin32 VMS hpux:10.20<
+# >%d<   >4<           >1<              >4 != 1 skip: all<
+#
 # The following tests are not currently run, for the reasons stated:
 
 =pod
 
 =begin problematic
 
->%.0f<      >-0.1<        >-0<  >C library bug: no minus on VMS, HP-UX<
 >%.0f<      >1.5<         >2<   >Standard vague: no rounding rules<
 >%.0f<      >2.5<         >2<   >Standard vague: no rounding rules<
 
@@ -105,6 +180,8 @@ __END__
 >%G<        >1234567e96<  >1.23457E+102<
 >%G<        >.1234567e-101< >1.23457E-102<
 >%G<        >12345.6789<  >12345.7<
+>%G<        >1234567e96<  >1.23457E+102<       >exponent too big skip: os390<
+>%G<        >.1234567e-101< >1.23457E-102<     >exponent too small skip: os390<
 >%H<        >''<          >%H INVALID<
 >%I<        >''<          >%I INVALID<
 >%J<        >''<          >%J INVALID<
@@ -112,13 +189,13 @@ __END__
 >%L<        >''<          >%L INVALID<
 >%M<        >''<          >%M INVALID<
 >%N<        >''<          >%N INVALID<
->%O<        >2**32-1<     >37777777777<    >Synonum for %lo<
+>%O<        >2**32-1<     >37777777777<    >Synonym for %lo<
 >%P<        >''<          >%P INVALID<
 >%Q<        >''<          >%Q INVALID<
 >%R<        >''<          >%R INVALID<
 >%S<        >''<          >%S INVALID<
 >%T<        >''<          >%T INVALID<
->%U<        >2**32-1<     >4294967295<     >Synonum for %lu<
+>%U<        >2**32-1<     >4294967295<     >Synonym for %lu<
 >%V<        >''<          >%V INVALID<
 >%W<        >''<          >%W INVALID<
 >%X<        >2**32-1<     >FFFFFFFF<       >Like %x, but with u/c letters<
@@ -165,20 +242,30 @@ __END__
 >%+vd<      >chr(1)<      >+1<
 >%#vd<      >chr(1)<      >1<
 >%vd<       >"\01\02\03"< >1.2.3<
+>%vd<       >v1.2.3<      >1.2.3<
+>%vd<       >[version::qv("1.2.3")]< >1.2.3<
+>%vd<       >[version->new("1.2")]< >1.200<
+>%vd<       >[version->new("1.02")]< >1.20<
+>%vd<       >[version->new("1.002")]< >1.2<
+>%vd<       >[version->new("1048576.5")]< >1048576.500<
+>%vd<       >[version->new("50")]< >50.0<
 >%v.3d<     >"\01\02\03"< >001.002.003<
->%v03d<     >"\01\02\03"< >001.002.003<
->%v-3d<     >"\01\02\03"< >1  .2  .3  <
->%v+-3d<    >"\01\02\03"< >+1 .2  .3  <
+>%0v3d<     >"\01\02\03"< >001.002.003<
+>%v.3d<     >[version::qv("1.2.3")]< >001.002.003<
+>%-v3d<     >"\01\02\03"< >1  .2  .3  <
+>%+-v3d<    >"\01\02\03"< >+1 .2  .3  <
+>%+-v3d<    >[version::qv("1.2.3")]< >+1 .2  .3  <
 >%v4.3d<    >"\01\02\03"< > 001. 002. 003<
->%v04.3d<   >"\01\02\03"< >0001.0002.0003<
->%*v02d<    >['-', "\0\7\14"]< >00-07-12<
->%v.*d<     >[3, "\01\02\03"]< >001.002.003<
->%v0*d<     >[3, "\01\02\03"]< >001.002.003<
->%v-*d<     >[3, "\01\02\03"]< >1  .2  .3  <
->%v+-*d<    >[3, "\01\02\03"]< >+1 .2  .3  <
->%v*.*d<    >[4, 3, "\01\02\03"]< > 001. 002. 003<
->%v0*.*d<   >[4, 3, "\01\02\03"]< >0001.0002.0003<
->%*v0*d<    >['-', 2, "\0\7\13"]< >00-07-11<
+>%0v4.3d<   >"\01\02\03"< >0001.0002.0003<
+>%0*v2d<    >['-', "\0\7\14"]< >00-07-12<
+>%v.*d<     >["\01\02\03", 3]< >001.002.003<
+>%0v*d<     >["\01\02\03", 3]< >001.002.003<
+>%-v*d<     >["\01\02\03", 3]< >1  .2  .3  <
+>%+-v*d<    >["\01\02\03", 3]< >+1 .2  .3  <
+>%v*.*d<    >["\01\02\03", 4, 3]< > 001. 002. 003<
+>%0v*.*d<   >["\01\02\03", 4, 3]< >0001.0002.0003<
+>%0*v*d<    >['-', "\0\7\13", 2]< >00-07-11<
+>%0*v*d<    >['-', version::qv("0.7.11"), 2]< >00-07-11<
 >%e<        >1234.875<    >1.234875e+03<
 >%e<        >0.000012345< >1.234500e-05<
 >%e<        >1234567E96<  >1.234567e+102<
@@ -191,16 +278,18 @@ __END__
 >%#e<       >-1234.875<   >-1.234875e+03<
 >%.0e<      >1234.875<    >1e+03<
 >%#.0e<     >1234.875<    >1.e+03<
+>%.0e<      >1.875<       >2e+00<
+>%.0e<      >0.875<       >9e-01<
 >%.*e<      >[0, 1234.875]< >1e+03<
 >%.1e<      >1234.875<    >1.2e+03<
 >%-12.4e<   >1234.875<    >1.2349e+03  <
 >%12.4e<    >1234.875<    >  1.2349e+03<
 >%+-12.4e<  >1234.875<    >+1.2349e+03 <
 >%+12.4e<   >1234.875<    > +1.2349e+03<
->%+012.4e<  >1234.875<    >+01.2349e+03<
 >%+-12.4e<  >-1234.875<   >-1.2349e+03 <
 >%+12.4e<   >-1234.875<   > -1.2349e+03<
->%+012.4e<  >-1234.875<   >-01.2349e+03<
+>%e<        >1234567E96<  >1.234567e+102<      >exponent too big skip: os390<
+>%e<        >.1234567E-101< >1.234567e-102<    >exponent too small skip: os390<
 >%f<        >1234.875<    >1234.875000<
 >%+f<       >1234.875<    >+1234.875000<
 >%#f<       >1234.875<    >1234.875000<
@@ -209,28 +298,32 @@ __END__
 >%#f<       >-1234.875<   >-1234.875000<
 >%6f<       >1234.875<    >1234.875000<
 >%*f<       >[6, 1234.875]< >1234.875000<
+>%.0f<      >-0.1<        >-0<  >C library bug: no minus skip: VMS<
 >%.0f<      >1234.875<    >1235<
 >%.1f<      >1234.875<    >1234.9<
 >%-8.1f<    >1234.875<    >1234.9  <
 >%8.1f<     >1234.875<    >  1234.9<
 >%+-8.1f<   >1234.875<    >+1234.9 <
 >%+8.1f<    >1234.875<    > +1234.9<
->%+08.1f<   >1234.875<    >+01234.9<
 >%+-8.1f<   >-1234.875<   >-1234.9 <
 >%+8.1f<    >-1234.875<   > -1234.9<
->%+08.1f<   >-1234.875<   >-01234.9<
 >%*.*f<     >[5, 2, 12.3456]< >12.35<
 >%f<        >0<           >0.000000<
 >%.0f<      >0<           >0<
 >%.0f<      >2**38<       >274877906944<   >Should have exact int'l rep'n<
 >%.0f<      >0.1<         >0<
->%.0f<      >0.6<         >1<              >Known to fail with sfio<
->%.0f<      >-0.6<        >-1<             >Known to fail with sfio<
+>%.0f<      >0.6<         >1<              >Known to fail with sfio, (irix|nonstop-ux|powerux); -DHAS_LDBL_SPRINTF_BUG may fix<
+>%.0f<      >-0.6<        >-1<             >Known to fail with sfio, (irix|nonstop-ux|powerux); -DHAS_LDBL_SPRINTF_BUG may fix<
+>%.0f<      >1.6<         >2<
+>%.0f<      >-1.6<        >-2<
 >%.0f<      >1<           >1<
 >%#.0f<     >1<           >1.<
+>%.0lf<     >1<           >1<              >'l' should have no effect<
+>%.0hf<     >1<           >%.0hf INVALID<  >'h' should be rejected<
 >%g<        >12345.6789<  >12345.7<
 >%+g<       >12345.6789<  >+12345.7<
 >%#g<       >12345.6789<  >12345.7<
+>%.0g<      >-0.0<       >-0<             >C99 standard mandates minus sign but C89 does not skip: MSWin32 VMS hpux:10.20 openbsd netbsd:1.5 irix<
 >%.0g<      >12345.6789<  >1e+04<
 >%#.0g<     >12345.6789<  >1.e+04<
 >%.2g<      >12345.6789<  >1.2e+04<
@@ -256,9 +349,10 @@ __END__
 >%g<        >0<           >0<
 >%13g<      >1234567.89<  >  1.23457e+06<
 >%+13g<     >1234567.89<  > +1.23457e+06<
->%+013g<    >1234567.89<  >+01.23457e+06<
->%013g<      >1234567.89< >001.23457e+06<
->%-13g<      >1234567.89< >1.23457e+06  <
+>%013g<     >1234567.89<  >001.23457e+06<
+>%-13g<     >1234567.89<  >1.23457e+06  <
+>%g<        >.1234567E-101< >1.23457e-102<     >exponent too small skip: os390<
+>%g<        >1234567E96<  >1.23457e+102<       >exponent too big skip: os390<
 >%h<        >''<          >%h INVALID<
 >%i<        >123456.789<  >123456<         >Synonym for %d<
 >%j<        >''<          >%j INVALID<
@@ -269,7 +363,11 @@ __END__
 >%o<        >2**32-1<     >37777777777<
 >%+o<       >2**32-1<     >37777777777<
 >%#o<       >2**32-1<     >037777777777<
+>%o<        >642<         >1202<          >check smaller octals across platforms<
+>%+o<       >642<         >1202<
+>%#o<       >642<         >01202<
 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
+>%d< >$p=sprintf('%-8p',$p);$p=~/^[0-9a-f]+\s*$/< >1< >Coarse hack: hex from %p?<
 >%#p<       >''<          >%#p INVALID<
 >%q<        >''<          >%q INVALID<
 >%r<        >''<          >%r INVALID<
@@ -302,5 +400,60 @@ __END__
 >%-010x<    >2**32-1<     >ffffffff  <
 >%0-10x<    >2**32-1<     >ffffffff  <
 >%0*x<      >[-10, ,2**32-1]< >ffffffff  <
+>%vx<       >[version::qv("1.2.3")]< >1.2.3<
+>%vx<       >[version::qv("1.20.300")]< >1.14.12c<
 >%y<        >''<          >%y INVALID<
 >%z<        >''<          >%z INVALID<
+>%2$d %1$d<    >[12, 34]<      >34 12<
+>%*2$d<                >[12, 3]<       > 12<
+>%2$d %d<      >[12, 34]<      >34 12<
+>%2$d %d %d<   >[12, 34]<      >34 12 34<
+>%3$d %d %d<   >[12, 34, 56]<  >56 12 34<
+>%2$*3$d %d<   >[12, 34, 3]<   > 34 12<
+>%*3$2$d %d<   >[12, 34, 3]<   >%*3$2$d 12 INVALID<
+>%2$d<         >12<    >0 UNINIT<
+>%0$d<         >12<    >%0$d INVALID<
+>%1$$d<                >12<    >%1$$d INVALID<
+>%1$1$d<       >12<    >%1$1$d INVALID<
+>%*2$*2$d<     >[12, 3]<       >%*2$*2$d INVALID<
+>%*2*2$d<      >[12, 3]<       >%*2*2$d INVALID<
+>%*2$1d<       >[12, 3]<       >%*2$1d INVALID<
+>%0v2.2d<      >''<    ><
+>%vc,%d<       >[63, 64, 65]<  >%vc,63 INVALID<
+>%v%,%d<       >[63, 64, 65]<  >%v%,63 INVALID<
+>%vd,%d<       >[1, 2, 3]<     >49,2<
+>%vf,%d<       >[1, 2, 3]<     >%vf,1 INVALID<
+>%vF,%d<       >[1, 2, 3]<     >%vF,1 INVALID<
+>%ve,%d<       >[1, 2, 3]<     >%ve,1 INVALID<
+>%vE,%d<       >[1, 2, 3]<     >%vE,1 INVALID<
+>%vg,%d<       >[1, 2, 3]<     >%vg,1 INVALID<
+>%vG,%d<       >[1, 2, 3]<     >%vG,1 INVALID<
+>%vp<  >''<    >%vp INVALID<
+>%vn<  >''<    >%vn INVALID<
+>%vs,%d<       >[1, 2, 3]<     >%vs,1 INVALID<
+>%v_<  >''<    >%v_ INVALID<
+>%v#x< >''<    >%v#x INVALID<
+>%v02x<        >"foo\012"<     >66.6f.6f.0a<
+>%#v.8b<       >"a\000b"<      >0b01100001.00000000.0b01100010<
+>%#v.4o<       >"a\000b"<      >0141.0000.0142<
+>%#v.3i<       >"a\000b"<      >097.000.098<
+>%#v.2x<       >"a\000b"<      >0x61.00.0x62<
+>%#*v.8b<      >["][", "a\000b"]<      >0b01100001][00000000][0b01100010<
+>%#*v.4o<      >["][", "a\000b"]<      >0141][0000][0142<
+>%#*v.3i<      >["][", "a\000b"]<      >097][000][098<
+>%#*v.2x<      >["][", "a\000b"]<      >0x61][00][0x62<
+>%#v.8b<       >"a\x{1e01}\000b\x{1e03}"<      >0b01100001.0b1111000000001.00000000.0b01100010.0b1111000000011<
+>%#v.4o<       >"a\x{1e01}\000b\x{1e03}"<      >0141.017001.0000.0142.017003<
+>%#v.3i<       >"a\x{1e01}\000b\x{1e03}"<      >097.7681.000.098.7683<
+>%#v.2x<       >"a\x{1e01}\000b\x{1e03}"<      >0x61.0x1e01.00.0x62.0x1e03<
+>%V-%s<                >["Hello"]<     >%V-Hello INVALID<
+>%K %d %d<     >[13, 29]<      >%K 13 29 INVALID<
+>%*.*K %d<     >[13, 29, 76]<  >%*.*K 13 INVALID<
+>%4$K %d<      >[45, 67]<      >%4$K 45 INVALID<
+>%d %K %d<     >[23, 45]<      >23 %K 45 INVALID<
+>%*v*999\$d %d %d<     >[11, 22, 33]<  >%*v*999\$d 11 22 INVALID<
+>%#b<          >0<     >0<
+>%#o<          >0<     >0<
+>%#x<          >0<     >0<
+>%2147483647$v2d<      >''<    ><
+>%*2147483647$v2d<     >''<    > UNINIT<