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