xref: /openbsd/gnu/usr.bin/perl/t/op/push.t (revision 9f11ffb7)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc('../lib');
7}
8
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
23plan tests => 10 + @tests*2;
24die "blech" unless @tests;
25
26@x = (1,2,3);
27push(@x,@x);
28is( join(':',@x), '1:2:3:1:2:3', 'push array onto array');
29push(@x,4);
30is( join(':',@x), '1:2:3:1:2:3:4', 'push integer onto array');
31
32# test autovivification
33push @$undef1, 1, 2, 3;
34is( join(':',@$undef1), '1:2:3', 'autovivify array');
35
36# test implicit dereference errors
37eval "push 42, 0, 1, 2, 3";
38like ( $@, qr/must be array/, 'push onto a literal integer');
39
40$hashref = { };
41eval q{ push $hashref, 0, 1, 2, 3 };
42like( $@, qr/Experimental push on scalar is now forbidden/, 'push onto a hashref');
43
44eval q{ push bless([]), 0, 1, 2, 3 };
45like( $@, 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
58foreach $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
871;  # this file is require'd by lib/tie-stdpush.t
88