This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 3.0 patch #17 patch #16, continued
[perl5.git] / t / cmd.subval
1 #!./perl
2
3 # $Header: cmd.subval,v 3.0 89/10/18 15:24:52 lwall Locked $
4
5 sub foo1 {
6     'true1';
7     if ($_[0]) { 'true2'; }
8 }
9
10 sub foo2 {
11     'true1';
12     if ($_[0]) { return 'true2'; } else { return 'true3'; }
13     'true0';
14 }
15
16 sub foo3 {
17     'true1';
18     unless ($_[0]) { 'true2'; }
19 }
20
21 sub foo4 {
22     'true1';
23     unless ($_[0]) { 'true2'; } else { 'true3'; }
24 }
25
26 sub foo5 {
27     'true1';
28     'true2' if $_[0];
29 }
30
31 sub foo6 {
32     'true1';
33     'true2' unless $_[0];
34 }
35
36 print "1..26\n";
37
38 if (do foo1(0) eq '0') {print "ok 1\n";} else {print "not ok 1 $foo\n";}
39 if (do foo1(1) eq 'true2') {print "ok 2\n";} else {print "not ok 2\n";}
40 if (do foo2(0) eq 'true3') {print "ok 3\n";} else {print "not ok 3\n";}
41 if (do foo2(1) eq 'true2') {print "ok 4\n";} else {print "not ok 4\n";}
42
43 if (do foo3(0) eq 'true2') {print "ok 5\n";} else {print "not ok 5\n";}
44 if (do foo3(1) eq '1') {print "ok 6\n";} else {print "not ok 6\n";}
45 if (do foo4(0) eq 'true2') {print "ok 7\n";} else {print "not ok 7\n";}
46 if (do foo4(1) eq 'true3') {print "ok 8\n";} else {print "not ok 8\n";}
47
48 if (do foo5(0) eq '0') {print "ok 9\n";} else {print "not ok 9\n";}
49 if (do foo5(1) eq 'true2') {print "ok 10\n";} else {print "not ok 10\n";}
50 if (do foo6(0) eq 'true2') {print "ok 11\n";} else {print "not ok 11\n";}
51 if (do foo6(1) eq '1') {print "ok 12\n";} else {print "not ok 12 $x\n";}
52
53 # Now test to see that recursion works using a Fibonacci number generator
54
55 sub fib {
56     local($arg) = @_;
57     local($foo);
58     $level++;
59     if ($arg <= 2) {
60         $foo = 1;
61     }
62     else {
63         $foo = do fib($arg-1) + do fib($arg-2);
64     }
65     $level--;
66     $foo;
67 }
68
69 @good = (0,1,1,2,3,5,8,13,21,34,55,89);
70
71 for ($i = 1; $i <= 10; $i++) {
72     $foo = $i + 12;
73     if (do fib($i) == $good[$i]) {
74         print "ok $foo\n";
75     }
76     else {
77         print "not ok $foo\n";
78     }
79 }
80
81 sub ary1 {
82     (1,2,3);
83 }
84
85 print &ary1 eq 3 ? "ok 23\n" : "not ok 23\n";
86
87 print join(':',&ary1) eq '1:2:3' ? "ok 24\n" : "not ok 24\n";
88
89 sub ary2 {
90     do {
91         return (1,2,3);
92         (3,2,1);
93     };
94     0;
95 }
96
97 print &ary2 eq 3 ? "ok 25\n" : "not ok 25\n";
98
99 $x = join(':',&ary2);
100 print $x eq '1:2:3' ? "ok 26\n" : "not ok 26 $x\n";
101