This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip over state declarations at run time
[perl5.git] / t / op / unshift.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require "./test.pl";
6 }
7
8 plan(36);
9
10 @array = (1, 2, 3);
11 $aref  = [1, 2, 3];
12
13 no warnings 'experimental::autoderef';
14 {
15     no warnings 'syntax';
16     $count3 = unshift (@array);
17     $count3r = unshift ($aref);
18 }
19 is(join(' ',@array), '1 2 3', 'unshift null');
20 cmp_ok($count3, '==', 3, 'unshift count == 3');
21 is(join(' ',@$aref), '1 2 3', 'unshift null (ref)');
22 cmp_ok($count3r, '==', 3, 'unshift count == 3 (ref)');
23
24
25 $count3_2 = unshift (@array, ());
26 is(join(' ',@array), '1 2 3', 'unshift null empty');
27 cmp_ok($count3_2, '==', 3, 'unshift count == 3 again');
28 $count3_2r = unshift ($aref, ());
29 is(join(' ',@$aref), '1 2 3', 'unshift null empty (ref)');
30 cmp_ok($count3_2r, '==', 3, 'unshift count == 3 again (ref)');
31
32 $count4 = unshift (@array, 0);
33 is(join(' ',@array), '0 1 2 3', 'unshift singleton list');
34 cmp_ok($count4, '==', 4, 'unshift count == 4');
35 $count4r = unshift ($aref, 0);
36 is(join(' ',@$aref), '0 1 2 3', 'unshift singleton list (ref)');
37 cmp_ok($count4r, '==', 4, 'unshift count == 4 (ref)');
38
39 $count7 = unshift (@array, 3, 2, 1);
40 is(join(' ',@array), '3 2 1 0 1 2 3', 'unshift list');
41 cmp_ok($count7, '==', 7, 'unshift count == 7');
42 $count7r = unshift ($aref, 3, 2, 1);
43 is(join(' ',@$aref), '3 2 1 0 1 2 3', 'unshift list (ref)');
44 cmp_ok($count7r, '==', 7, 'unshift count == 7 (ref)');
45
46 @list = (5, 4);
47 $count9 = unshift (@array, @list);
48 is(join(' ',@array), '5 4 3 2 1 0 1 2 3', 'unshift array');
49 cmp_ok($count9, '==', 9, 'unshift count == 9');
50 $count9r = unshift ($aref, @list);
51 is(join(' ',@$aref), '5 4 3 2 1 0 1 2 3', 'unshift array (ref)');
52 cmp_ok($count9r, '==', 9, 'unshift count == 9 (ref)');
53
54
55 @list = (7);
56 @list2 = (6);
57 $count11 = unshift (@array, @list, @list2);
58 is(join(' ',@array), '7 6 5 4 3 2 1 0 1 2 3', 'unshift arrays');
59 cmp_ok($count11, '==', 11, 'unshift count == 11');
60 $count11r = unshift ($aref, @list, @list2);
61 is(join(' ',@$aref), '7 6 5 4 3 2 1 0 1 2 3', 'unshift arrays (ref)');
62 cmp_ok($count11r, '==', 11, 'unshift count == 11 (ref)');
63
64 # ignoring counts
65 @alpha = ('y', 'z');
66 $alpharef = ['y', 'z'];
67
68 {
69     no warnings 'syntax';
70     unshift (@alpha);
71     unshift ($alpharef);
72 }
73 is(join(' ',@alpha), 'y z', 'void unshift null');
74 is(join(' ',@$alpharef), 'y z', 'void unshift null (ref)');
75
76 unshift (@alpha, ());
77 is(join(' ',@alpha), 'y z', 'void unshift null empty');
78 unshift ($alpharef, ());
79 is(join(' ',@$alpharef), 'y z', 'void unshift null empty (ref)');
80
81 unshift (@alpha, 'x');
82 is(join(' ',@alpha), 'x y z', 'void unshift singleton list');
83 unshift ($alpharef, 'x');
84 is(join(' ',@$alpharef), 'x y z', 'void unshift singleton list (ref)');
85
86 unshift (@alpha, 'u', 'v', 'w');
87 is(join(' ',@alpha), 'u v w x y z', 'void unshift list');
88 unshift ($alpharef, 'u', 'v', 'w');
89 is(join(' ',@$alpharef), 'u v w x y z', 'void unshift list (ref)');
90
91 @bet = ('s', 't');
92 unshift (@alpha, @bet);
93 is(join(' ',@alpha), 's t u v w x y z', 'void unshift array');
94 unshift ($alpharef, @bet);
95 is(join(' ',@$alpharef), 's t u v w x y z', 'void unshift array (ref)');
96
97 @bet = ('q');
98 @gimel = ('r');
99 unshift (@alpha, @bet, @gimel);
100 is(join(' ',@alpha), 'q r s t u v w x y z', 'void unshift arrays');
101 unshift ($alpharef, @bet, @gimel);
102 is(join(' ',@$alpharef), 'q r s t u v w x y z', 'void unshift arrays (ref)');
103