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
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require './test.pl';
7 }
8
9 @tests = split(/\n/, <<EOF);
10 0 3,                    0 1 2,          3 4 5 6 7
11 0 0 a b c,              ,               a b c 0 1 2 3 4 5 6 7
12 8 0 a b c,              ,               0 1 2 3 4 5 6 7 a b c
13 7 0 6.5,                ,               0 1 2 3 4 5 6 6.5 7
14 1 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
15 0 1 a,                  0,              a 1 2 3 4 5 6 7
16 1 6 x y z,              1 2 3 4 5 6,    0 x y z 7
17 0 7 x y z,              0 1 2 3 4 5 6,  x y z 7
18 1 7 x y z,              1 2 3 4 5 6 7,  0 x y z
19 4,                      4 5 6 7,        0 1 2 3
20 -4,                     4 5 6 7,        0 1 2 3
21 EOF
22
23 plan tests => 16 + @tests*4;
24 die "blech" unless @tests;
25
26 @x = (1,2,3);
27 push(@x,@x);
28 is( join(':',@x), '1:2:3:1:2:3', 'push array onto array');
29 push(@x,4);
30 is( join(':',@x), '1:2:3:1:2:3:4', 'push integer onto array');
31
32 # test for push/pop intuiting @ on array
33 {
34     no warnings 'deprecated';
35     push(x,3);
36 }
37 is( join(':',@x), '1:2:3:1:2:3:4:3', 'push intuiting @ on array');
38 {
39     no warnings 'deprecated';
40     pop(x);
41 }
42 is( join(':',@x), '1:2:3:1:2:3:4', 'pop intuiting @ on array');
43
44 no warnings 'experimental::autoderef';
45
46 # test for push/pop on arrayref
47 push(\@x,5);
48 is( join(':',@x), '1:2:3:1:2:3:4:5', 'push arrayref');
49 pop(\@x);
50 is( join(':',@x), '1:2:3:1:2:3:4', 'pop arrayref');
51
52 # test autovivification
53 push @$undef1, 1, 2, 3;
54 is( join(':',@$undef1), '1:2:3', 'autovivify array');
55
56 # test push on undef (error)
57 eval { push $undef2, 1, 2, 3 };
58 like( $@, qr/Not an ARRAY/, 'push on undef generates an error');
59
60 # test constant
61 use constant CONST_ARRAYREF => [qw/a b c/];
62 push CONST_ARRAYREF(), qw/d e f/;
63 is( join(':',@{CONST_ARRAYREF()}), 'a:b:c:d:e:f', 'test constant');
64
65 # test implicit dereference errors
66 eval "push 42, 0, 1, 2, 3";
67 like ( $@, qr/must be array/, 'push onto a literal integer');
68
69 $hashref = { };
70 eval { push $hashref, 0, 1, 2, 3 };
71 like( $@, qr/Not an ARRAY reference/, 'push onto a hashref');
72
73 eval { push bless([]), 0, 1, 2, 3 };
74 like( $@, qr/Not an unblessed ARRAY reference/, 'push onto a blessed array ref');
75
76 $test = 13;
77
78 # test context
79 {
80     my($first, $second) = ([1], [2]);
81     sub two_things { return +($first, $second) }
82     push two_things(), 3;
83     is( join(':',@$first), '1', "\$first = [ @$first ];");
84     is( join(':',@$second), '2:3', "\$second = [ @$second ]");
85
86     push @{ two_things() }, 4;
87     is( join(':',@$first), '1', "\$first = [ @$first ];");
88     is( join(':',@$second), '2:3:4', "\$second = [ @$second ]");
89 }
90
91 foreach $line (@tests) {
92     ($list,$get,$leave) = split(/,\t*/,$line);
93     ($pos, $len, @list) = split(' ',$list);
94     @get = split(' ',$get);
95     @leave = split(' ',$leave);
96     @x = (0,1,2,3,4,5,6,7);
97     $y = [0,1,2,3,4,5,6,7];
98     if (defined $len) {
99         @got = splice(@x, $pos, $len, @list);
100         @got2 = splice($y, $pos, $len, @list);
101     }
102     else {
103         @got = splice(@x, $pos);
104         @got2 = splice($y, $pos);
105     }
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");
110 }
111
112 1;  # this file is require'd by lib/tie-stdpush.t