xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/cmd/for.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateprint "1..13\n";
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gatefor ($i = 0; $i <= 10; $i++) {
6*0Sstevel@tonic-gate    $x[$i] = $i;
7*0Sstevel@tonic-gate}
8*0Sstevel@tonic-gate$y = $x[10];
9*0Sstevel@tonic-gateprint "#1	:$y: eq :10:\n";
10*0Sstevel@tonic-gate$y = join(' ', @x);
11*0Sstevel@tonic-gateprint "#1	:$y: eq :0 1 2 3 4 5 6 7 8 9 10:\n";
12*0Sstevel@tonic-gateif (join(' ', @x) eq '0 1 2 3 4 5 6 7 8 9 10') {
13*0Sstevel@tonic-gate	print "ok 1\n";
14*0Sstevel@tonic-gate} else {
15*0Sstevel@tonic-gate	print "not ok 1\n";
16*0Sstevel@tonic-gate}
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gate$i = $c = 0;
19*0Sstevel@tonic-gatefor (;;) {
20*0Sstevel@tonic-gate	$c++;
21*0Sstevel@tonic-gate	last if $i++ > 10;
22*0Sstevel@tonic-gate}
23*0Sstevel@tonic-gateif ($c == 12) {print "ok 2\n";} else {print "not ok 2\n";}
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate$foo = 3210;
26*0Sstevel@tonic-gate@ary = (1,2,3,4,5);
27*0Sstevel@tonic-gateforeach $foo (@ary) {
28*0Sstevel@tonic-gate	$foo *= 2;
29*0Sstevel@tonic-gate}
30*0Sstevel@tonic-gateif (join('',@ary) eq '246810') {print "ok 3\n";} else {print "not ok 3\n";}
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gatefor (@ary) {
33*0Sstevel@tonic-gate    s/(.*)/ok $1\n/;
34*0Sstevel@tonic-gate}
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gateprint $ary[1];
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate# test for internal scratch array generation
39*0Sstevel@tonic-gate# this also tests that $foo was restored to 3210 after test 3
40*0Sstevel@tonic-gatefor (split(' ','a b c d e')) {
41*0Sstevel@tonic-gate	$foo .= $_;
42*0Sstevel@tonic-gate}
43*0Sstevel@tonic-gateif ($foo eq '3210abcde') {print "ok 5\n";} else {print "not ok 5 $foo\n";}
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gateforeach $foo (("ok 6\n","ok 7\n")) {
46*0Sstevel@tonic-gate	print $foo;
47*0Sstevel@tonic-gate}
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gatesub foo {
50*0Sstevel@tonic-gate    for $i (1..5) {
51*0Sstevel@tonic-gate	return $i if $_[0] == $i;
52*0Sstevel@tonic-gate    }
53*0Sstevel@tonic-gate}
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gateprint foo(1) == 1 ? "ok" : "not ok", " 8\n";
56*0Sstevel@tonic-gateprint foo(2) == 2 ? "ok" : "not ok", " 9\n";
57*0Sstevel@tonic-gateprint foo(5) == 5 ? "ok" : "not ok", " 10\n";
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gatesub bar {
60*0Sstevel@tonic-gate    return (1, 2, 4);
61*0Sstevel@tonic-gate}
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate$a = 0;
64*0Sstevel@tonic-gateforeach $b (bar()) {
65*0Sstevel@tonic-gate    $a += $b;
66*0Sstevel@tonic-gate}
67*0Sstevel@tonic-gateprint $a == 7 ? "ok" : "not ok", " 11\n";
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate$loop_count = 0;
70*0Sstevel@tonic-gatefor ("-3" .. "0") {
71*0Sstevel@tonic-gate    $loop_count++;
72*0Sstevel@tonic-gate}
73*0Sstevel@tonic-gateprint $loop_count == 4 ? "ok" : "not ok", " 12\n";
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate# modifying arrays in loops is a no-no
76*0Sstevel@tonic-gate@a = (3,4);
77*0Sstevel@tonic-gateeval { @a = () for (1,2,@a) };
78*0Sstevel@tonic-gateprint $@ =~ /Use of freed value in iteration/ ? "ok" : "not ok", " 13\n";
79