1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#!./perl
BEGIN {
chdir 't' if -d 't';
require "./test.pl";
set_up_inc(qw(. ../lib));
}
plan( tests => 73 );
@foo = (1, 2, 3, 4);
cmp_ok($foo[0], '==', 1, 'first elem');
cmp_ok($foo[3], '==', 4, 'last elem');
$_ = join(':',@foo);
cmp_ok($_, 'eq', '1:2:3:4', 'join list');
($a,$b,$c,$d) = (1,2,3,4);
cmp_ok("$a;$b;$c;$d", 'eq', '1;2;3;4', 'list assign');
($c,$b,$a) = split(/ /,"111 222 333");
cmp_ok("$a;$b;$c",'eq','333;222;111','list split on space');
($a,$b,$c) = ($c,$b,$a);
cmp_ok("$a;$b;$c",'eq','111;222;333','trio rotate');
($a, $b) = ($b, $a);
cmp_ok("$a-$b",'eq','222-111','duo swap');
($a, $b) = ($b, $a) = ($a, $b);
cmp_ok("$a-$b",'eq','222-111','duo swap swap');
($a, $b[1], $c{2}, $d) = (1, 2, 3, 4);
cmp_ok($a,'==',1,'assign scalar in list');
cmp_ok($b[1],'==',2,'assign aelem in list');
cmp_ok($c{2},'==',3,'assign helem in list');
cmp_ok($d,'==',4,'assign last scalar in list');
@foo = (1,2,3,4,5,6,7,8);
($a, $b, $c, $d) = @foo;
cmp_ok("$a/$b/$c/$d",'eq','1/2/3/4','long list assign');
@foo = (1,2);
($a, $b, $c, $d) = @foo;
cmp_ok($a,'==',1,'short list 1 defined');
cmp_ok($b,'==',2,'short list 2 defined');
ok(!defined($c),'short list 3 undef');
ok(!defined($d),'short list 4 undef');
@foo = @bar = (1);
cmp_ok(join(':',@foo,@bar),'eq','1:1','list reassign');
@foo = @bar = (2,3);
cmp_ok(join(':',join('+',@foo),join('-',@bar)),'eq','2+3:2-3','long list reassign');
@foo = ();
@foo = 1+2+3;
cmp_ok(join(':',@foo),'eq','6','scalar assign to array');
{
my ($a, $b, $c);
for ($x = 0; $x < 3; $x = $x + 1) {
($a, $b, $c) =
$x == 0 ? ('a','b','c')
: $x == 1 ? ('d','e','f')
: ('g','h','i')
;
if ($x == 0) {
cmp_ok($a,'eq','a','ternary for a 1');
cmp_ok($b,'eq','b','ternary for b 1');
cmp_ok($c,'eq','c','ternary for c 1');
}
if ($x == 1) {
cmp_ok($a,'eq','d','ternary for a 2');
cmp_ok($b,'eq','e','ternary for b 2');
cmp_ok($c,'eq','f','ternary for c 2');
}
if ($x == 2) {
cmp_ok($a,'eq','g','ternary for a 3');
cmp_ok($b,'eq','h','ternary for b 3');
cmp_ok($c,'eq','i','ternary for c 3');
}
}
}
{
my ($a, $b, $c);
for ($x = 0; $x < 3; $x = $x + 1) {
($a, $b, $c) = do {
if ($x == 0) {
('a','b','c');
}
elsif ($x == 1) {
('d','e','f');
}
else {
('g','h','i');
}
};
if ($x == 0) {
cmp_ok($a,'eq','a','block for a 1');
cmp_ok($b,'eq','b','block for b 1');
cmp_ok($c,'eq','c','block for c 1');
}
if ($x == 1) {
cmp_ok($a,'eq','d','block for a 2');
cmp_ok($b,'eq','e','block for b 2');
cmp_ok($c,'eq','f','block for c 2');
}
if ($x == 2) {
cmp_ok($a,'eq','g','block for a 3');
cmp_ok($b,'eq','h','block for b 3');
cmp_ok($c,'eq','i','block for c 3');
}
}
}
$x = 666;
@a = ($x == 12345 || (1,2,3));
cmp_ok(join('*',@a),'eq','1*2*3','logical or f');
@a = ($x == $x || (4,5,6));
cmp_ok(join('*',@a),'eq','1','logical or t');
cmp_ok(join('',1,2,(3,4,5)),'eq','12345','list ..(...)');
cmp_ok(join('',(1,2,3,4,5)),'eq','12345','list (.....)');
cmp_ok(join('',(1,2,3,4),5),'eq','12345','list (....).');
cmp_ok(join('',1,(2,3,4),5),'eq','12345','list .(...).');
cmp_ok(join('',1,2,(3,4),5),'eq','12345','list ..(..).');
cmp_ok(join('',1,2,3,(4),5),'eq','12345','list ...(.).');
cmp_ok(join('',(1,2),3,(4,5)),'eq','12345','list (..).(..)');
{
my @a = (0, undef, undef, 3);
my @b = @a[1,2];
my @c = (0, undef, undef, 3)[1, 2];
cmp_ok(scalar(@b),'==',scalar(@c),'slice and slice');
cmp_ok(scalar(@c),'==',2,'slice len');
@b = (29, scalar @c[()]);
cmp_ok(join(':',@b),'eq','29:','slice ary nil');
my %h = (a => 1);
@b = (30, scalar @h{()});
cmp_ok(join(':',@b),'eq','30:','slice hash nil');
my $size = scalar(()[1..1]);
cmp_ok($size,'==','0','size nil');
$size = scalar(()=((1,2,3,4,5)[()])[2,3,4]);
is $size, 0, 'slice of empty list from complex expr is empty list';
@a = (1)[2,3,4];
is "@{[ map $_//'undef', @a ]}", "undef undef undef",
'slice beyond the end of non-empty list returns undefs';
}
{
# perl #39882
sub test_two_args {
my $test_name = shift;
is(scalar(@_), 2, $test_name);
}
test_two_args("simple list slice", (10,11)[2,3]);
test_two_args("grepped list slice", grep(1, (10,11)[2,3]));
test_two_args("sorted list slice", sort((10,11)[2,3]));
test_two_args("assigned list slice", my @tmp = (10,11)[2,3]);
test_two_args("do-returned list slice", do { (10,11)[2,3]; });
test_two_args("list literal slice", qw(a b)[2,3]);
is (()=qw()[2,3], 0, "empty literal slice");
}
{
# perl #20321
is (join('', @{[('abc'=~/./g)[0,1,2,1,0]]}), "abcba");
}
{
is(join('', qw(a b c)[2,0,1]), "cab");
my @a = qw(a b c);
is(join(":", @a), "a:b:c");
my @b = qw();
is($#b, -1);
}
{
# comma operator with lvalue only propagates the lvalue context to
# the last operand.
("const", my $x) ||= 1;
is( $x, 1 );
}
# [perl #78194] list slice aliasing op return values
sub {
is(\$_[0], \$_[1],
'[perl #78194] \$_[0] == \$_[1] when @_ aliases elems repeated by lslice'
)
}
->(("${\''}")[0,0]);
# [perl #122995] Hang when compiling while(1) in a sub-list
# No ok() or is() necessary.
sub foo { () = ($a, my $b, ($c, do { while(1) {} })) }
# List assignment and OPpTARGET_MY
{
my ($a,$b);
my $foo = "foo";
my $bar = "bar";
($a,$b) = ($b = $foo."", $a = $bar . "");
is("$a,$b", "foo,bar", 'common vars check accounts for OPpTARGET_MY');
}
sub TIESCALAR {bless{}}
sub FETCH {$_[0]{fetched}++}
sub empty {}
tie $t, "";
() = (empty(), ($t)x10); # empty() since sub calls usually result in copies
is(tied($t)->{fetched}, undef, 'assignment to empty list makes no copies');
# this was passing a trash SV at the top of the stack to SvIV()
ok(($0[()[()]],1), "[perl #126193] list slice with zero indexes");
# RT #131732: pp_list must extend stack when empty-array arg and not in list
# context
{
my @x;
@x;
pass('no panic'); # panics only under DEBUGGING
}
fresh_perl_is(<<'EOS', "", {}, "[perl #131954] heap use after free in pp_list");
#!./perl
BEGIN {
my $bar = "bar";
sub test_no_error {
eval $_[0];
}
test_no_error($_) for split /\n/,
q[ x
definfoo, $bar;
x
x
x
grep((not $bar, $bar, $bar), $bar);
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
];
}
EOS
# this used to SEGV due to deep recursion in Perl_list()
{
my $e = "1"; $e = "(1,$e)" for 1..100_000; $e = "() = $e"; eval $e;
is $@, "", "SEGV in Perl_list";
}
|