This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
svleak.t: Enable syntax error tests under -Dmad
[perl5.git] / t / op / array.t
CommitLineData
a687059c
LW
1#!./perl
2
6b42d12b
DM
3BEGIN {
4 chdir 't' if -d 't';
404a4710 5 @INC = ('.', '../lib');
9f71cfe6 6 require 'test.pl';
6b42d12b
DM
7}
8
70ce9249 9plan (127);
a687059c 10
05fc92f1
GS
11#
12# @foo, @bar, and @ary are also used from tie-stdarray after tie-ing them
13#
14
a687059c 15@ary = (1,2,3,4,5);
0dd3f902 16is(join('',@ary), '12345');
a687059c
LW
17
18$tmp = $ary[$#ary]; --$#ary;
0dd3f902
NC
19is($tmp, 5);
20is($#ary, 3);
21is(join('',@ary), '1234');
a687059c 22
136c2a5e
NC
23{
24 no warnings 'deprecated';
25
a687059c
LW
26@foo = ();
27$r = join(',', $#foo, @foo);
0dd3f902 28is($r, "-1");
a687059c
LW
29$foo[0] = '0';
30$r = join(',', $#foo, @foo);
0dd3f902 31is($r, "0,0");
a687059c
LW
32$foo[2] = '2';
33$r = join(',', $#foo, @foo);
0dd3f902 34is($r, "2,0,,2");
a687059c
LW
35@bar = ();
36$bar[0] = '0';
37$bar[1] = '1';
38$r = join(',', $#bar, @bar);
0dd3f902 39is($r, "1,0,1");
a687059c
LW
40@bar = ();
41$r = join(',', $#bar, @bar);
0dd3f902 42is($r, "-1");
a687059c
LW
43$bar[0] = '0';
44$r = join(',', $#bar, @bar);
0dd3f902 45is($r, "0,0");
a687059c
LW
46$bar[2] = '2';
47$r = join(',', $#bar, @bar);
0dd3f902 48is($r, "2,0,,2");
14ce8c55 49reset 'b' if $^O ne 'VMS';
a687059c
LW
50@bar = ();
51$bar[0] = '0';
52$r = join(',', $#bar, @bar);
0dd3f902 53is($r, "0,0");
a687059c
LW
54$bar[2] = '2';
55$r = join(',', $#bar, @bar);
0dd3f902 56is($r, "2,0,,2");
a687059c 57
136c2a5e
NC
58}
59
a687059c 60$foo = 'now is the time';
0dd3f902
NC
61ok(scalar (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/)));
62is($F1, 'now');
63is($F2, 'is');
64is($Etc, 'the time');
a687059c
LW
65
66$foo = 'lskjdf';
0dd3f902
NC
67ok(!($cnt = (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))))
68 or diag("$cnt $F1:$F2:$Etc");
a687059c
LW
69
70%foo = ('blurfl','dyick','foo','bar','etc.','etc.');
71%bar = %foo;
0dd3f902 72is($bar{'foo'}, 'bar');
a687059c 73%bar = ();
0dd3f902 74is($bar{'foo'}, undef);
a687059c 75(%bar,$a,$b) = (%foo,'how','now');
0dd3f902
NC
76is($bar{'foo'}, 'bar');
77is($bar{'how'}, 'now');
a687059c 78@bar{keys %foo} = values %foo;
0dd3f902
NC
79is($bar{'foo'}, 'bar');
80is($bar{'how'}, 'now');
a687059c
LW
81
82@foo = grep(/e/,split(' ','now is the time for all good men to come to'));
0dd3f902 83is(join(' ',@foo), 'the time men come');
a687059c
LW
84
85@foo = grep(!/e/,split(' ','now is the time for all good men to come to'));
0dd3f902 86is(join(' ',@foo), 'now is for all good to to');
79a0689e
LW
87
88$foo = join('',('a','b','c','d','e','f')[0..5]);
0dd3f902 89is($foo, 'abcdef');
79a0689e
LW
90
91$foo = join('',('a','b','c','d','e','f')[0..1]);
0dd3f902 92is($foo, 'ab');
79a0689e
LW
93
94$foo = join('',('a','b','c','d','e','f')[6]);
0dd3f902 95is($foo, '');
79a0689e
LW
96
97@foo = ('a','b','c','d','e','f')[0,2,4];
98@bar = ('a','b','c','d','e','f')[1,3,5];
99$foo = join('',(@foo,@bar)[0..5]);
0dd3f902 100is($foo, 'acebdf');
79a0689e
LW
101
102$foo = ('a','b','c','d','e','f')[0,2,4];
0dd3f902 103is($foo, 'e');
79a0689e
LW
104
105$foo = ('a','b','c','d','e','f')[1];
0dd3f902 106is($foo, 'b');
a0231f0e 107
c6aa4a32 108@foo = ( 'foo', 'bar', 'burbl');
136c2a5e
NC
109{
110 no warnings 'deprecated';
111 push(foo, 'blah');
112}
0dd3f902 113is($#foo, 3);
b3381831
GS
114
115# various AASSIGN_COMMON checks (see newASSIGNOP() in op.c)
116
0dd3f902 117#curr_test(38);
b3381831
GS
118
119@foo = @foo;
0dd3f902 120is("@foo", "foo bar burbl blah"); # 38
b3381831
GS
121
122(undef,@foo) = @foo;
0dd3f902 123is("@foo", "bar burbl blah"); # 39
b3381831
GS
124
125@foo = ('XXX',@foo, 'YYY');
0dd3f902 126is("@foo", "XXX bar burbl blah YYY"); # 40
b3381831 127
3201ebbd 128@foo = @foo = qw(foo b\a\r bu\\rbl blah);
0dd3f902 129is("@foo", 'foo b\a\r bu\\rbl blah'); # 41
b3381831
GS
130
131@bar = @foo = qw(foo bar); # 42
0dd3f902
NC
132is("@foo", "foo bar");
133is("@bar", "foo bar"); # 43
b3381831
GS
134
135# try the same with local
05fc92f1
GS
136# XXX tie-stdarray fails the tests involving local, so we use
137# different variable names to escape the 'tie'
138
139@bee = ( 'foo', 'bar', 'burbl', 'blah');
b3381831
GS
140{
141
05fc92f1 142 local @bee = @bee;
0dd3f902 143 is("@bee", "foo bar burbl blah"); # 44
b3381831 144 {
05fc92f1 145 local (undef,@bee) = @bee;
0dd3f902 146 is("@bee", "bar burbl blah"); # 45
b3381831 147 {
05fc92f1 148 local @bee = ('XXX',@bee,'YYY');
0dd3f902 149 is("@bee", "XXX bar burbl blah YYY"); # 46
b3381831 150 {
05fc92f1 151 local @bee = local(@bee) = qw(foo bar burbl blah);
0dd3f902 152 is("@bee", "foo bar burbl blah"); # 47
b3381831 153 {
05fc92f1 154 local (@bim) = local(@bee) = qw(foo bar);
0dd3f902
NC
155 is("@bee", "foo bar"); # 48
156 is("@bim", "foo bar"); # 49
b3381831 157 }
0dd3f902 158 is("@bee", "foo bar burbl blah"); # 50
b3381831 159 }
0dd3f902 160 is("@bee", "XXX bar burbl blah YYY"); # 51
b3381831 161 }
0dd3f902 162 is("@bee", "bar burbl blah"); # 52
b3381831 163 }
0dd3f902 164 is("@bee", "foo bar burbl blah"); # 53
b3381831
GS
165}
166
167# try the same with my
168{
05fc92f1 169 my @bee = @bee;
0dd3f902 170 is("@bee", "foo bar burbl blah"); # 54
b3381831 171 {
05fc92f1 172 my (undef,@bee) = @bee;
0dd3f902 173 is("@bee", "bar burbl blah"); # 55
b3381831 174 {
05fc92f1 175 my @bee = ('XXX',@bee,'YYY');
0dd3f902 176 is("@bee", "XXX bar burbl blah YYY"); # 56
b3381831 177 {
05fc92f1 178 my @bee = my @bee = qw(foo bar burbl blah);
0dd3f902 179 is("@bee", "foo bar burbl blah"); # 57
b3381831 180 {
05fc92f1 181 my (@bim) = my(@bee) = qw(foo bar);
0dd3f902
NC
182 is("@bee", "foo bar"); # 58
183 is("@bim", "foo bar"); # 59
b3381831 184 }
0dd3f902 185 is("@bee", "foo bar burbl blah"); # 60
b3381831 186 }
0dd3f902 187 is("@bee", "XXX bar burbl blah YYY"); # 61
b3381831 188 }
0dd3f902 189 is("@bee", "bar burbl blah"); # 62
b3381831 190 }
0dd3f902 191 is("@bee", "foo bar burbl blah"); # 63
b3381831
GS
192}
193
f17e6c41
RGS
194# try the same with our (except that previous values aren't restored)
195{
196 our @bee = @bee;
197 is("@bee", "foo bar burbl blah");
198 {
199 our (undef,@bee) = @bee;
200 is("@bee", "bar burbl blah");
201 {
202 our @bee = ('XXX',@bee,'YYY');
203 is("@bee", "XXX bar burbl blah YYY");
204 {
205 our @bee = our @bee = qw(foo bar burbl blah);
206 is("@bee", "foo bar burbl blah");
207 {
208 our (@bim) = our(@bee) = qw(foo bar);
209 is("@bee", "foo bar");
210 is("@bim", "foo bar");
211 }
212 }
213 }
214 }
215}
216
352edd90 217# make sure reification behaves
0dd3f902
NC
218my $t = curr_test();
219sub reify { $_[1] = $t++; print "@_\n"; }
352edd90
GS
220reify('ok');
221reify('ok');
9d001be8 222
0dd3f902 223curr_test($t);
7517970f 224
0dd3f902
NC
225# qw() is no longer a runtime split, it's compiletime.
226is (qw(foo bar snorfle)[2], 'snorfle');
7517970f 227
0dd3f902 228@ary = (12,23,34,45,56);
7517970f 229
0dd3f902
NC
230is(shift(@ary), 12);
231is(pop(@ary), 56);
232is(push(@ary,56), 4);
233is(unshift(@ary,12), 5);
4c8f17b9
BH
234
235sub foo { "a" }
236@foo=(foo())[0,0];
0dd3f902 237is ($foo[1], "a");
b0840a2a 238
6b42d12b
DM
239# bugid #15439 - clearing an array calls destructors which may try
240# to modify the array - caused 'Attempt to free unreferenced scalar'
241
242my $got = runperl (
243 prog => q{
244 sub X::DESTROY { @a = () }
3d7a9343 245 @a = (bless {}, q{X});
6b42d12b
DM
246 @a = ();
247 },
248 stderr => 1
249 );
250
251$got =~ s/\n/ /g;
0dd3f902 252is ($got, '');
2b573ace
JH
253
254# Test negative and funky indices.
255
0dd3f902 256
2b573ace
JH
257{
258 my @a = 0..4;
0dd3f902
NC
259 is($a[-1], 4);
260 is($a[-2], 3);
261 is($a[-5], 0);
262 ok(!defined $a[-6]);
263
264 is($a[2.1] , 2);
265 is($a[2.9] , 2);
266 is($a[undef], 0);
267 is($a["3rd"], 3);
2b573ace
JH
268}
269
2b573ace
JH
270
271{
272 my @a;
273 eval '$a[-1] = 0';
0dd3f902 274 like($@, qr/Modification of non-creatable array value attempted, subscript -1/, "\$a[-1] = 0");
2b573ace 275}
0dd3f902 276
1b20cd17
NC
277sub test_arylen {
278 my $ref = shift;
83bf042f 279 local $^W = 1;
1b20cd17 280 is ($$ref, undef, "\$# on freed array is undef");
83bf042f
NC
281 my @warn;
282 local $SIG{__WARN__} = sub {push @warn, "@_"};
1b20cd17 283 $$ref = 1000;
83bf042f
NC
284 is (scalar @warn, 1);
285 like ($warn[0], qr/^Attempt to set length of freed array/);
286}
1b20cd17
NC
287
288{
289 my $a = \$#{[]};
290 # Need a new statement to make it go out of scope
291 test_arylen ($a);
292 test_arylen (do {my @a; \$#a});
293}
404a4710
NC
294
295{
296 use vars '@array';
297
298 my $outer = \$#array;
299 is ($$outer, -1);
300 is (scalar @array, 0);
301
302 $$outer = 3;
303 is ($$outer, 3);
304 is (scalar @array, 4);
305
306 my $ref = \@array;
307
404a4710
NC
308 my $inner;
309 {
310 local @array;
311 $inner = \$#array;
312
313 is ($$inner, -1);
314 is (scalar @array, 0);
315 $$outer = 6;
316
317 is (scalar @$ref, 7);
318
319 is ($$inner, -1);
320 is (scalar @array, 0);
321
322 $$inner = 42;
323 }
324
325 is (scalar @array, 7);
326 is ($$outer, 6);
327
2fc04a10 328 is ($$inner, undef, "orphaned $#foo is always undef");
404a4710
NC
329
330 is (scalar @array, 7);
331 is ($$outer, 6);
332
333 $$inner = 1;
334
335 is (scalar @array, 7);
336 is ($$outer, 6);
337
338 $$inner = 503; # Bang!
339
340 is (scalar @array, 7);
341 is ($$outer, 6);
342}
343
344{
345 # Bug #36211
346 use vars '@array';
347 for (1,2) {
348 {
349 local @a;
350 is ($#a, -1);
351 @a=(1..4)
352 }
353 }
354}
355
e4c5ccf3
RH
356{
357 # Bug #37350
358 my @array = (1..4);
359 $#{@array} = 7;
360 is ($#{4}, 7);
361
362 my $x;
363 $#{$x} = 3;
364 is(scalar @$x, 4);
365
366 push @{@array}, 23;
367 is ($4[8], 23);
368}
369{
370 # Bug #37350 -- once more with a global
371 use vars '@array';
372 @array = (1..4);
373 $#{@array} = 7;
374 is ($#{4}, 7);
375
376 my $x;
377 $#{$x} = 3;
378 is(scalar @$x, 4);
379
380 push @{@array}, 23;
381 is ($4[8], 23);
382}
383
f17e6c41
RGS
384# more tests for AASSIGN_COMMON
385
386{
387 our($x,$y,$z) = (1..3);
388 our($y,$z) = ($x,$y);
389 is("$x $y $z", "1 1 2");
390}
391{
392 our($x,$y,$z) = (1..3);
393 (our $y, our $z) = ($x,$y);
394 is("$x $y $z", "1 1 2");
395}
8d38d4f3 396{
3023b5f3 397 # AASSIGN_COMMON detection with logical operators
8d38d4f3
GG
398 my $true = 1;
399 our($x,$y,$z) = (1..3);
400 (our $y, our $z) = $true && ($x,$y);
401 is("$x $y $z", "1 1 2");
402}
f17e6c41 403
0f907b96
FC
404# [perl #70171]
405{
406 my $x = get_x(); my %x = %$x; sub get_x { %x=(1..4); return \%x };
407 is(
408 join(" ", map +($_,$x{$_}), sort keys %x), "1 2 3 4",
409 'bug 70171 (self-assignment via my %x = %$x)'
410 );
411 my $y = get_y(); my @y = @$y; sub get_y { @y=(1..4); return \@y };
412 is(
413 "@y", "1 2 3 4",
414 'bug 70171 (self-assignment via my @x = @$x)'
415 );
416}
417
ade1ceae
DM
418# [perl #70171], [perl #82110]
419{
420 my ($i, $ra, $rh);
421 again:
422 my @a = @$ra; # common assignment on 2nd attempt
423 my %h = %$rh; # common assignment on 2nd attempt
424 @a = qw(1 2 3 4);
425 %h = qw(a 1 b 2 c 3 d 4);
426 $ra = \@a;
427 $rh = \%h;
428 goto again unless $i++;
429
430 is("@a", "1 2 3 4",
431 'bug 70171 (self-assignment via my @x = @$x) - goto variant'
432 );
433 is(
434 join(" ", map +($_,$h{$_}), sort keys %h), "a 1 b 2 c 3 d 4",
435 'bug 70171 (self-assignment via my %x = %$x) - goto variant'
436 );
437}
438
439
8f878375
FC
440*trit = *scile; $trit[0];
441ok(1, 'aelem_fast on a nonexistent array does not crash');
f17e6c41 442
9f71cfe6
FC
443# [perl #107440]
444sub A::DESTROY { $::ra = 0 }
445$::ra = [ bless [], 'A' ];
446undef @$::ra;
447pass 'no crash when freeing array that is being undeffed';
448$::ra = [ bless [], 'A' ];
449@$::ra = ('a'..'z');
450pass 'no crash when freeing array that is being cleared';
451
70ce9249
FC
452# [perl #85670] Copying magic to elements
453SKIP: {
454 skip "no Scalar::Util::weaken on miniperl", 1, if is_miniperl;
455 require Scalar::Util;
456 package glelp {
457 Scalar::Util::weaken ($a = \@ISA);
458 @ISA = qw(Foo);
459 Scalar::Util::weaken ($a = \$ISA[0]);
460 ::is @ISA, 1, 'backref magic is not copied to elements';
461 }
462}
463package peen {
464 $#ISA = -1;
465 @ISA = qw(Foo);
466 $ISA[0] = qw(Sphare);
467
468 sub Sphare::pling { 'pling' }
469
470 ::is eval { pling peen }, 'pling',
471 'arylen_p magic does not stop isa magic from being copied';
472}
473
474
404a4710 475"We're included by lib/Tie/Array/std.t so we need to return something true";