Commit | Line | Data |
8d063cd8 |
1 | #!./perl |
2 | |
79072805 |
3 | # $RCSfile: cond.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:41 $ |
8d063cd8 |
4 | |
caec1979 |
5 | print "1..16\n"; |
8d063cd8 |
6 | |
7 | print 1 ? "ok 1\n" : "not ok 1\n"; # compile time |
8 | print 0 ? "not ok 2\n" : "ok 2\n"; |
9 | |
10 | $x = 1; |
11 | print $x ? "ok 3\n" : "not ok 3\n"; # run time |
12 | print !$x ? "not ok 4\n" : "ok 4\n"; |
caec1979 |
13 | |
14 | # Very low precedence between the ? and the : |
15 | print $x ? "ok 5\n" or "not ok 5\n" : "not ok 5\n"; |
16 | # Binds tighter than assignment |
17 | $msg = "not ok 6\n" ? "ok 6\n" : "ok 6\n"; |
18 | print $msg; |
19 | # Binds looser than ".." |
20 | print "ok ", $x ? 7 : -2..15, "\n"; |
21 | # Right-associative |
22 | print $x ? "ok 8\n" : 0 ? "not ok 8\n" : "not ok 8\n"; |
23 | # No parens needed when nested like an if-elsif-elsif-elsif-else |
24 | $n = 9; |
25 | print $n == 7 ? "not ok 9\n" : |
26 | $n == 8 ? "not ok 9\n" : |
27 | $n == 9 ? "ok 9\n" : |
28 | $n == 10 ? "not ok 9\n" : |
29 | "not ok 9\n"; |
30 | # Nor when used like a deeply nested if-else chain |
31 | print $n != 7 ? |
32 | $n != 8 ? |
33 | $n != 9 ? |
34 | $n != 10 ? |
35 | "not ok 10\n" |
36 | : |
37 | "not ok 10\n" |
38 | : |
39 | "ok 10\n" |
40 | : |
41 | "not ok 10\n" |
42 | : |
43 | "not ok 10\n"; |
44 | # A random pathologically nested example, which parses like |
45 | # $a ? ($b ? ($c ? $d : ($e ? $f : $g)) : $h) : ($i ? $j : $k), |
46 | # i.e., |
47 | # if ($a) { |
48 | # if ($b) { |
49 | # if ($c) { |
50 | # $d; |
51 | # } else { |
52 | # if ($e) { |
53 | # $f; |
54 | # } else { |
55 | # $g; |
56 | # } |
57 | # } |
58 | # } else { |
59 | # $h; |
60 | # } |
61 | # } else { |
62 | # if ($i) { |
63 | # $j; |
64 | # } else { |
65 | # $k; |
66 | # } |
67 | # } |
68 | # We exercise all the branches. The ".5"s should be dont-cares. |
69 | ($d, $f, $g, $h, $j, $k) = |
70 | ("ok 11\n", "ok 12\n", "ok 13\n", "ok 14\n", "ok 15\n", "ok 16\n"); |
71 | ($a, $b, $c, $e, $i) = (1, 1, 1, .5, .5); |
72 | print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k; |
73 | ($a, $b, $c, $e, $i) = (1, 1, 0, 1, .5); |
74 | print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k; |
75 | ($a, $b, $c, $e, $i) = (1, 1, 0, 0, .5); |
76 | print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k; |
77 | ($a, $b, $c, $e, $i) = (1, 0, .5, .5, .5); |
78 | print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k; |
79 | ($a, $b, $c, $e, $i) = (0, .5, .5, .5, 1); |
80 | print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k; |
81 | ($a, $b, $c, $e, $i) = (0, .5, .5, .5, 0); |
82 | print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k; |