Change the regression test added by 26410 to use test.pl; this makes
[p5sagit/p5-mst-13.2.git] / t / op / sprintf2.t
CommitLineData
1d917b39 1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
45e52d63 9plan tests => 275;
1d917b39 10
11is(
12 sprintf("%.40g ",0.01),
13 sprintf("%.40g", 0.01)." ",
14 q(the sprintf "%.<number>g" optimization)
15);
16is(
17 sprintf("%.40f ",0.01),
18 sprintf("%.40f", 0.01)." ",
19 q(the sprintf "%.<number>f" optimization)
20);
6c94ec8b 21{
22 chop(my $utf8_format = "%-3s\x{100}");
23 is(
24 sprintf($utf8_format, "\xe4"),
25 "\xe4 ",
26 q(width calculation under utf8 upgrade)
27 );
28}
fc7325bb 29
30# Used to mangle PL_sv_undef
31fresh_perl_is(
32 'print sprintf "xxx%n\n"; print undef',
33 'Modification of a read-only value attempted at - line 1.',
34 { switches => [ '-w' ] },
35 q(%n should not be able to modify read-only constants),
863811b2 36);
37
2fba7546 38# check overflows
45e52d63 39for (int(~0/2+1), ~0, "9999999999999999999") {
2fba7546 40 is(eval {sprintf "%${_}d", 0}, undef, "no sprintf result expected %${_}d");
41 like($@, qr/^Integer overflow in format string for sprintf /, "overflow in sprintf");
42 is(eval {printf "%${_}d\n", 0}, undef, "no printf result expected %${_}d");
43 like($@, qr/^Integer overflow in format string for prtf /, "overflow in printf");
44}
863811b2 45
2fba7546 46# check %NNN$ for range bounds
863811b2 47{
48 my ($warn, $bad) = (0,0);
49 local $SIG{__WARN__} = sub {
50 if ($_[0] =~ /uninitialized/) {
51 $warn++
52 }
53 else {
54 $bad++
55 }
56 };
2fba7546 57
45e52d63 58 my $fmt = join('', map("%$_\$s%" . ((1 << 31)-$_) . '$s', 1..20));
2fba7546 59 my $result = sprintf $fmt, qw(a b c d);
60 is($result, "abcd", "only four valid values in $fmt");
863811b2 61 is($warn, 36, "expected warnings");
62 is($bad, 0, "unexpected warnings");
63}
64
343ef749 65{
66 foreach my $ord (0 .. 255) {
67 my $bad = 0;
68 local $SIG{__WARN__} = sub {
69 if ($_[0] !~ /^Invalid conversion in sprintf/) {
70 warn $_[0];
71 $bad++;
72 }
73 };
74 my $r = eval {sprintf '%v' . chr $ord};
75 is ($bad, 0, "pattern '%v' . chr $ord");
76 }
77}