source: trunk/essentials/dev-lang/perl/t/cmd/for.t@ 3315

Last change on this file since 3315 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 9.5 KB
Line 
1#!./perl
2
3print "1..78\n";
4
5for ($i = 0; $i <= 10; $i++) {
6 $x[$i] = $i;
7}
8$y = $x[10];
9print "#1 :$y: eq :10:\n";
10$y = join(' ', @x);
11print "#1 :$y: eq :0 1 2 3 4 5 6 7 8 9 10:\n";
12if (join(' ', @x) eq '0 1 2 3 4 5 6 7 8 9 10') {
13 print "ok 1\n";
14} else {
15 print "not ok 1\n";
16}
17
18$i = $c = 0;
19for (;;) {
20 $c++;
21 last if $i++ > 10;
22}
23if ($c == 12) {print "ok 2\n";} else {print "not ok 2\n";}
24
25$foo = 3210;
26@ary = (1,2,3,4,5);
27foreach $foo (@ary) {
28 $foo *= 2;
29}
30if (join('',@ary) eq '246810') {print "ok 3\n";} else {print "not ok 3\n";}
31
32for (@ary) {
33 s/(.*)/ok $1\n/;
34}
35
36print $ary[1];
37
38# test for internal scratch array generation
39# this also tests that $foo was restored to 3210 after test 3
40for (split(' ','a b c d e')) {
41 $foo .= $_;
42}
43if ($foo eq '3210abcde') {print "ok 5\n";} else {print "not ok 5 $foo\n";}
44
45foreach $foo (("ok 6\n","ok 7\n")) {
46 print $foo;
47}
48
49sub foo {
50 for $i (1..5) {
51 return $i if $_[0] == $i;
52 }
53}
54
55print foo(1) == 1 ? "ok" : "not ok", " 8\n";
56print foo(2) == 2 ? "ok" : "not ok", " 9\n";
57print foo(5) == 5 ? "ok" : "not ok", " 10\n";
58
59sub bar {
60 return (1, 2, 4);
61}
62
63$a = 0;
64foreach $b (bar()) {
65 $a += $b;
66}
67print $a == 7 ? "ok" : "not ok", " 11\n";
68
69$loop_count = 0;
70for ("-3" .. "0") {
71 $loop_count++;
72}
73print $loop_count == 4 ? "ok" : "not ok", " 12\n";
74
75# modifying arrays in loops is a no-no
76@a = (3,4);
77eval { @a = () for (1,2,@a) };
78print $@ =~ /Use of freed value in iteration/ ? "ok" : "not ok", " 13\n";
79
80# [perl #30061] double destory when same iterator variable (eg $_) used in
81# DESTROY as used in for loop that triggered the destroy
82
83{
84
85 my $x = 0;
86 sub X::DESTROY {
87 my $o = shift;
88 $x++;
89 1 for (1);
90 }
91
92 my %h;
93 $h{foo} = bless [], 'X';
94 delete $h{foo} for $h{foo}, 1;
95 print $x == 1 ? "ok" : "not ok", " 14 - double destroy, x=$x\n";
96}
97
98# A lot of tests to check that reversed for works.
99my $test = 14;
100sub is {
101 my ($got, $expected, $name) = @_;
102 ++$test;
103 if ($got eq $expected) {
104 print "ok $test # $name\n";
105 return 1;
106 }
107 print "not ok $test # $name\n";
108 print "# got '$got', expected '$expected'\n";
109 return 0;
110}
111
112@array = ('A', 'B', 'C');
113for (@array) {
114 $r .= $_;
115}
116is ($r, 'ABC', 'Forwards for array');
117$r = '';
118for (1,2,3) {
119 $r .= $_;
120}
121is ($r, '123', 'Forwards for list');
122$r = '';
123for (map {$_} @array) {
124 $r .= $_;
125}
126is ($r, 'ABC', 'Forwards for array via map');
127$r = '';
128for (map {$_} 1,2,3) {
129 $r .= $_;
130}
131is ($r, '123', 'Forwards for list via map');
132
133$r = '';
134for (reverse @array) {
135 $r .= $_;
136}
137is ($r, 'CBA', 'Reverse for array');
138$r = '';
139for (reverse 1,2,3) {
140 $r .= $_;
141}
142is ($r, '321', 'Reverse for list');
143$r = '';
144for (reverse map {$_} @array) {
145 $r .= $_;
146}
147is ($r, 'CBA', 'Reverse for array via map');
148$r = '';
149for (reverse map {$_} 1,2,3) {
150 $r .= $_;
151}
152is ($r, '321', 'Reverse for list via map');
153
154$r = '';
155for my $i (@array) {
156 $r .= $i;
157}
158is ($r, 'ABC', 'Forwards for array with var');
159$r = '';
160for my $i (1,2,3) {
161 $r .= $i;
162}
163is ($r, '123', 'Forwards for list with var');
164$r = '';
165for my $i (map {$_} @array) {
166 $r .= $i;
167}
168is ($r, 'ABC', 'Forwards for array via map with var');
169$r = '';
170for my $i (map {$_} 1,2,3) {
171 $r .= $i;
172}
173is ($r, '123', 'Forwards for list via map with var');
174
175$r = '';
176for my $i (reverse @array) {
177 $r .= $i;
178}
179is ($r, 'CBA', 'Reverse for array with var');
180$r = '';
181for my $i (reverse 1,2,3) {
182 $r .= $i;
183}
184is ($r, '321', 'Reverse for list with var');
185$r = '';
186for my $i (reverse map {$_} @array) {
187 $r .= $i;
188}
189is ($r, 'CBA', 'Reverse for array via map with var');
190$r = '';
191for my $i (reverse map {$_} 1,2,3) {
192 $r .= $i;
193}
194is ($r, '321', 'Reverse for list via map with var');
195
196# For some reason the generate optree is different when $_ is implicit.
197$r = '';
198for $_ (@array) {
199 $r .= $_;
200}
201is ($r, 'ABC', 'Forwards for array with explicit $_');
202$r = '';
203for $_ (1,2,3) {
204 $r .= $_;
205}
206is ($r, '123', 'Forwards for list with explicit $_');
207$r = '';
208for $_ (map {$_} @array) {
209 $r .= $_;
210}
211is ($r, 'ABC', 'Forwards for array via map with explicit $_');
212$r = '';
213for $_ (map {$_} 1,2,3) {
214 $r .= $_;
215}
216is ($r, '123', 'Forwards for list via map with explicit $_');
217
218$r = '';
219for $_ (reverse @array) {
220 $r .= $_;
221}
222is ($r, 'CBA', 'Reverse for array with explicit $_');
223$r = '';
224for $_ (reverse 1,2,3) {
225 $r .= $_;
226}
227is ($r, '321', 'Reverse for list with explicit $_');
228$r = '';
229for $_ (reverse map {$_} @array) {