Fix [RT#66098] -- stricter checking on SvIVX exposed a lack of SvIOK check
[p5sagit/p5-mst-13.2.git] / t / op / sprintf2.t
index 7544705..2e225c8 100644 (file)
@@ -6,7 +6,10 @@ BEGIN {
     require './test.pl';
 }   
 
-plan tests => 275;
+plan tests => 1319;
+
+use strict;
+use Config;
 
 is(
     sprintf("%.40g ",0.01),
@@ -18,13 +21,23 @@ is(
     sprintf("%.40f", 0.01)." ",
     q(the sprintf "%.<number>f" optimization)
 );
-{
-       chop(my $utf8_format = "%-3s\x{100}");
-       is(
-               sprintf($utf8_format, "\xe4"),
-               "\xe4  ",
-               q(width calculation under utf8 upgrade)
-       );
+
+# cases of $i > 1 are against [perl #39126]
+for my $i (1, 5, 10, 20, 50, 100) {
+    chop(my $utf8_format = "%-*s\x{100}");
+    my $string = "\xB4"x$i;        # latin1 ACUTE or ebcdic COPYRIGHT
+    my $expect = $string."  "x$i;  # followed by 2*$i spaces
+    is(sprintf($utf8_format, 3*$i, $string), $expect,
+       "width calculation under utf8 upgrade, length=$i");
+}
+
+# check simultaneous width & precision with wide characters
+for my $i (1, 3, 5, 10) {
+    my $string = "\x{0410}"x($i+10);   # cyrillic capital A
+    my $expect = "\x{0410}"x$i;        # cut down to exactly $i characters
+    my $format = "%$i.${i}s";
+    is(sprintf($format, $string), $expect,
+       "width & precision interplay with utf8 strings, length=$i");
 }
 
 # Used to mangle PL_sv_undef
@@ -75,3 +88,80 @@ for (int(~0/2+1), ~0, "9999999999999999999") {
        is ($bad, 0, "pattern '%v' . chr $ord");
     }
 }
+
+sub mysprintf_int_flags {
+    my ($fmt, $num) = @_;
+    die "wrong format $fmt" if $fmt !~ /^%([-+ 0]+)([1-9][0-9]*)d\z/;
+    my $flag  = $1;
+    my $width = $2;
+    my $sign  = $num < 0 ? '-' :
+               $flag =~ /\+/ ? '+' :
+               $flag =~ /\ / ? ' ' :
+               '';
+    my $abs   = abs($num);
+    my $padlen = $width - length($sign.$abs);
+    return
+       $flag =~ /0/ && $flag !~ /-/ # do zero padding
+           ? $sign . '0' x $padlen . $abs
+           : $flag =~ /-/ # left or right
+               ? $sign . $abs . ' ' x $padlen
+               : ' ' x $padlen . $sign . $abs;
+}
+
+# Whole tests for "%4d" with 2 to 4 flags;
+# total counts: 3 * (4**2 + 4**3 + 4**4) == 1008
+
+my @flags = ("-", "+", " ", "0");
+for my $num (0, -1, 1) {
+    for my $f1 (@flags) {
+       for my $f2 (@flags) {
+           for my $f3 ('', @flags) { # '' for doubled flags
+               my $flag = $f1.$f2.$f3;
+               my $width = 4;
+               my $fmt   = '%'."${flag}${width}d";
+               my $result = sprintf($fmt, $num);
+               my $expect = mysprintf_int_flags($fmt, $num);
+               is($result, $expect, qq/sprintf("$fmt",$num)/);
+
+               next if $f3 eq '';
+
+               for my $f4 (@flags) { # quadrupled flags
+                   my $flag = $f1.$f2.$f3.$f4;
+                   my $fmt   = '%'."${flag}${width}d";
+                   my $result = sprintf($fmt, $num);
+                   my $expect = mysprintf_int_flags($fmt, $num);
+                   is($result, $expect, qq/sprintf("$fmt",$num)/);
+               }
+           }
+       }
+    }
+}
+
+# test that %f doesn't panic with +Inf, -Inf, NaN [perl #45383]
+foreach my $n (2**1e100, -2**1e100, 2**1e100/2**1e100) { # +Inf, -Inf, NaN
+    eval { my $f = sprintf("%f", $n); };
+    is $@, "", "sprintf(\"%f\", $n)";
+}
+
+# test %ll formats with and without HAS_QUAD
+eval { my $q = pack "q", 0 };
+my $Q = $@ eq '';
+
+my @tests = (
+  [ '%lld' => [qw( 4294967296 -100000000000000 )] ],
+  [ '%lli' => [qw( 4294967296 -100000000000000 )] ],
+  [ '%llu' => [qw( 4294967296  100000000000000 )] ],
+  [ '%Ld'  => [qw( 4294967296 -100000000000000 )] ],
+  [ '%Li'  => [qw( 4294967296 -100000000000000 )] ],
+  [ '%Lu'  => [qw( 4294967296  100000000000000 )] ],
+);
+
+for my $t (@tests) {
+  my($fmt, $nums) = @$t;
+  for my $num (@$nums) {
+    my $w; local $SIG{__WARN__} = sub { $w = shift };
+    is(sprintf($fmt, $num), $Q ? $num : $fmt, "quad: $fmt -> $num");
+    like($w, $Q ? '' : qr/Invalid conversion in sprintf: "$fmt"/, "warning: $fmt");
+  }
+}
+