This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / splice.t
CommitLineData
48cdf507
GA
1#!./perl
2
ce6d40e0
CK
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
9$| = 1;
48cdf507
GA
10
11@a = (1..10);
12
13sub j { join(":",@_) }
14
ce6d40e0
CK
15is( j(splice(@a,@a,0,11,12)), '', 'return value of splice when nothing is removed, only added');
16is( j(@a), j(1..12), '... added two elements');
48cdf507 17
ce6d40e0
CK
18is( j(splice(@a,-1)), "12", 'remove last element, return value');
19is( j(@a), j(1..11), '... removed last element');
48cdf507 20
ce6d40e0
CK
21is( j(splice(@a,0,1)), "1", 'remove first element, return value');
22is( j(@a), j(2..11), '... first element removed');
48cdf507 23
ce6d40e0
CK
24is( j(splice(@a,0,0,0,1)), "", 'emulate shift, return value is empty');
25is( j(@a), j(0..11), '... added two elements to beginning of the list');
48cdf507 26
ce6d40e0
CK
27is( j(splice(@a,5,1,5)), "5", 'remove and replace an element to the end of the list, return value is the element');
28is( j(@a), j(0..11), '... list remains the same');
48cdf507 29
ce6d40e0
CK
30is( j(splice(@a, @a, 0, 12, 13)), "", 'push two elements onto the end of the list, return value is empty');
31is( j(@a), j(0..13), '... added two elements to the end of the list');
48cdf507 32
ce6d40e0
CK
33is( j(splice(@a, -@a, @a, 1, 2, 3)), j(0..13), 'splice the whole list out, add 3 elements, return value is @a');
34is( j(@a), j(1..3), '... array only contains new elements');
48cdf507 35
ce6d40e0
CK
36is( j(splice(@a, 1, -1, 7, 7)), "2", 'replace middle element with two elements, negative offset, return value is the element' );
37is( j(@a), j(1,7,7,3), '... array 1,7,7,3');
48cdf507 38
ce6d40e0
CK
39is( j(splice(@a,-3,-2,2)), j(7), 'replace first 7 with a 2, negative offset, negative length, return value is 7');
40is( j(@a), j(1,2,7,3), '... array has 1,2,7,3');
f507e126
RS
41
42# Bug 20000223.001 - no test for splice(@array). Destructive test!
ce6d40e0
CK
43is( j(splice(@a)), j(1,2,7,3), 'bare splice empties the array, return value is the array');
44is( j(@a), '', 'array is empty');
f507e126 45
aae9faae
DL
46# Tests 11 and 12:
47# [ID 20010711.005] in Tie::Array, SPLICE ignores context, breaking SHIFT
48
49my $foo;
50
51@a = ('red', 'green', 'blue');
52$foo = splice @a, 1, 2;
ce6d40e0 53is( $foo, 'blue', 'remove a single element in scalar context');
aae9faae
DL
54
55@a = ('red', 'green', 'blue');
56$foo = shift @a;
ce6d40e0 57is( $foo, 'red', 'do the same with shift');
f507e126 58
50528de0
WL
59# Bug [perl #30568] - insertions of deleted elements
60@a = (1, 2, 3);
61splice( @a, 0, 3, $a[1], $a[0] );
ce6d40e0 62is( j(@a), j(2,1), 'splice and replace with indexes 1, 0');
50528de0
WL
63
64@a = (1, 2, 3);
65splice( @a, 0, 3 ,$a[0], $a[1] );
ce6d40e0 66is( j(@a), j(1,2), 'splice and replace with indexes 0, 1');
50528de0
WL
67
68@a = (1, 2, 3);
69splice( @a, 0, 3 ,$a[2], $a[1], $a[0] );
ce6d40e0 70is( j(@a), j(3,2,1), 'splice and replace with indexes 2, 1, 0');
50528de0
WL
71
72@a = (1, 2, 3);
73splice( @a, 0, 3, $a[0], $a[1], $a[2], $a[0], $a[1], $a[2] );
ce6d40e0 74is( j(@a), j(1,2,3,1,2,3), 'splice and replace with a whole bunch');
50528de0
WL
75
76@a = (1, 2, 3);
77splice( @a, 1, 2, $a[2], $a[1] );
ce6d40e0 78is( j(@a), j(1,3,2), 'swap last two elements');
50528de0
WL
79
80@a = (1, 2, 3);
81splice( @a, 1, 2, $a[1], $a[1] );
ce6d40e0 82is( j(@a), j(1,2,2), 'duplicate middle element on the end');
474af990
FR
83
84# splice should invoke get magic
85
ce6d40e0 86ok( ! Foo->isa('Bar'), 'Foo is not a Bar');
474af990
FR
87
88splice @Foo::ISA, 0, 0, 'Bar';
50b2b0fc 89ok( Foo->isa('Bar'), 'splice @ISA and make Foo a Bar');
cba5a3b0 90
d4fc4415 91# Test undef first arg
0953b66b 92eval { no warnings 'experimental';splice( $new_arrayref, 0, 0, 1, 2, 3 ) };
ce6d40e0
CK
93like($@, qr/Not an ARRAY/, 'undefined first argument to splice');
94
31c61add
FC
95# Test arrays with nonexistent elements (crashes when it fails)
96@a = ();
97$#a++;
98is sprintf("%s", splice @a, 0, 1), "",
99 'splice handles nonexistent elems when shrinking the array';
100@a = ();
101$#a++;
102is sprintf("%s", splice @a, 0, 1, undef), "",
103 'splice handles nonexistent elems when array len stays the same';
104
ce6d40e0 105done_testing;