This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #37725] perl segfaults on reversed array reference
[perl5.git] / t / op / undef.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 print "1..36\n";
9
10 print defined($a) ? "not ok 1\n" : "ok 1\n";
11
12 $a = 1+1;
13 print defined($a) ? "ok 2\n" : "not ok 2\n";
14
15 undef $a;
16 print defined($a) ? "not ok 3\n" : "ok 3\n";
17
18 $a = "hi";
19 print defined($a) ? "ok 4\n" : "not ok 4\n";
20
21 $a = $b;
22 print defined($a) ? "not ok 5\n" : "ok 5\n";
23
24 @ary = ("1arg");
25 $a = pop(@ary);
26 print defined($a) ? "ok 6\n" : "not ok 6\n";
27 $a = pop(@ary);
28 print defined($a) ? "not ok 7\n" : "ok 7\n";
29
30 @ary = ("1arg");
31 $a = shift(@ary);
32 print defined($a) ? "ok 8\n" : "not ok 8\n";
33 $a = shift(@ary);
34 print defined($a) ? "not ok 9\n" : "ok 9\n";
35
36 $ary{'foo'} = 'hi';
37 print defined($ary{'foo'}) ? "ok 10\n" : "not ok 10\n";
38 print defined($ary{'bar'}) ? "not ok 11\n" : "ok 11\n";
39 undef $ary{'foo'};
40 print defined($ary{'foo'}) ? "not ok 12\n" : "ok 12\n";
41
42 print defined(@ary) ? "ok 13\n" : "not ok 13\n";
43 print defined(%ary) ? "ok 14\n" : "not ok 14\n";
44 undef @ary;
45 print defined(@ary) ? "not ok 15\n" : "ok 15\n";
46 undef %ary;
47 print defined(%ary) ? "not ok 16\n" : "ok 16\n";
48 @ary = (1);
49 print defined @ary ? "ok 17\n" : "not ok 17\n";
50 %ary = (1,1);
51 print defined %ary ? "ok 18\n" : "not ok 18\n";
52
53 sub foo { print "ok 19\n"; }
54
55 &foo || print "not ok 19\n";
56
57 print defined &foo ? "ok 20\n" : "not ok 20\n";
58 undef &foo;
59 print defined(&foo) ? "not ok 21\n" : "ok 21\n";
60
61 eval { undef $1 };
62 print $@ =~ /^Modification of a read/ ? "ok 22\n" : "not ok 22\n";
63
64 eval { $1 = undef };
65 print $@ =~ /^Modification of a read/ ? "ok 23\n" : "not ok 23\n";
66
67 {
68     require Tie::Hash;
69     tie my %foo, 'Tie::StdHash';
70     print defined %foo ? "ok 24\n" : "not ok 24\n";
71     %foo = ( a => 1 );
72     print defined %foo ? "ok 25\n" : "not ok 25\n";
73 }
74
75 {
76     require Tie::Array;
77     tie my @foo, 'Tie::StdArray';
78     print defined @foo ? "ok 26\n" : "not ok 26\n";
79     @foo = ( a => 1 );
80     print defined @foo ? "ok 27\n" : "not ok 27\n";
81 }
82
83 {
84     # [perl #17753] segfault when undef'ing unquoted string constant
85     eval 'undef tcp';
86     print $@ =~ /^Can't modify constant item/ ? "ok 28\n" : "not ok 28\n";
87 }
88
89 # bugid 3096
90 # undefing a hash may free objects with destructors that then try to
91 # modify the hash. To them, the hash should appear empty.
92
93 $test = 29;
94 %hash = (
95     key1 => bless({}, 'X'),
96     key2 => bless({}, 'X'),
97 );
98 undef %hash;
99 sub X::DESTROY {
100     print "not " if keys   %hash; print "ok $test\n"; $test++;
101     print "not " if values %hash; print "ok $test\n"; $test++;
102     print "not " if each   %hash; print "ok $test\n"; $test++;
103     print "not " if defined delete $hash{'key2'}; print "ok $test\n"; $test++;
104 }