OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 // Dart test program for testing basic boolean properties. | |
6 // @static-clean | |
7 | |
8 class BoolTest { | |
9 static void testEquality() { | |
10 Expect.equals(true, true); | |
11 Expect.equals(false, false); | |
12 Expect.equals(true, true === true); | |
13 Expect.equals(false, true === false); | |
14 Expect.equals(true, false === false); | |
15 Expect.equals(false, false === true); | |
16 Expect.equals(false, true !== true); | |
17 Expect.equals(true, true !== false); | |
18 Expect.equals(false, false !== false); | |
19 Expect.equals(true, false !== true); | |
20 Expect.equals(true, true == true); | |
21 Expect.equals(false, true == false); | |
22 Expect.equals(true, false == false); | |
23 Expect.equals(false, false == true); | |
24 Expect.equals(false, true != true); | |
25 Expect.equals(true, true != false); | |
26 Expect.equals(false, false != false); | |
27 Expect.equals(true, false != true); | |
28 Expect.equals(true, true === (true == true)); | |
29 Expect.equals(true, false === (true == false)); | |
30 Expect.equals(true, true === (false == false)); | |
31 Expect.equals(true, false === (false == true)); | |
32 Expect.equals(false, true !== (true == true)); | |
33 Expect.equals(false, false !== (true == false)); | |
34 Expect.equals(false, true !== (false == false)); | |
35 Expect.equals(false, false !== (false == true)); | |
36 Expect.equals(false, false === (true == true)); | |
37 Expect.equals(false, true === (true == false)); | |
38 Expect.equals(false, false === (false == false)); | |
39 Expect.equals(false, true === (false == true)); | |
40 Expect.equals(true, false !== (true == true)); | |
41 Expect.equals(true, true !== (true == false)); | |
42 Expect.equals(true, false !== (false == false)); | |
43 Expect.equals(true, true !== (false == true)); | |
44 // Expect.equals could rely on a broken boolean equality. | |
45 if (true == false) { | |
46 throw "Expect.equals broken"; | |
47 } | |
48 if (false == true) { | |
49 throw "Expect.equals broken"; | |
50 } | |
51 if (true === false) { | |
52 throw "Expect.equals broken"; | |
53 } | |
54 if (false === true) { | |
55 throw "Expect.equals broken"; | |
56 } | |
57 if (true == true) { | |
58 } else { | |
59 throw "Expect.equals broken"; | |
60 } | |
61 if (false == false) { | |
62 } else { | |
63 throw "Expect.equals broken"; | |
64 } | |
65 if (true === true) { | |
66 } else { | |
67 throw "Expect.equals broken"; | |
68 } | |
69 if (false === false) { | |
70 } else { | |
71 throw "Expect.equals broken"; | |
72 } | |
73 if (true != false) { | |
74 } else { | |
75 throw "Expect.equals broken"; | |
76 } | |
77 if (false != true) { | |
78 } else { | |
79 throw "Expect.equals broken"; | |
80 } | |
81 if (true !== false) { | |
82 } else { | |
83 throw "Expect.equals broken"; | |
84 } | |
85 if (false !== true) { | |
86 } else { | |
87 throw "Expect.equals broken"; | |
88 } | |
89 if (true != true) { | |
90 throw "Expect.equals broken"; | |
91 } | |
92 if (false != false) { | |
93 throw "Expect.equals broken"; | |
94 } | |
95 if (true !== true) { | |
96 throw "Expect.equals broken"; | |
97 } | |
98 if (false !== false) { | |
99 throw "Expect.equals broken"; | |
100 } | |
101 } | |
102 | |
103 static void testToString() { | |
104 Expect.equals("true", true.toString()); | |
105 Expect.equals("false", false.toString()); | |
106 } | |
107 | |
108 static void testNegate(isTrue, isFalse) { | |
109 Expect.equals(true, !false); | |
110 Expect.equals(false, !true); | |
111 Expect.equals(true, !isFalse); | |
112 Expect.equals(false, !isTrue); | |
113 } | |
114 | |
115 static void testLogicalOp() { | |
116 testOr(a, b, onTypeError) { | |
117 try { | |
118 return a || b; | |
119 } catch (TypeError t) { | |
120 return onTypeError; | |
121 } | |
122 } | |
123 testAnd(a, b, onTypeError) { | |
124 try { | |
125 return a && b; | |
126 } catch (TypeError t) { | |
127 return onTypeError; | |
128 } | |
129 } | |
130 | |
131 var isTrue = true; | |
132 var isFalse = false; | |
133 Expect.equals(true, testAnd(isTrue, isTrue, false)); | |
134 Expect.equals(false, testAnd(isTrue, 0, false)); | |
135 Expect.equals(false, testAnd(isTrue, 1, false)); | |
136 Expect.equals(false, testAnd(isTrue, "true", false)); | |
137 Expect.equals(false, testAnd(0, isTrue, false)); | |
138 Expect.equals(false, testAnd(1, isTrue, false)); | |
139 | |
140 Expect.equals(true, testOr(isTrue, isTrue, false)); | |
141 Expect.equals(true, testOr(isFalse, isTrue, false)); | |
142 Expect.equals(true, testOr(isTrue, isFalse, false)); | |
143 Expect.equals(true, testOr(isTrue, 0, true)); | |
144 Expect.equals(true, testOr(isTrue, 1, true)); | |
145 Expect.equals(false, testOr(isFalse, 0, false)); | |
146 Expect.equals(false, testOr(isFalse, 1, false)); | |
147 Expect.equals(true, testOr(0, isTrue, true)); | |
148 Expect.equals(true, testOr(1, isTrue, true)); | |
149 Expect.equals(false, testOr(0, isFalse, false)); | |
150 Expect.equals(false, testOr(1, isFalse, false)); | |
151 | |
152 // Test side effects. | |
153 int trueCount = 0, falseCount = 0; | |
154 | |
155 trueFunc() { | |
156 trueCount++; | |
157 return true; | |
158 } | |
159 | |
160 falseFunc() { | |
161 falseCount++; | |
162 return false; | |
163 } | |
164 | |
165 Expect.equals(0, trueCount); | |
166 Expect.equals(0, falseCount); | |
167 | |
168 trueFunc() && trueFunc(); | |
169 Expect.equals(2, trueCount); | |
170 Expect.equals(0, falseCount); | |
171 | |
172 trueCount = falseCount = 0; | |
173 falseFunc() && trueFunc(); | |
174 Expect.equals(0, trueCount); | |
175 Expect.equals(1, falseCount); | |
176 | |
177 trueCount = falseCount = 0; | |
178 trueFunc() && falseFunc(); | |
179 Expect.equals(1, trueCount); | |
180 Expect.equals(1, falseCount); | |
181 | |
182 trueCount = falseCount = 0; | |
183 falseFunc() && falseFunc(); | |
184 Expect.equals(0, trueCount); | |
185 Expect.equals(1, falseCount); | |
186 | |
187 trueCount = falseCount = 0; | |
188 trueFunc() || trueFunc(); | |
189 Expect.equals(1, trueCount); | |
190 Expect.equals(0, falseCount); | |
191 | |
192 trueCount = falseCount = 0; | |
193 falseFunc() || trueFunc(); | |
194 Expect.equals(1, trueCount); | |
195 Expect.equals(1, falseCount); | |
196 | |
197 trueCount = falseCount = 0; | |
198 trueFunc() || falseFunc(); | |
199 Expect.equals(1, trueCount); | |
200 Expect.equals(0, falseCount); | |
201 | |
202 trueCount = falseCount = 0; | |
203 falseFunc() || falseFunc(); | |
204 Expect.equals(0, trueCount); | |
205 Expect.equals(2, falseCount); | |
206 } | |
207 | |
208 | |
209 static void testMain() { | |
210 testEquality(); | |
211 testNegate(true, false); | |
212 testToString(); | |
213 testLogicalOp(); | |
214 } | |
215 } | |
216 | |
217 main() { | |
218 BoolTest.testMain(); | |
219 } | |
OLD | NEW |