This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deprecate above \xFF in bitwise string ops
[perl5.git] / t / op / push.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require './test.pl';
6     set_up_inc('../lib');
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 => 10 + @tests*2;
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 autovivification
33 push @$undef1, 1, 2, 3;
34 is( join(':',@$undef1), '1:2:3', 'autovivify array');
35
36 # test implicit dereference errors
37 eval "push 42, 0, 1, 2, 3";
38 like ( $@, qr/must be array/, 'push onto a literal integer');
39
40 $hashref = { };
41 eval q{ push $hashref, 0, 1, 2, 3 };
42 like( $@, qr/Experimental push on scalar is now forbidden/, 'push onto a hashref');
43
44 eval q{ push bless([]), 0, 1, 2, 3 };
45 like( $@, qr/Experimental push on scalar is now forbidden/, 'push onto a blessed array ref');
46
47 $test = 13;
48
49 # test context
50 {
51     my($first, $second) = ([1], [2]);
52     sub two_things { return +($first, $second) }
53     push @{ two_things() }, 3;
54     is( join(':',@$first), '1', "\$first = [ @$first ];");
55     is( join(':',@$second), '2:3', "\$second = [ @$second ]");
56 }
57
58 foreach $line (@tests) {
59     ($list,$get,$leave) = split(/,\t*/,$line);
60     ($pos, $len, @list) = split(' ',$list);
61     @get = split(' ',$get);
62     @leave = split(' ',$leave);
63     @x = (0,1,2,3,4,5,6,7);
64     if (defined $len) {
65         @got = splice(@x, $pos, $len, @list);
66     }
67     else {
68         @got = splice(@x, $pos);
69     }
70     is(join(':',@got), join(':',@get),   "got: @got == @get");
71     is(join(':',@x),   join(':',@leave), "left: @x == @leave");
72 }
73
74 # See RT#131000
75 {
76     local $@;
77     my @readonly_array = 10..11;
78     Internals::SvREADONLY(@readonly_array, 1);
79     eval { push @readonly_array, () };
80     is $@, '', "can push empty list onto readonly array";
81
82     eval { push @readonly_array, 9 };
83     like $@, qr/^Modification of a read-only value/,
84         "croak when pushing onto readonly array";
85 }
86
87 1;  # this file is require'd by lib/tie-stdpush.t