OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 @NoInline() @AssumeDynamic() | 6 |
| 7 @NoInline() |
| 8 @AssumeDynamic() |
7 confuse(x) => x; | 9 confuse(x) => x; |
8 | 10 |
9 @NoInline() | 11 @NoInline() |
10 asNum(x) { | 12 asNum(x) { |
11 var result = confuse(x); | 13 var result = confuse(x); |
12 if (result is num) return result; | 14 if (result is num) return result; |
13 throw new ArgumentError.value(x); | 15 throw new ArgumentError.value(x); |
14 } | 16 } |
15 | 17 |
16 @NoInline() | 18 @NoInline() |
17 uint31(x) { | 19 uint31(x) { |
18 var result = confuse(x); | 20 var result = confuse(x); |
19 if (x is int) { | 21 if (x is int) { |
20 var masked = 0x7fffffff & x; // inferred uint31 type. | 22 var masked = 0x7fffffff & x; // inferred uint31 type. |
21 if (masked == x) return masked; | 23 if (masked == x) return masked; |
22 } | 24 } |
23 throw new ArgumentError('Not uint31: $x'); | 25 throw new ArgumentError('Not uint31: $x'); |
24 } | 26 } |
25 | 27 |
26 @NoInline() | 28 @NoInline() |
27 uint32(x) { | 29 uint32(x) { |
28 var result = confuse(x); | 30 var result = confuse(x); |
29 if (x is int) { | 31 if (x is int) { |
30 var masked = 0xffffffff & x; // inferred uint32 type. | 32 var masked = 0xffffffff & x; // inferred uint32 type. |
31 if (masked == x) return masked; | 33 if (masked == x) return masked; |
32 } | 34 } |
33 throw new ArgumentError('Not uint32: $x'); | 35 throw new ArgumentError('Not uint32: $x'); |
34 } | 36 } |
35 | 37 |
36 @NoInline() | 38 @NoInline() |
37 int zero() { return 0; } | 39 int zero() { |
| 40 return 0; |
| 41 } |
38 | 42 |
39 @NoInline() | 43 @NoInline() |
40 int one() { return 1; } | 44 int one() { |
| 45 return 1; |
| 46 } |
41 | 47 |
42 @NoInline() | 48 @NoInline() |
43 int minus1() { return 0 - 1; } | 49 int minus1() { |
| 50 return 0 - 1; |
| 51 } |
44 | 52 |
45 @NoInline() | 53 @NoInline() |
46 int minus2() { return 0 - 2; } | 54 int minus2() { |
| 55 return 0 - 2; |
| 56 } |
47 | 57 |
48 @NoInline() | 58 @NoInline() |
49 int two() { return 2; } | 59 int two() { |
| 60 return 2; |
| 61 } |
50 | 62 |
51 @NoInline() | 63 @NoInline() |
52 int three() { return 3; } | 64 int three() { |
| 65 return 3; |
| 66 } |
53 | 67 |
54 @NoInline() | 68 @NoInline() |
55 int five() { return 5; } | 69 int five() { |
| 70 return 5; |
| 71 } |
56 | 72 |
57 @NoInline() | 73 @NoInline() |
58 int minus5() { return 0 - 5; } | 74 int minus5() { |
| 75 return 0 - 5; |
| 76 } |
59 | 77 |
60 @NoInline() | 78 @NoInline() |
61 int ninetyNine() { return 99; } | 79 int ninetyNine() { |
| 80 return 99; |
| 81 } |
62 | 82 |
63 @NoInline() | 83 @NoInline() |
64 int four99() { return 499; } | 84 int four99() { |
| 85 return 499; |
| 86 } |
65 | 87 |
66 @NoInline() | 88 @NoInline() |
67 int four99times99() { return 499 * 99; } | 89 int four99times99() { |
| 90 return 499 * 99; |
| 91 } |
68 | 92 |
69 @NoInline() | 93 @NoInline() |
70 int four99times99plus1() { return 499 * 99 + 1; } | 94 int four99times99plus1() { |
| 95 return 499 * 99 + 1; |
| 96 } |
71 | 97 |
72 @NoInline() | 98 @NoInline() |
73 void addTest() { | 99 void addTest() { |
74 var m1 = 0 - 1; | 100 var m1 = 0 - 1; |
75 Expect.equals(0, 0 + 0); | 101 Expect.equals(0, 0 + 0); |
76 Expect.equals(0, confuse(0) + 0); | 102 Expect.equals(0, confuse(0) + 0); |
77 Expect.equals(0, asNum(0) + 0); | 103 Expect.equals(0, asNum(0) + 0); |
78 Expect.equals(0, uint31(0) + 0); | 104 Expect.equals(0, uint31(0) + 0); |
79 | 105 |
80 Expect.equals(m1, m1 + 0); | 106 Expect.equals(m1, m1 + 0); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 Expect.equals(499, four99times99() ~/ 99); | 178 Expect.equals(499, four99times99() ~/ 99); |
153 Expect.equals(499, four99times99plus1() ~/ 99); | 179 Expect.equals(499, four99times99plus1() ~/ 99); |
154 | 180 |
155 Expect.equals(-33, -100 ~/ 3); | 181 Expect.equals(-33, -100 ~/ 3); |
156 Expect.equals(-33, asNum(-100) ~/ 3); | 182 Expect.equals(-33, asNum(-100) ~/ 3); |
157 Expect.equals(33, -100 ~/ -3); | 183 Expect.equals(33, -100 ~/ -3); |
158 Expect.equals(33, asNum(-100) ~/ -3); | 184 Expect.equals(33, asNum(-100) ~/ -3); |
159 | 185 |
160 // Signed int32 boundary is involved in optimizations. | 186 // Signed int32 boundary is involved in optimizations. |
161 | 187 |
162 Expect.equals(-0x80000000, -0x80000000 ~/ 1.0); | 188 Expect.equals(-0x80000000, -0x80000000 ~/ 1.0); |
163 Expect.equals(-0x80000000, -0x80000000 ~/ 1.0000000000000001); | 189 Expect.equals(-0x80000000, -0x80000000 ~/ 1.0000000000000001); |
164 Expect.equals(-0x7fffffff, -0x80000000 ~/ 1.0000000000000002); | 190 Expect.equals(-0x7fffffff, -0x80000000 ~/ 1.0000000000000002); |
165 | 191 |
166 Expect.equals(-0x80000000, asNum(-0x80000000) ~/ 1.0); | 192 Expect.equals(-0x80000000, asNum(-0x80000000) ~/ 1.0); |
167 Expect.equals(-0x80000000, asNum(-0x80000000) ~/ 1.0000000000000001); | 193 Expect.equals(-0x80000000, asNum(-0x80000000) ~/ 1.0000000000000001); |
168 Expect.equals(-0x7fffffff, asNum(-0x80000000) ~/ 1.0000000000000002); | 194 Expect.equals(-0x7fffffff, asNum(-0x80000000) ~/ 1.0000000000000002); |
169 | 195 |
170 Expect.equals(-0x80000000, asNum(0x80000000) ~/ -1.0); | 196 Expect.equals(-0x80000000, asNum(0x80000000) ~/ -1.0); |
171 Expect.equals(-0x80000000, asNum(0x80000000) ~/ -1.0000000000000001); | 197 Expect.equals(-0x80000000, asNum(0x80000000) ~/ -1.0000000000000001); |
172 Expect.equals(-0x7fffffff, asNum(0x80000000) ~/ -1.0000000000000002); | 198 Expect.equals(-0x7fffffff, asNum(0x80000000) ~/ -1.0000000000000002); |
173 | 199 |
174 Expect.equals(0x7fffffff, 0x10000000 ~/ .12500000000000002); | 200 Expect.equals(0x7fffffff, 0x10000000 ~/ .12500000000000002); |
175 Expect.equals(0x80000000, 0x10000000 ~/ .125); | 201 Expect.equals(0x80000000, 0x10000000 ~/ .125); |
176 Expect.equals(-0x7fffffff, 0x10000000 ~/ -.12500000000000002); | 202 Expect.equals(-0x7fffffff, 0x10000000 ~/ -.12500000000000002); |
177 Expect.equals(-0x80000000, 0x10000000 ~/ -.125); | 203 Expect.equals(-0x80000000, 0x10000000 ~/ -.125); |
178 | 204 |
179 Expect.equals(0x7fffffff, uint31(0x10000000) ~/ .12500000000000002); | 205 Expect.equals(0x7fffffff, uint31(0x10000000) ~/ .12500000000000002); |
180 Expect.equals(0x80000000, uint31(0x10000000) ~/ .125); | 206 Expect.equals(0x80000000, uint31(0x10000000) ~/ .125); |
181 Expect.equals(-0x7fffffff, uint31(0x10000000) ~/ -.12500000000000002); | 207 Expect.equals(-0x7fffffff, uint31(0x10000000) ~/ -.12500000000000002); |
182 Expect.equals(-0x80000000, uint31(0x10000000) ~/ -.125); | 208 Expect.equals(-0x80000000, uint31(0x10000000) ~/ -.125); |
183 | 209 |
184 // These can be compiled to `(a / 2) | 0`. | 210 // These can be compiled to `(a / 2) | 0`. |
185 Expect.equals(100, uint31(200) ~/ 2); | 211 Expect.equals(100, uint31(200) ~/ 2); |
186 Expect.equals(100, uint32(200) ~/ 2); | 212 Expect.equals(100, uint32(200) ~/ 2); |
187 | 213 |
188 Expect.equals(100, asNum(200) ~/ 2); | 214 Expect.equals(100, asNum(200) ~/ 2); |
189 Expect.equals(100, confuse(200) ~/ 2); | 215 Expect.equals(100, confuse(200) ~/ 2); |
190 Expect.equals(-100, uint31(200) ~/ -2); | 216 Expect.equals(-100, uint31(200) ~/ -2); |
191 Expect.equals(-100, uint32(200) ~/ -2); | 217 Expect.equals(-100, uint32(200) ~/ -2); |
192 Expect.equals(-100, asNum(200) ~/ -2); | 218 Expect.equals(-100, asNum(200) ~/ -2); |
193 Expect.equals(-100, confuse(200) ~/ -2); | 219 Expect.equals(-100, confuse(200) ~/ -2); |
194 | 220 |
195 // These can be compiled to `((a + b) / 2) | 0`. | 221 // These can be compiled to `((a + b) / 2) | 0`. |
196 Expect.equals(100, (uint31(100) + uint31(100)) ~/ 2); | 222 Expect.equals(100, (uint31(100) + uint31(100)) ~/ 2); |
197 Expect.equals(0x7fffffff, (uint31(0x7fffffff) + uint31(0x7fffffff)) ~/ 2); | 223 Expect.equals(0x7fffffff, (uint31(0x7fffffff) + uint31(0x7fffffff)) ~/ 2); |
198 | 224 |
199 // NaN and Infinity results are errors. | 225 // NaN and Infinity results are errors. |
200 Expect.throws(() => -1 ~/ 0); | 226 Expect.throws(() => -1 ~/ 0); |
201 Expect.throws(() => 1.5 ~/ 0); | 227 Expect.throws(() => 1.5 ~/ 0); |
202 Expect.throws(() => 1e200 ~/ 1e-200); | 228 Expect.throws(() => 1e200 ~/ 1e-200); |
203 Expect.throws(() => -1e200 ~/ 1e-200); | 229 Expect.throws(() => -1e200 ~/ 1e-200); |
204 Expect.throws(() => 1e200 ~/ -1e-200); | 230 Expect.throws(() => 1e200 ~/ -1e-200); |
205 Expect.throws(() => -1e200 ~/ -1e-200); | 231 Expect.throws(() => -1e200 ~/ -1e-200); |
206 Expect.throws(() => double.NAN ~/ 2); | 232 Expect.throws(() => double.NAN ~/ 2); |
207 } | 233 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 @NoInline() | 321 @NoInline() |
296 void andTest() { | 322 void andTest() { |
297 Expect.equals(2, 10 & 3); | 323 Expect.equals(2, 10 & 3); |
298 Expect.equals(7, 15 & 7); | 324 Expect.equals(7, 15 & 7); |
299 Expect.equals(10, 10 & 10); | 325 Expect.equals(10, 10 & 10); |
300 | 326 |
301 Expect.equals(99, ninetyNine() & ninetyNine()); | 327 Expect.equals(99, ninetyNine() & ninetyNine()); |
302 Expect.equals(34, four99() & 42); | 328 Expect.equals(34, four99() & 42); |
303 Expect.equals(3, minus5() & 7); | 329 Expect.equals(3, minus5() & 7); |
304 | 330 |
305 Expect.equals(0, uint31(0x7ffffffe) & uint31(1)); | 331 Expect.equals(0, uint31(0x7ffffffe) & uint31(1)); |
306 Expect.equals(0, asNum(0x7ffffffe) & asNum(1)); | 332 Expect.equals(0, asNum(0x7ffffffe) & asNum(1)); |
307 } | 333 } |
308 | 334 |
309 @NoInline() | 335 @NoInline() |
310 void orTest() { | 336 void orTest() { |
311 Expect.equals(11, 10 | 3); | 337 Expect.equals(11, 10 | 3); |
312 Expect.equals(15, 15 | 7); | 338 Expect.equals(15, 15 | 7); |
313 Expect.equals(10, 10 | 10); | 339 Expect.equals(10, 10 | 10); |
314 | 340 |
315 Expect.equals(99, ninetyNine() | ninetyNine()); | 341 Expect.equals(99, ninetyNine() | ninetyNine()); |
316 Expect.equals(507, four99() | 42); | 342 Expect.equals(507, four99() | 42); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 orTest(); | 555 orTest(); |
530 xorTest(); | 556 xorTest(); |
531 notTest(); | 557 notTest(); |
532 negateTest(); | 558 negateTest(); |
533 equalsTest(); | 559 equalsTest(); |
534 lessTest(); | 560 lessTest(); |
535 lessEqualTest(); | 561 lessEqualTest(); |
536 greaterTest(); | 562 greaterTest(); |
537 greaterEqualTest(); | 563 greaterEqualTest(); |
538 } | 564 } |
OLD | NEW |