This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Quick integration of mainline changes to date
[perl5.git] / t / op / array.t
CommitLineData
a687059c
LW
1#!./perl
2
146174a9 3print "1..66\n";
a687059c 4
05fc92f1
GS
5#
6# @foo, @bar, and @ary are also used from tie-stdarray after tie-ing them
7#
8
a687059c
LW
9@ary = (1,2,3,4,5);
10if (join('',@ary) eq '12345') {print "ok 1\n";} else {print "not ok 1\n";}
11
12$tmp = $ary[$#ary]; --$#ary;
13if ($tmp == 5) {print "ok 2\n";} else {print "not ok 2\n";}
14if ($#ary == 3) {print "ok 3\n";} else {print "not ok 3\n";}
15if (join('',@ary) eq '1234') {print "ok 4\n";} else {print "not ok 4\n";}
16
17$[ = 1;
18@ary = (1,2,3,4,5);
19if (join('',@ary) eq '12345') {print "ok 5\n";} else {print "not ok 5\n";}
20
21$tmp = $ary[$#ary]; --$#ary;
22if ($tmp == 5) {print "ok 6\n";} else {print "not ok 6\n";}
23if ($#ary == 4) {print "ok 7\n";} else {print "not ok 7\n";}
24if (join('',@ary) eq '1234') {print "ok 8\n";} else {print "not ok 8\n";}
25
26if ($ary[5] eq '') {print "ok 9\n";} else {print "not ok 9\n";}
27
a0d0e21e 28$#ary += 1; # see if element 5 gone for good
a687059c 29if ($#ary == 5) {print "ok 10\n";} else {print "not ok 10\n";}
a0d0e21e 30if (defined $ary[5]) {print "not ok 11\n";} else {print "ok 11\n";}
a687059c
LW
31
32$[ = 0;
33@foo = ();
34$r = join(',', $#foo, @foo);
35if ($r eq "-1") {print "ok 12\n";} else {print "not ok 12 $r\n";}
36$foo[0] = '0';
37$r = join(',', $#foo, @foo);
38if ($r eq "0,0") {print "ok 13\n";} else {print "not ok 13 $r\n";}
39$foo[2] = '2';
40$r = join(',', $#foo, @foo);
41if ($r eq "2,0,,2") {print "ok 14\n";} else {print "not ok 14 $r\n";}
42@bar = ();
43$bar[0] = '0';
44$bar[1] = '1';
45$r = join(',', $#bar, @bar);
46if ($r eq "1,0,1") {print "ok 15\n";} else {print "not ok 15 $r\n";}
47@bar = ();
48$r = join(',', $#bar, @bar);
49if ($r eq "-1") {print "ok 16\n";} else {print "not ok 16 $r\n";}
50$bar[0] = '0';
51$r = join(',', $#bar, @bar);
52if ($r eq "0,0") {print "ok 17\n";} else {print "not ok 17 $r\n";}
53$bar[2] = '2';
54$r = join(',', $#bar, @bar);
55if ($r eq "2,0,,2") {print "ok 18\n";} else {print "not ok 18 $r\n";}
56reset 'b';
57@bar = ();
58$bar[0] = '0';
59$r = join(',', $#bar, @bar);
60if ($r eq "0,0") {print "ok 19\n";} else {print "not ok 19 $r\n";}
61$bar[2] = '2';
62$r = join(',', $#bar, @bar);
63if ($r eq "2,0,,2") {print "ok 20\n";} else {print "not ok 20 $r\n";}
64
65$foo = 'now is the time';
66if (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/)) {
67 if ($F1 eq 'now' && $F2 eq 'is' && $Etc eq 'the time') {
68 print "ok 21\n";
69 }
70 else {
71 print "not ok 21\n";
72 }
73}
74else {
75 print "not ok 21\n";
76}
77
78$foo = 'lskjdf';
79if ($cnt = (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))) {
80 print "not ok 22 $cnt $F1:$F2:$Etc\n";
81}
82else {
83 print "ok 22\n";
84}
85
86%foo = ('blurfl','dyick','foo','bar','etc.','etc.');
87%bar = %foo;
88print $bar{'foo'} eq 'bar' ? "ok 23\n" : "not ok 23\n";
89%bar = ();
90print $bar{'foo'} eq '' ? "ok 24\n" : "not ok 24\n";
91(%bar,$a,$b) = (%foo,'how','now');
92print $bar{'foo'} eq 'bar' ? "ok 25\n" : "not ok 25\n";
93print $bar{'how'} eq 'now' ? "ok 26\n" : "not ok 26\n";
94@bar{keys %foo} = values %foo;
95print $bar{'foo'} eq 'bar' ? "ok 27\n" : "not ok 27\n";
96print $bar{'how'} eq 'now' ? "ok 28\n" : "not ok 28\n";
97
98@foo = grep(/e/,split(' ','now is the time for all good men to come to'));
99print join(' ',@foo) eq 'the time men come' ? "ok 29\n" : "not ok 29\n";
100
101@foo = grep(!/e/,split(' ','now is the time for all good men to come to'));
102print join(' ',@foo) eq 'now is for all good to to' ? "ok 30\n" : "not ok 30\n";
79a0689e
LW
103
104$foo = join('',('a','b','c','d','e','f')[0..5]);
105print $foo eq 'abcdef' ? "ok 31\n" : "not ok 31\n";
106
107$foo = join('',('a','b','c','d','e','f')[0..1]);
108print $foo eq 'ab' ? "ok 32\n" : "not ok 32\n";
109
110$foo = join('',('a','b','c','d','e','f')[6]);
111print $foo eq '' ? "ok 33\n" : "not ok 33\n";
112
113@foo = ('a','b','c','d','e','f')[0,2,4];
114@bar = ('a','b','c','d','e','f')[1,3,5];
115$foo = join('',(@foo,@bar)[0..5]);
116print $foo eq 'acebdf' ? "ok 34\n" : "not ok 34\n";
117
118$foo = ('a','b','c','d','e','f')[0,2,4];
119print $foo eq 'e' ? "ok 35\n" : "not ok 35\n";
120
121$foo = ('a','b','c','d','e','f')[1];
122print $foo eq 'b' ? "ok 36\n" : "not ok 36\n";
a0231f0e 123
c6aa4a32
SP
124@foo = ( 'foo', 'bar', 'burbl');
125push(foo, 'blah');
f1192cee 126print $#foo == 3 ? "ok 37\n" : "not ok 37\n";
b3381831
GS
127
128# various AASSIGN_COMMON checks (see newASSIGNOP() in op.c)
129
130$test = 37;
131sub t { ++$test; print "not " unless $_[0]; print "ok $test\n"; }
132
133@foo = @foo;
134t("@foo" eq "foo bar burbl blah"); # 38
135
136(undef,@foo) = @foo;
137t("@foo" eq "bar burbl blah"); # 39
138
139@foo = ('XXX',@foo, 'YYY');
140t("@foo" eq "XXX bar burbl blah YYY"); # 40
141
142@foo = @foo = qw(foo bar burbl blah);
143t("@foo" eq "foo bar burbl blah"); # 41
144
145@bar = @foo = qw(foo bar); # 42
146t("@foo" eq "foo bar");
147t("@bar" eq "foo bar"); # 43
148
149# try the same with local
05fc92f1
GS
150# XXX tie-stdarray fails the tests involving local, so we use
151# different variable names to escape the 'tie'
152
153@bee = ( 'foo', 'bar', 'burbl', 'blah');
b3381831
GS
154{
155
05fc92f1
GS
156 local @bee = @bee;
157 t("@bee" eq "foo bar burbl blah"); # 44
b3381831 158 {
05fc92f1
GS
159 local (undef,@bee) = @bee;
160 t("@bee" eq "bar burbl blah"); # 45
b3381831 161 {
05fc92f1
GS
162 local @bee = ('XXX',@bee,'YYY');
163 t("@bee" eq "XXX bar burbl blah YYY"); # 46
b3381831 164 {
05fc92f1
GS
165 local @bee = local(@bee) = qw(foo bar burbl blah);
166 t("@bee" eq "foo bar burbl blah"); # 47
b3381831 167 {
05fc92f1
GS
168 local (@bim) = local(@bee) = qw(foo bar);
169 t("@bee" eq "foo bar"); # 48
170 t("@bim" eq "foo bar"); # 49
b3381831 171 }
05fc92f1 172 t("@bee" eq "foo bar burbl blah"); # 50
b3381831 173 }
05fc92f1 174 t("@bee" eq "XXX bar burbl blah YYY"); # 51
b3381831 175 }
05fc92f1 176 t("@bee" eq "bar burbl blah"); # 52
b3381831 177 }
05fc92f1 178 t("@bee" eq "foo bar burbl blah"); # 53
b3381831
GS
179}
180
181# try the same with my
182{
183
05fc92f1
GS
184 my @bee = @bee;
185 t("@bee" eq "foo bar burbl blah"); # 54
b3381831 186 {
05fc92f1
GS
187 my (undef,@bee) = @bee;
188 t("@bee" eq "bar burbl blah"); # 55
b3381831 189 {
05fc92f1
GS
190 my @bee = ('XXX',@bee,'YYY');
191 t("@bee" eq "XXX bar burbl blah YYY"); # 56
b3381831 192 {
05fc92f1
GS
193 my @bee = my @bee = qw(foo bar burbl blah);
194 t("@bee" eq "foo bar burbl blah"); # 57
b3381831 195 {
05fc92f1
GS
196 my (@bim) = my(@bee) = qw(foo bar);
197 t("@bee" eq "foo bar"); # 58
198 t("@bim" eq "foo bar"); # 59
b3381831 199 }
05fc92f1 200 t("@bee" eq "foo bar burbl blah"); # 60
b3381831 201 }
05fc92f1 202 t("@bee" eq "XXX bar burbl blah YYY"); # 61
b3381831 203 }
05fc92f1 204 t("@bee" eq "bar burbl blah"); # 62
b3381831 205 }
05fc92f1 206 t("@bee" eq "foo bar burbl blah"); # 63
b3381831
GS
207}
208
352edd90
GS
209# make sure reification behaves
210my $t = 63;
211sub reify { $_[1] = ++$t; print "@_\n"; }
212reify('ok');
213reify('ok');
146174a9
CB
214
215# qw() is no more a runtime split, it's compiletime.
216print "not " unless qw(foo bar snorfle)[2] eq 'snorfle';
217print "ok 66\n";
218