OLD | NEW |
| (Empty) |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Tests of conditional expressions and statements and negation. | |
6 | |
7 import 'expect.dart'; | |
8 | |
9 mkTrue() => true; | |
10 mkFalse() => false; | |
11 | |
12 check(b) { | |
13 Expect.isTrue(b); | |
14 return b; | |
15 } | |
16 | |
17 main() { | |
18 // Check that ?: gets the right answer. | |
19 Expect.isTrue((mkTrue() ? 0 : 1) == 0); | |
20 Expect.isTrue((mkFalse() ? 0 : 1) == 1); | |
21 // Check that it doesn't evaluate all subexpressions. | |
22 mkTrue() ? Expect.isTrue(true) : Expect.isTrue(false); | |
23 mkFalse() ? Expect.isTrue(false) : Expect.isTrue(true); | |
24 | |
25 // Check that && and || get the right answers. | |
26 Expect.isTrue(mkTrue() && mkTrue()); | |
27 Expect.isTrue(!(mkTrue() && mkFalse())); | |
28 Expect.isTrue(!(mkFalse() && mkTrue())); | |
29 Expect.isTrue(!(mkFalse() && mkFalse())); | |
30 Expect.isTrue(mkTrue() || mkTrue()); | |
31 Expect.isTrue(mkTrue() || mkFalse()); | |
32 Expect.isTrue(mkFalse() || mkTrue()); | |
33 Expect.isTrue(!(mkFalse() || mkFalse())); | |
34 | |
35 // Check that they don't evaluate both subexpressions. | |
36 mkTrue() && check(true); | |
37 mkFalse() && check(false); | |
38 mkTrue() || check(true); | |
39 mkFalse() || check(true); | |
40 | |
41 // Check that if works. | |
42 if (mkTrue()) { | |
43 Expect.isTrue(true); | |
44 } else { | |
45 Expect.isTrue(false); | |
46 } | |
47 if (mkFalse()) { | |
48 Expect.isTrue(false); | |
49 } else { | |
50 Expect.isTrue(true); | |
51 } | |
52 if (!mkTrue()) { | |
53 Expect.isTrue(false); | |
54 } else { | |
55 Expect.isTrue(true); | |
56 } | |
57 if (!mkFalse()) { | |
58 Expect.isTrue(true); | |
59 } else { | |
60 Expect.isTrue(false); | |
61 } | |
62 | |
63 // Check that ?:, &&, and || work for control flow. | |
64 if (mkTrue() ? mkTrue() : mkFalse()) { | |
65 Expect.isTrue(true); | |
66 } else { | |
67 Expect.isTrue(false); | |
68 } | |
69 if (mkTrue() ? mkFalse() : mkTrue()) { | |
70 Expect.isTrue(false); | |
71 } else { | |
72 Expect.isTrue(true); | |
73 } | |
74 if (mkFalse() ? mkTrue() : mkFalse()) { | |
75 Expect.isTrue(false); | |
76 } else { | |
77 Expect.isTrue(true); | |
78 } | |
79 if (mkFalse() ? mkFalse() : mkTrue()) { | |
80 Expect.isTrue(true); | |
81 } else { | |
82 Expect.isTrue(false); | |
83 } | |
84 if (mkTrue() && mkTrue()) { | |
85 Expect.isTrue(true); | |
86 } else { | |
87 Expect.isTrue(false); | |
88 } | |
89 if (mkTrue() && mkFalse()) { | |
90 Expect.isTrue(false); | |
91 } else { | |
92 Expect.isTrue(true); | |
93 } | |
94 if (mkFalse() && mkTrue()) { | |
95 Expect.isTrue(false); | |
96 } else { | |
97 Expect.isTrue(true); | |
98 } | |
99 if (mkFalse() && mkFalse()) { | |
100 Expect.isTrue(false); | |
101 } else { | |
102 Expect.isTrue(true); | |
103 } | |
104 if (mkTrue() || mkTrue()) { | |
105 Expect.isTrue(true); | |
106 } else { | |
107 Expect.isTrue(false); | |
108 } | |
109 if (mkTrue() || mkFalse()) { | |
110 Expect.isTrue(true); | |
111 } else { | |
112 Expect.isTrue(false); | |
113 } | |
114 if (mkFalse() || mkTrue()) { | |
115 Expect.isTrue(true); | |
116 } else { | |
117 Expect.isTrue(false); | |
118 } | |
119 if (mkFalse() || mkFalse()) { | |
120 Expect.isTrue(false); | |
121 } else { | |
122 Expect.isTrue(true); | |
123 } | |
124 | |
125 // Test empty else branches. | |
126 if (mkTrue()) { | |
127 Expect.isTrue(true); | |
128 } | |
129 if (mkFalse()) { | |
130 Expect.isTrue(false); | |
131 } | |
132 | |
133 var x = 0; | |
134 if (mkTrue()) { | |
135 x = 1; | |
136 } | |
137 Expect.isTrue(x == 1); | |
138 if (mkFalse()) { | |
139 x = 2; | |
140 } | |
141 Expect.isTrue(x == 1); | |
142 } | |
OLD | NEW |