This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/fork.t: Up the sleep time in a test to avoid timing issues
[perl5.git] / t / op / push.t
CommitLineData
8d063cd8
LW
1#!./perl
2
e589c1fb
CK
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
79a0689e
LW
9@tests = split(/\n/, <<EOF);
100 3, 0 1 2, 3 4 5 6 7
110 0 a b c, , a b c 0 1 2 3 4 5 6 7
128 0 a b c, , 0 1 2 3 4 5 6 7 a b c
137 0 6.5, , 0 1 2 3 4 5 6 6.5 7
141 0 a b c d e f g h i j,, 0 a b c d e f g h i j 1 2 3 4 5 6 7
150 1 a, 0, a 1 2 3 4 5 6 7
161 6 x y z, 1 2 3 4 5 6, 0 x y z 7
170 7 x y z, 0 1 2 3 4 5 6, x y z 7
181 7 x y z, 1 2 3 4 5 6 7, 0 x y z
194, 4 5 6 7, 0 1 2 3
20-4, 4 5 6 7, 0 1 2 3
21EOF
22
e589c1fb 23plan tests => 16 + @tests*4;
79a0689e 24die "blech" unless @tests;
8d063cd8
LW
25
26@x = (1,2,3);
27push(@x,@x);
e589c1fb 28is( join(':',@x), '1:2:3:1:2:3', 'push array onto array');
a60c0954 29push(@x,4);
e589c1fb 30is( join(':',@x), '1:2:3:1:2:3:4', 'push integer onto array');
79a0689e 31
c6aa4a32 32# test for push/pop intuiting @ on array
35ea4f2c
NC
33{
34 no warnings 'deprecated';
35 push(x,3);
36}
e589c1fb 37is( join(':',@x), '1:2:3:1:2:3:4:3', 'push intuiting @ on array');
35ea4f2c
NC
38{
39 no warnings 'deprecated';
40 pop(x);
41}
e589c1fb 42is( join(':',@x), '1:2:3:1:2:3:4', 'pop intuiting @ on array');
c6aa4a32 43
d401967c 44no warnings 'experimental::autoderef';
0953b66b 45
cba5a3b0
DG
46# test for push/pop on arrayref
47push(\@x,5);
e589c1fb 48is( join(':',@x), '1:2:3:1:2:3:4:5', 'push arrayref');
cba5a3b0 49pop(\@x);
e589c1fb 50is( join(':',@x), '1:2:3:1:2:3:4', 'pop arrayref');
cba5a3b0
DG
51
52# test autovivification
53push @$undef1, 1, 2, 3;
e589c1fb 54is( join(':',@$undef1), '1:2:3', 'autovivify array');
d4fc4415
FC
55
56# test push on undef (error)
57eval { push $undef2, 1, 2, 3 };
e589c1fb 58like( $@, qr/Not an ARRAY/, 'push on undef generates an error');
cba5a3b0
DG
59
60# test constant
61use constant CONST_ARRAYREF => [qw/a b c/];
62push CONST_ARRAYREF(), qw/d e f/;
e589c1fb 63is( join(':',@{CONST_ARRAYREF()}), 'a:b:c:d:e:f', 'test constant');
cba5a3b0
DG
64
65# test implicit dereference errors
66eval "push 42, 0, 1, 2, 3";
e589c1fb 67like ( $@, qr/must be array/, 'push onto a literal integer');
cba5a3b0
DG
68
69$hashref = { };
70eval { push $hashref, 0, 1, 2, 3 };
e589c1fb 71like( $@, qr/Not an ARRAY reference/, 'push onto a hashref');
cba5a3b0 72
d4fc4415 73eval { push bless([]), 0, 1, 2, 3 };
e589c1fb 74like( $@, qr/Not an unblessed ARRAY reference/, 'push onto a blessed array ref');
d4fc4415
FC
75
76$test = 13;
cba5a3b0
DG
77
78# test context
79{
80 my($first, $second) = ([1], [2]);
81 sub two_things { return +($first, $second) }
82 push two_things(), 3;
e589c1fb
CK
83 is( join(':',@$first), '1', "\$first = [ @$first ];");
84 is( join(':',@$second), '2:3', "\$second = [ @$second ]");
cba5a3b0
DG
85
86 push @{ two_things() }, 4;
e589c1fb
CK
87 is( join(':',@$first), '1', "\$first = [ @$first ];");
88 is( join(':',@$second), '2:3:4', "\$second = [ @$second ]");
cba5a3b0
DG
89}
90
79a0689e
LW
91foreach $line (@tests) {
92 ($list,$get,$leave) = split(/,\t*/,$line);
79072805 93 ($pos, $len, @list) = split(' ',$list);
79a0689e
LW
94 @get = split(' ',$get);
95 @leave = split(' ',$leave);
96 @x = (0,1,2,3,4,5,6,7);
cba5a3b0 97 $y = [0,1,2,3,4,5,6,7];
79072805
LW
98 if (defined $len) {
99 @got = splice(@x, $pos, $len, @list);
cba5a3b0 100 @got2 = splice($y, $pos, $len, @list);
79072805
LW
101 }
102 else {
103 @got = splice(@x, $pos);
cba5a3b0 104 @got2 = splice($y, $pos);
79072805 105 }
e589c1fb
CK
106 is(join(':',@got), join(':',@get), "got: @got == @get");
107 is(join(':',@x), join(':',@leave), "left: @x == @leave");
108 is(join(':',@got2), join(':',@get), "ref got: @got2 == @get");
109 is(join(':',@$y), join(':',@leave), "ref left: @$y == @leave");
79a0689e
LW
110}
111
a60c0954 1121; # this file is require'd by lib/tie-stdpush.t