This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pack c/C on inf/nan.
[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 => 14 + @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 no warnings 'experimental::autoderef';
33
34 # test for push/pop on arrayref
35 push(\@x,5);
36 is( join(':',@x), '1:2:3:1:2:3:4:5', 'push arrayref');
37 pop(\@x);
38 is( join(':',@x), '1:2:3:1:2:3:4', 'pop arrayref');
39
40 # test autovivification
41 push @$undef1, 1, 2, 3;
42 is( join(':',@$undef1), '1:2:3', 'autovivify array');
43
44 # test push on undef (error)
45 eval { push $undef2, 1, 2, 3 };
46 like( $@, qr/Not an ARRAY/, 'push on undef generates an error');
47
48 # test constant
49 use constant CONST_ARRAYREF => [qw/a b c/];
50 push CONST_ARRAYREF(), qw/d e f/;
51 is( join(':',@{CONST_ARRAYREF()}), 'a:b:c:d:e:f', 'test constant');
52
53 # test implicit dereference errors
54 eval "push 42, 0, 1, 2, 3";
55 like ( $@, qr/must be array/, 'push onto a literal integer');
56
57 $hashref = { };
58 eval { push $hashref, 0, 1, 2, 3 };
59 like( $@, qr/Not an ARRAY reference/, 'push onto a hashref');
60
61 eval { push bless([]), 0, 1, 2, 3 };
62 like( $@, qr/Not an unblessed ARRAY reference/, 'push onto a blessed array ref');
63
64 $test = 13;
65
66 # test context
67 {
68     my($first, $second) = ([1], [2]);
69     sub two_things { return +($first, $second) }
70     push two_things(), 3;
71     is( join(':',@$first), '1', "\$first = [ @$first ];");
72     is( join(':',@$second), '2:3', "\$second = [ @$second ]");
73
74     push @{ two_things() }, 4;
75     is( join(':',@$first), '1', "\$first = [ @$first ];");
76     is( join(':',@$second), '2:3:4', "\$second = [ @$second ]");
77 }
78
79 foreach $line (@tests) {
80     ($list,$get,$leave) = split(/,\t*/,$line);
81     ($pos, $len, @list) = split(' ',$list);
82     @get = split(' ',$get);
83     @leave = split(' ',$leave);
84     @x = (0,1,2,3,4,5,6,7);
85     $y = [0,1,2,3,4,5,6,7];
86     if (defined $len) {
87         @got = splice(@x, $pos, $len, @list);
88         @got2 = splice($y, $pos, $len, @list);
89     }
90     else {
91         @got = splice(@x, $pos);
92         @got2 = splice($y, $pos);
93     }
94     is(join(':',@got), join(':',@get),   "got: @got == @get");
95     is(join(':',@x),   join(':',@leave), "left: @x == @leave");
96     is(join(':',@got2), join(':',@get),   "ref got: @got2 == @get");
97     is(join(':',@$y),   join(':',@leave), "ref left: @$y == @leave");
98 }
99
100 1;  # this file is require'd by lib/tie-stdpush.t