summaryrefslogtreecommitdiff
path: root/t/op/cond.t
blob: de72a51e9fd9c5b78104fc74006a53df15c34bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!./perl

# $RCSfile: cond.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:41 $

print "1..16\n";

print 1 ? "ok 1\n" : "not ok 1\n";	# compile time
print 0 ? "not ok 2\n" : "ok 2\n";

$x = 1;
print $x ? "ok 3\n" : "not ok 3\n";	# run time
print !$x ? "not ok 4\n" : "ok 4\n";

# Very low precedence between the ? and the :
print $x ? "ok 5\n" or "not ok 5\n" : "not ok 5\n";
# Binds tighter than assignment
$msg = "not ok 6\n" ? "ok 6\n" : "ok 6\n";
print $msg;
# Binds looser than ".."
print "ok ", $x ? 7 : -2..15, "\n";
# Right-associative
print $x ? "ok 8\n" : 0 ? "not ok 8\n" : "not ok 8\n";
# No parens needed when nested like an if-elsif-elsif-elsif-else
$n = 9;
print $n ==  7 ? "not ok 9\n" :
      $n ==  8 ? "not ok 9\n" :
      $n ==  9 ?     "ok 9\n" :
      $n == 10 ? "not ok 9\n" :
      	 	 "not ok 9\n";
# Nor when used like a deeply nested if-else chain
print $n != 7 ?
	$n != 8 ?
	  $n != 9 ?
	    $n != 10 ?
	       "not ok 10\n"
	    :
	       "not ok 10\n"
	  :
	    "ok 10\n"
	:
	  "not ok 10\n"
      :
        "not ok 10\n";
# A random pathologically nested example, which parses like
# $a ? ($b ? ($c ? $d : ($e ? $f : $g)) : $h) : ($i ? $j : $k),
# i.e.,
# if ($a) {
#     if ($b) {
# 	if ($c) {
# 	    $d;
# 	} else {
# 	    if ($e) {
# 		$f;
# 	    } else {
# 		$g;
# 	    }
# 	}
#     } else {
# 	$h;
#     }
# } else {
#     if ($i) {
# 	$j;
#     } else {
# 	$k;
#     }
# }
# We exercise all the branches. The ".5"s should be dont-cares.
($d, $f, $g, $h, $j, $k) =
  ("ok 11\n", "ok 12\n", "ok 13\n", "ok 14\n", "ok 15\n", "ok 16\n");
($a, $b, $c, $e, $i) = (1,  1,  1, .5, .5);
print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k;
($a, $b, $c, $e, $i) = (1,  1,  0,  1, .5);
print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k;
($a, $b, $c, $e, $i) = (1,  1,  0,  0, .5);
print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k;
($a, $b, $c, $e, $i) = (1,  0, .5, .5, .5);
print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k;
($a, $b, $c, $e, $i) = (0, .5, .5, .5,  1);
print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k;
($a, $b, $c, $e, $i) = (0, .5, .5, .5,  0);
print $a ? $ b ? $c ? $d : $e ? $f : $g : $h : $i ? $j : $k;