This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Encode to CPAN version 2.78
[perl5.git] / t / op / push.t
CommitLineData
8d063cd8
LW
1#!./perl
2
e589c1fb
CK
3BEGIN {
4 chdir 't' if -d 't';
e589c1fb 5 require './test.pl';
43ece5b1 6 set_up_inc('../lib');
e589c1fb
CK
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
26230909 23plan tests => 8 + @tests*2;
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
cba5a3b0
DG
32# test autovivification
33push @$undef1, 1, 2, 3;
e589c1fb 34is( join(':',@$undef1), '1:2:3', 'autovivify array');
d4fc4415 35
cba5a3b0
DG
36# test implicit dereference errors
37eval "push 42, 0, 1, 2, 3";
e589c1fb 38like ( $@, qr/must be array/, 'push onto a literal integer');
cba5a3b0
DG
39
40$hashref = { };
26230909
AC
41eval q{ push $hashref, 0, 1, 2, 3 };
42like( $@, qr/Experimental push on scalar is now forbidden/, 'push onto a hashref');
cba5a3b0 43
26230909
AC
44eval q{ push bless([]), 0, 1, 2, 3 };
45like( $@, qr/Experimental push on scalar is now forbidden/, 'push onto a blessed array ref');
d4fc4415
FC
46
47$test = 13;
cba5a3b0
DG
48
49# test context
50{
51 my($first, $second) = ([1], [2]);
52 sub two_things { return +($first, $second) }
26230909 53 push @{ two_things() }, 3;
e589c1fb
CK
54 is( join(':',@$first), '1', "\$first = [ @$first ];");
55 is( join(':',@$second), '2:3', "\$second = [ @$second ]");
cba5a3b0
DG
56}
57
79a0689e
LW
58foreach $line (@tests) {
59 ($list,$get,$leave) = split(/,\t*/,$line);
79072805 60 ($pos, $len, @list) = split(' ',$list);
79a0689e
LW
61 @get = split(' ',$get);
62 @leave = split(' ',$leave);
63 @x = (0,1,2,3,4,5,6,7);
79072805
LW
64 if (defined $len) {
65 @got = splice(@x, $pos, $len, @list);
66 }
67 else {
68 @got = splice(@x, $pos);
69 }
e589c1fb
CK
70 is(join(':',@got), join(':',@get), "got: @got == @get");
71 is(join(':',@x), join(':',@leave), "left: @x == @leave");
79a0689e
LW
72}
73
a60c0954 741; # this file is require'd by lib/tie-stdpush.t