Turn barewords into strings to run under strict subs.
[p5sagit/p5-mst-13.2.git] / t / op / array.t
CommitLineData
a687059c 1#!./perl
2
6b42d12b 3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
a835f506 8print "1..82\n";
a687059c 9
05fc92f1 10#
11# @foo, @bar, and @ary are also used from tie-stdarray after tie-ing them
12#
13
a687059c 14@ary = (1,2,3,4,5);
15if (join('',@ary) eq '12345') {print "ok 1\n";} else {print "not ok 1\n";}
16
17$tmp = $ary[$#ary]; --$#ary;
18if ($tmp == 5) {print "ok 2\n";} else {print "not ok 2\n";}
19if ($#ary == 3) {print "ok 3\n";} else {print "not ok 3\n";}
20if (join('',@ary) eq '1234') {print "ok 4\n";} else {print "not ok 4\n";}
21
22$[ = 1;
23@ary = (1,2,3,4,5);
24if (join('',@ary) eq '12345') {print "ok 5\n";} else {print "not ok 5\n";}
25
26$tmp = $ary[$#ary]; --$#ary;
27if ($tmp == 5) {print "ok 6\n";} else {print "not ok 6\n";}
28if ($#ary == 4) {print "ok 7\n";} else {print "not ok 7\n";}
29if (join('',@ary) eq '1234') {print "ok 8\n";} else {print "not ok 8\n";}
30
31if ($ary[5] eq '') {print "ok 9\n";} else {print "not ok 9\n";}
32
a0d0e21e 33$#ary += 1; # see if element 5 gone for good
a687059c 34if ($#ary == 5) {print "ok 10\n";} else {print "not ok 10\n";}
a0d0e21e 35if (defined $ary[5]) {print "not ok 11\n";} else {print "ok 11\n";}
a687059c 36
37$[ = 0;
38@foo = ();
39$r = join(',', $#foo, @foo);
40if ($r eq "-1") {print "ok 12\n";} else {print "not ok 12 $r\n";}
41$foo[0] = '0';
42$r = join(',', $#foo, @foo);
43if ($r eq "0,0") {print "ok 13\n";} else {print "not ok 13 $r\n";}
44$foo[2] = '2';
45$r = join(',', $#foo, @foo);
46if ($r eq "2,0,,2") {print "ok 14\n";} else {print "not ok 14 $r\n";}
47@bar = ();
48$bar[0] = '0';
49$bar[1] = '1';
50$r = join(',', $#bar, @bar);
51if ($r eq "1,0,1") {print "ok 15\n";} else {print "not ok 15 $r\n";}
52@bar = ();
53$r = join(',', $#bar, @bar);
54if ($r eq "-1") {print "ok 16\n";} else {print "not ok 16 $r\n";}
55$bar[0] = '0';
56$r = join(',', $#bar, @bar);
57if ($r eq "0,0") {print "ok 17\n";} else {print "not ok 17 $r\n";}
58$bar[2] = '2';
59$r = join(',', $#bar, @bar);
60if ($r eq "2,0,,2") {print "ok 18\n";} else {print "not ok 18 $r\n";}
61reset 'b';
62@bar = ();
63$bar[0] = '0';
64$r = join(',', $#bar, @bar);
65if ($r eq "0,0") {print "ok 19\n";} else {print "not ok 19 $r\n";}
66$bar[2] = '2';
67$r = join(',', $#bar, @bar);
68if ($r eq "2,0,,2") {print "ok 20\n";} else {print "not ok 20 $r\n";}
69
70$foo = 'now is the time';
71if (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/)) {
72 if ($F1 eq 'now' && $F2 eq 'is' && $Etc eq 'the time') {
73 print "ok 21\n";
74 }
75 else {
76 print "not ok 21\n";
77 }
78}
79else {
80 print "not ok 21\n";
81}
82
83$foo = 'lskjdf';
84if ($cnt = (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))) {
85 print "not ok 22 $cnt $F1:$F2:$Etc\n";
86}
87else {
88 print "ok 22\n";
89}
90
91%foo = ('blurfl','dyick','foo','bar','etc.','etc.');
92%bar = %foo;
93print $bar{'foo'} eq 'bar' ? "ok 23\n" : "not ok 23\n";
94%bar = ();
95print $bar{'foo'} eq '' ? "ok 24\n" : "not ok 24\n";
96(%bar,$a,$b) = (%foo,'how','now');
97print $bar{'foo'} eq 'bar' ? "ok 25\n" : "not ok 25\n";
98print $bar{'how'} eq 'now' ? "ok 26\n" : "not ok 26\n";
99@bar{keys %foo} = values %foo;
100print $bar{'foo'} eq 'bar' ? "ok 27\n" : "not ok 27\n";
101print $bar{'how'} eq 'now' ? "ok 28\n" : "not ok 28\n";
102
103@foo = grep(/e/,split(' ','now is the time for all good men to come to'));
104print join(' ',@foo) eq 'the time men come' ? "ok 29\n" : "not ok 29\n";
105
106@foo = grep(!/e/,split(' ','now is the time for all good men to come to'));
107print join(' ',@foo) eq 'now is for all good to to' ? "ok 30\n" : "not ok 30\n";
79a0689e 108
109$foo = join('',('a','b','c','d','e','f')[0..5]);
110print $foo eq 'abcdef' ? "ok 31\n" : "not ok 31\n";
111
112$foo = join('',('a','b','c','d','e','f')[0..1]);
113print $foo eq 'ab' ? "ok 32\n" : "not ok 32\n";
114
115$foo = join('',('a','b','c','d','e','f')[6]);
116print $foo eq '' ? "ok 33\n" : "not ok 33\n";
117
118@foo = ('a','b','c','d','e','f')[0,2,4];
119@bar = ('a','b','c','d','e','f')[1,3,5];
120$foo = join('',(@foo,@bar)[0..5]);
121print $foo eq 'acebdf' ? "ok 34\n" : "not ok 34\n";
122
123$foo = ('a','b','c','d','e','f')[0,2,4];
124print $foo eq 'e' ? "ok 35\n" : "not ok 35\n";
125
126$foo = ('a','b','c','d','e','f')[1];
127print $foo eq 'b' ? "ok 36\n" : "not ok 36\n";
a0231f0e 128
c6aa4a32 129@foo = ( 'foo', 'bar', 'burbl');
130push(foo, 'blah');
f1192cee 131print $#foo == 3 ? "ok 37\n" : "not ok 37\n";
b3381831 132
133# various AASSIGN_COMMON checks (see newASSIGNOP() in op.c)
134
135$test = 37;
136sub t { ++$test; print "not " unless $_[0]; print "ok $test\n"; }
137
138@foo = @foo;
139t("@foo" eq "foo bar burbl blah"); # 38
140
141(undef,@foo) = @foo;
142t("@foo" eq "bar burbl blah"); # 39
143
144@foo = ('XXX',@foo, 'YYY');
145t("@foo" eq "XXX bar burbl blah YYY"); # 40
146
3201ebbd 147@foo = @foo = qw(foo b\a\r bu\\rbl blah);
148t("@foo" eq 'foo b\a\r bu\\rbl blah'); # 41
b3381831 149
150@bar = @foo = qw(foo bar); # 42
151t("@foo" eq "foo bar");
152t("@bar" eq "foo bar"); # 43
153
154# try the same with local
05fc92f1 155# XXX tie-stdarray fails the tests involving local, so we use
156# different variable names to escape the 'tie'
157
158@bee = ( 'foo', 'bar', 'burbl', 'blah');
b3381831 159{
160
05fc92f1 161 local @bee = @bee;
162 t("@bee" eq "foo bar burbl blah"); # 44
b3381831 163 {
05fc92f1 164 local (undef,@bee) = @bee;
165 t("@bee" eq "bar burbl blah"); # 45
b3381831 166 {
05fc92f1 167 local @bee = ('XXX',@bee,'YYY');
168 t("@bee" eq "XXX bar burbl blah YYY"); # 46
b3381831 169 {
05fc92f1 170 local @bee = local(@bee) = qw(foo bar burbl blah);
171 t("@bee" eq "foo bar burbl blah"); # 47
b3381831 172 {
05fc92f1 173 local (@bim) = local(@bee) = qw(foo bar);
174 t("@bee" eq "foo bar"); # 48
175 t("@bim" eq "foo bar"); # 49
b3381831 176 }
05fc92f1 177 t("@bee" eq "foo bar burbl blah"); # 50
b3381831 178 }
05fc92f1 179 t("@bee" eq "XXX bar burbl blah YYY"); # 51
b3381831 180 }
05fc92f1 181 t("@bee" eq "bar burbl blah"); # 52
b3381831 182 }
05fc92f1 183 t("@bee" eq "foo bar burbl blah"); # 53
b3381831 184}
185
186# try the same with my
187{
188
05fc92f1 189 my @bee = @bee;
190 t("@bee" eq "foo bar burbl blah"); # 54
b3381831 191 {
05fc92f1 192 my (undef,@bee) = @bee;
193 t("@bee" eq "bar burbl blah"); # 55
b3381831 194 {
05fc92f1 195 my @bee = ('XXX',@bee,'YYY');
196 t("@bee" eq "XXX bar burbl blah YYY"); # 56
b3381831 197 {
05fc92f1 198 my @bee = my @bee = qw(foo bar burbl blah);
199 t("@bee" eq "foo bar burbl blah"); # 57
b3381831 200 {
05fc92f1 201 my (@bim) = my(@bee) = qw(foo bar);
202 t("@bee" eq "foo bar"); # 58
203 t("@bim" eq "foo bar"); # 59
b3381831 204 }
05fc92f1 205 t("@bee" eq "foo bar burbl blah"); # 60
b3381831 206 }
05fc92f1 207 t("@bee" eq "XXX bar burbl blah YYY"); # 61
b3381831 208 }
05fc92f1 209 t("@bee" eq "bar burbl blah"); # 62
b3381831 210 }
05fc92f1 211 t("@bee" eq "foo bar burbl blah"); # 63
b3381831 212}
213
352edd90 214# make sure reification behaves
215my $t = 63;
216sub reify { $_[1] = ++$t; print "@_\n"; }
217reify('ok');
218reify('ok');
9d001be8 219
220# qw() is no more a runtime split, it's compiletime.
221print "not " unless qw(foo bar snorfle)[2] eq 'snorfle';
222print "ok 66\n";
223
7517970f 224@ary = (12,23,34,45,56);
225
226print "not " unless shift(@ary) == 12;
227print "ok 67\n";
228
229print "not " unless pop(@ary) == 56;
230print "ok 68\n";
231
232print "not " unless push(@ary,56) == 4;
233print "ok 69\n";
234
235print "not " unless unshift(@ary,12) == 5;
236print "ok 70\n";
4c8f17b9 237
238sub foo { "a" }
239@foo=(foo())[0,0];
240$foo[1] eq "a" or print "not ";
241print "ok 71\n";
b0840a2a 242
243# $[ should have the same effect regardless of whether the aelem
244# op is optimized to aelemfast.
245
246sub tary {
247 local $[ = 10;
248 my $five = 5;
249 print "not " unless $tary[5] == $tary[$five];
250 print "ok 72\n";
251}
252
253@tary = (0..50);
254tary();
6b42d12b 255
256
257require './test.pl';
258
259# bugid #15439 - clearing an array calls destructors which may try
260# to modify the array - caused 'Attempt to free unreferenced scalar'
261
262my $got = runperl (
263 prog => q{
264 sub X::DESTROY { @a = () }
265 @a = (bless {}, 'X');
266 @a = ();
267 },
268 stderr => 1
269 );
270
271$got =~ s/\n/ /g;
272print "# $got\nnot " unless $got eq '';
273print "ok 73\n";
2b573ace 274
275# Test negative and funky indices.
276
277{
278 my @a = 0..4;
279 print $a[-1] == 4 ? "ok 74\n" : "not ok 74\n";
280 print $a[-2] == 3 ? "ok 75\n" : "not ok 75\n";
281 print $a[-5] == 0 ? "ok 76\n" : "not ok 76\n";
282 print defined $a[-6] ? "not ok 77\n" : "ok 77\n";
283
284 print $a[2.1] == 2 ? "ok 78\n" : "not ok 78\n";
285 print $a[2.9] == 2 ? "ok 79\n" : "not ok 79\n";
286 print $a[undef] == 0 ? "ok 80\n" : "not ok 80\n";
287 print $a["3rd"] == 3 ? "ok 81\n" : "not ok 81\n";
288}
289
290sub kindalike { # TODO: test.pl-ize the array.t.
291 my ($s, $r, $m, $n) = @_;
292 print $s =~ /$r/ ? "ok $n - $m\n" : "not ok $n - $m ($s)\n";
293}
294
295{
296 my @a;
297 eval '$a[-1] = 0';
298 kindalike($@, qr/Modification of non-creatable array value attempted, subscript -1/, "\$a[-1] = 0", 82);
299}