#!./perl -w

# Tests for sprintf that do not fit the format of sprintf.t.

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require './test.pl';
}   

# We'll run 12 extra tests (see below) if $Q is false.
eval { my $q = pack "q", 0 };
my $Q = $@ eq '';

plan tests => 1406 + ($Q ? 0 : 12);

use strict;
use Config;

is(
    sprintf("%.40g ",0.01),
    sprintf("%.40g", 0.01)." ",
    q(the sprintf "%.<number>g" optimization)
);
is(
    sprintf("%.40f ",0.01),
    sprintf("%.40f", 0.01)." ",
    q(the sprintf "%.<number>f" optimization)
);

# 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
fresh_perl_like(
    'print sprintf "xxx%n\n"; print undef',
    qr/Modification of a read-only value attempted at - line 1\./,
    { switches => [ '-w' ] },
    q(%n should not be able to modify read-only constants),
);

# check overflows
for (int(~0/2+1), ~0, "9999999999999999999") {
    is(eval {sprintf "%${_}d", 0}, undef, "no sprintf result expected %${_}d");
    like($@, qr/^Integer overflow in format string for sprintf /, "overflow in sprintf");
    is(eval {printf "%${_}d\n", 0}, undef, "no printf result expected %${_}d");
    like($@, qr/^Integer overflow in format string for printf /, "overflow in printf");
}

# check %NNN$ for range bounds
{
    my ($warn, $bad) = (0,0);
    local $SIG{__WARN__} = sub {
	if ($_[0] =~ /missing argument/i) {
	    $warn++
	}
	else {
	    $bad++
	}
    };

    my $fmt = join('', map("%$_\$s%" . ((1 << 31)-$_) . '$s', 1..20));
    my $result = sprintf $fmt, qw(a b c d);
    is($result, "abcd", "only four valid values in $fmt");
    is($warn, 36, "expected warnings");
    is($bad,   0, "unexpected warnings");
}

# Tests for "missing argument" and "redundant argument" warnings
{
    my ($warn_missing, $warn_redundant, $warn_bad) = (0,0,0);
    local $SIG{__WARN__} = sub {
	if ($_[0] =~ /missing argument/i) {
	    $warn_missing++
	}
	elsif ($_[0] =~ /redundant argument/i) {
	    $warn_redundant++
	}
	else {
	    $warn_bad++
	}
    };

    my @tests = (
	# The "", "%s", and "%-p" formats have special-case handling
	# in sv.c
	{
	    fmt	 => "",
	    args => [ qw( x ) ],
	    res	 => "",
	    m	 => 0,
	    r	 => 1,
	},
	{
	    fmt	 => "%s",
	    args => [ qw( x y ) ],
	    res	 => "x",
	    m	 => 0,
	    r	 => 1,
	},
	{
	    fmt	 => "%-p",
	    args => [ qw( x y ) ],
	    res	 => qr/^[0-9a-f]+$/as,
	    m	 => 0,
	    r	 => 1,
	},
	# Other non-specialcased patterns
	{
	    fmt	 => "%s : %s",
	    args => [ qw( a b c ) ],
	    res	 => "a : b",
	    m	 => 0,
	    r	 => 1,
	},
	{
	    fmt	 => "%s : %s : %s",
	    args => [ qw( a b c d e ) ],
	    res	 => "a : b : c",
	    m	 => 0,
	    # Note how we'll only warn about redundant arguments once,
	    # even though both "d" and "e" are redundant...
	    r	 => 1,
	},
	{
	    fmt	 => "%s : %s : %s",
	    args => [ ],
	    res	 => " :  : ",
	    # ...But when arguments are missing we'll warn about every
	    # missing argument. This difference between the two
	    # warnings is a feature.
	    m	 => 3,
	    r	 => 0,
	},

	# Tests for format parameter indexes.
	#
	# Deciding what to do about these is a bit tricky, and so is
	# "correctly" warning about missing arguments on them.
	#
	# Should we warn if you supply 4 arguments but only use
	# argument 1,3 & 4? Or only if you supply 5 arguments and your
	# highest used argument is 4?
	#
	# For some uses of this printf feature (e.g. i18n systems)
	# it's a always a logic error to not print out every provided
	# argument, but for some other uses skipping some might be a
	# feature (although you could argue that then printf should be
	# called as e.g:
	#
	#     printf q[%1$s %3$s], x(), undef, z();
	#
	# Instead of:
	#
	#    printf q[%1$s %3$s], x(), y(), z();
	#
	# Since calling the (possibly expensive) y() function is
	# completely redundant there.
	#
	# We deal with all these potential problems by not even
	# trying. If the pattern contains any format parameter indexes
	# whatsoever we'll never warn about redundant arguments.
	{
	    fmt	 => '%1$s : %2$s',
	    args => [ qw( x y z ) ],
	    res	 => "x : y",
	    m	 => 0,
	    r	 => 0,
	},
	{
	    fmt	 => '%2$s : %4$s : %5$s',
	    args => [ qw( a b c d )],
	    res	 => "b : d : ",
	    m	 => 1,
	    r	 => 0,
	},
	{
	    fmt	 => '%s : %1$s : %s',
	    args => [ qw( x y z ) ],
	    res	 => "x : x : y",
	    m	 => 0,
	    r	 => 0,
	},

    );

    for my $i (0..$#tests) {
	my $test = $tests[$i];
	my $result = sprintf $test->{fmt}, @{$test->{args}};

	my $prefix = "For format '$test->{fmt}' and arguments/result '@{$test->{args}}'/'$result'";
	if (ref $test->{res} eq 'Regexp') {
	    like($result, $test->{res}, "$prefix got the right result");
	} else {
	    is($result, $test->{res}, "$prefix got the right result");
	}
	is($warn_missing, $test->{m}, "$prefix got '$test->{m}' 'missing argument' warnings");
	is($warn_redundant, $test->{r}, "$prefix got '$test->{r}' 'redundant argument' warnings");
	is($warn_bad, 0, "$prefix No unknown warnings");

	($warn_missing, $warn_redundant, $warn_bad) = (0,0,0);
    }
}

{
    foreach my $ord (0 .. 255) {
	my $bad = 0;
	local $SIG{__WARN__} = sub {
	    if ($_[0] !~ /^Invalid conversion in sprintf/) {
		warn $_[0];
		$bad++;
	    }
	};
	my $r = eval {sprintf '%v' . chr $ord};
	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
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 };
    my $sprintf_got = sprintf($fmt, $num);
    if ($Q) {
      is($sprintf_got, $num, "quad: $fmt -> $num");
      is($w, '', "no warnings for: $fmt -> $num");
    } else {
      is($sprintf_got, $fmt, "quad unsupported: $fmt -> $fmt");
      like($w, qr/Invalid conversion in sprintf: "$fmt"/, "got warning about invalid conversion from fmt : $fmt");
      like($w, qr/Redundant argument in sprintf/, "got warning about redundant argument in sprintf from fmt : $fmt");
    }
  }
}

# Check unicode vs byte length
for my $width (1,2,3,4,5,6,7) {
    for my $precis (1,2,3,4,5,6,7) {
        my $v = "\x{20ac}\x{20ac}";
        my $format = "%" . $width . "." . $precis . "s";
        my $chars = ($precis > 2 ? 2 : $precis);
        my $space = ($width < 2 ? 0 : $width - $chars);
        fresh_perl_is(
            'my $v = "\x{20ac}\x{20ac}"; my $x = sprintf "'.$format.'", $v; $x =~ /^(\s*)(\S*)$/; print "$_" for map {length} $1, $2',
            "$space$chars",
            {},
            q(sprintf ").$format.q(", "\x{20ac}\x{20ac}"),
        );
    }
}

# Overload count
package o { use overload '""', sub { ++our $count; $_[0][0]; } }
my $o = bless ["\x{100}"], o::;
() = sprintf "%1s", $o;
is $o::count, '1', 'sprinf %1s overload count';
$o::count = 0;
() = sprintf "%.1s", $o;
is $o::count, '1', 'sprinf %.1s overload count';
