This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix bug 36267 - assigning to a tied hash shouldn't change the
[perl5.git] / t / op / sprintf2.t
1 #!./perl -w
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require './test.pl';
7 }   
8
9 plan tests => 275;
10
11 is(
12     sprintf("%.40g ",0.01),
13     sprintf("%.40g", 0.01)." ",
14     q(the sprintf "%.<number>g" optimization)
15 );
16 is(
17     sprintf("%.40f ",0.01),
18     sprintf("%.40f", 0.01)." ",
19     q(the sprintf "%.<number>f" optimization)
20 );
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 }
29
30 # Used to mangle PL_sv_undef
31 fresh_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),
36 );
37
38 # check overflows
39 for (int(~0/2+1), ~0, "9999999999999999999") {
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 }
45
46 # check %NNN$ for range bounds
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     };
57
58     my $fmt = join('', map("%$_\$s%" . ((1 << 31)-$_) . '$s', 1..20));
59     my $result = sprintf $fmt, qw(a b c d);
60     is($result, "abcd", "only four valid values in $fmt");
61     is($warn, 36, "expected warnings");
62     is($bad,   0, "unexpected warnings");
63 }
64
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 }