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