Commit | Line | Data |
---|---|---|
378cc40b LW |
1 | #!./perl |
2 | ||
7583b5e8 DL |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
624c42e2 N |
5 | require "./test.pl"; |
6 | set_up_inc( qw(. ../lib) ); | |
7583b5e8 DL |
7 | } |
8 | ||
cc0776d6 | 9 | plan( tests => 56 ); |
01020589 GS |
10 | |
11 | # delete() on hash elements | |
378cc40b LW |
12 | |
13 | $foo{1} = 'a'; | |
14 | $foo{2} = 'b'; | |
15 | $foo{3} = 'c'; | |
5f05dabc | 16 | $foo{4} = 'd'; |
17 | $foo{5} = 'e'; | |
cc0776d6 DIM |
18 | $foo{6} = 'f'; |
19 | $foo{7} = 'g'; | |
378cc40b LW |
20 | |
21 | $foo = delete $foo{2}; | |
22 | ||
7583b5e8 DL |
23 | cmp_ok($foo,'eq','b','delete 2'); |
24 | ok(!(exists $foo{2}),'b absent'); | |
25 | cmp_ok($foo{1},'eq','a','a exists'); | |
26 | cmp_ok($foo{3},'eq','c','c exists'); | |
27 | cmp_ok($foo{4},'eq','d','d exists'); | |
28 | cmp_ok($foo{5},'eq','e','e exists'); | |
5f05dabc | 29 | |
30 | @foo = delete @foo{4, 5}; | |
31 | ||
7583b5e8 DL |
32 | cmp_ok(scalar(@foo),'==',2,'deleted slice'); |
33 | cmp_ok($foo[0],'eq','d','slice 1'); | |
34 | cmp_ok($foo[1],'eq','e','slice 2'); | |
35 | ok(!(exists $foo{4}),'d absent'); | |
36 | ok(!(exists $foo{5}),'e absent'); | |
37 | cmp_ok($foo{1},'eq','a','a still exists'); | |
38 | cmp_ok($foo{3},'eq','c','c still exists'); | |
378cc40b | 39 | |
cc0776d6 DIM |
40 | @foo = delete %foo{6,7}; |
41 | ||
42 | cmp_ok(scalar(@foo),'==',4,'deleted kvslice'); | |
43 | cmp_ok($foo[0],'eq','6','slice k1'); | |
44 | cmp_ok($foo[1],'eq','f','slice v1'); | |
45 | cmp_ok($foo[2],'eq','7','slice k2'); | |
46 | cmp_ok($foo[3],'eq','g','slice v2'); | |
47 | ok(!(exists $foo{5}),'f absent'); | |
48 | ok(!(exists $foo{6}),'g absent'); | |
49 | cmp_ok($foo{1},'eq','a','a still exists'); | |
50 | cmp_ok($foo{3},'eq','c','c still exists'); | |
51 | ||
c6aa4a32 | 52 | $foo = join('',values(%foo)); |
7583b5e8 | 53 | ok($foo eq 'ac' || $foo eq 'ca','remaining keys'); |
378cc40b | 54 | |
c6aa4a32 | 55 | foreach $key (keys %foo) { |
378cc40b LW |
56 | delete $foo{$key}; |
57 | } | |
58 | ||
59 | $foo{'foo'} = 'x'; | |
60 | $foo{'bar'} = 'y'; | |
61 | ||
c6aa4a32 | 62 | $foo = join('',values(%foo)); |
7583b5e8 | 63 | ok($foo eq 'xy' || $foo eq 'yx','fresh keys'); |
a0d0e21e LW |
64 | |
65 | $refhash{"top"}->{"foo"} = "FOO"; | |
66 | $refhash{"top"}->{"bar"} = "BAR"; | |
67 | ||
68 | delete $refhash{"top"}->{"bar"}; | |
69 | @list = keys %{$refhash{"top"}}; | |
70 | ||
7583b5e8 | 71 | cmp_ok("@list",'eq',"foo", 'autoviv and delete hashref'); |
2f83d9b6 GS |
72 | |
73 | { | |
74 | my %a = ('bar', 33); | |
75 | my($a) = \(values %a); | |
76 | my $b = \$a{bar}; | |
77 | my $c = \delete $a{bar}; | |
78 | ||
7583b5e8 | 79 | ok($a == $b && $b == $c,'a b c equivalent'); |
2f83d9b6 | 80 | } |
01020589 GS |
81 | |
82 | # delete() on array elements | |
83 | ||
84 | @foo = (); | |
85 | $foo[1] = 'a'; | |
86 | $foo[2] = 'b'; | |
87 | $foo[3] = 'c'; | |
88 | $foo[4] = 'd'; | |
89 | $foo[5] = 'e'; | |
cc0776d6 DIM |
90 | $foo[6] = 'f'; |
91 | $foo[7] = 'g'; | |
01020589 GS |
92 | |
93 | $foo = delete $foo[2]; | |
94 | ||
7583b5e8 DL |
95 | cmp_ok($foo,'eq','b','ary delete 2'); |
96 | ok(!(exists $foo[2]),'ary b absent'); | |
97 | cmp_ok($foo[1],'eq','a','ary a exists'); | |
98 | cmp_ok($foo[3],'eq','c','ary c exists'); | |
99 | cmp_ok($foo[4],'eq','d','ary d exists'); | |
100 | cmp_ok($foo[5],'eq','e','ary e exists'); | |
01020589 GS |
101 | |
102 | @bar = delete @foo[4,5]; | |
103 | ||
7583b5e8 DL |
104 | cmp_ok(scalar(@bar),'==',2,'ary deleted slice'); |
105 | cmp_ok($bar[0],'eq','d','ary slice 1'); | |
106 | cmp_ok($bar[1],'eq','e','ary slice 2'); | |
107 | ok(!(exists $foo[4]),'ary d absent'); | |
108 | ok(!(exists $foo[5]),'ary e absent'); | |
109 | cmp_ok($foo[1],'eq','a','ary a still exists'); | |
110 | cmp_ok($foo[3],'eq','c','ary c still exists'); | |
01020589 | 111 | |
cc0776d6 DIM |
112 | @bar = delete %foo[6,7]; |
113 | ||
114 | cmp_ok(scalar(@bar),'==',4,'deleted kvslice'); | |
115 | cmp_ok($bar[0],'eq','6','slice k1'); | |
116 | cmp_ok($bar[1],'eq','f','slice v1'); | |
117 | cmp_ok($bar[2],'eq','7','slice k2'); | |
118 | cmp_ok($bar[3],'eq','g','slice v2'); | |
119 | ok(!(exists $bar[5]),'f absent'); | |
120 | ok(!(exists $bar[6]),'g absent'); | |
121 | cmp_ok($foo[1],'eq','a','a still exists'); | |
122 | cmp_ok($foo[3],'eq','c','c still exists'); | |
123 | ||
01020589 | 124 | $foo = join('',@foo); |
7583b5e8 DL |
125 | cmp_ok($foo,'eq','ac','ary elems'); |
126 | cmp_ok(scalar(@foo),'==',4,'four is the number thou shalt count'); | |
01020589 GS |
127 | |
128 | foreach $key (0 .. $#foo) { | |
129 | delete $foo[$key]; | |
130 | } | |
131 | ||
7583b5e8 | 132 | cmp_ok(scalar(@foo),'==',0,'and then there were none'); |
01020589 GS |
133 | |
134 | $foo[0] = 'x'; | |
135 | $foo[1] = 'y'; | |
136 | ||
137 | $foo = "@foo"; | |
7583b5e8 | 138 | cmp_ok($foo,'eq','x y','two fresh'); |
01020589 GS |
139 | |
140 | $refary[0]->[0] = "FOO"; | |
141 | $refary[0]->[3] = "BAR"; | |
142 | ||
143 | delete $refary[0]->[3]; | |
144 | ||
7583b5e8 | 145 | cmp_ok( scalar(@{$refary[0]}),'==',1,'one down'); |
01020589 GS |
146 | |
147 | { | |
148 | my @a = 33; | |
149 | my($a) = \(@a); | |
150 | my $b = \$a[0]; | |
151 | my $c = \delete $a[bar]; | |
152 | ||
7583b5e8 | 153 | ok($a == $b && $b == $c,'a b c also equivalent'); |
01020589 | 154 | } |
9111c9c0 DM |
155 | |
156 | { | |
9111c9c0 DM |
157 | my %h; |
158 | my ($x,$y) = (1, scalar delete @h{()}); | |
7583b5e8 | 159 | ok(!defined($y),q([perl #29127] scalar delete of empty slice returned garbage)); |
9111c9c0 | 160 | } |
2c8ddff3 DM |
161 | |
162 | { | |
2c8ddff3 DM |
163 | my $x = 0; |
164 | sub X::DESTROY { $x++ } | |
165 | { | |
166 | my @a; | |
167 | $a[0] = bless [], 'X'; | |
168 | my $y = delete $a[0]; | |
169 | } | |
7583b5e8 | 170 | cmp_ok($x,'==',1,q([perl #30733] array delete didn't free returned element)); |
2c8ddff3 | 171 | } |