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 // Dart test program to test arithmetic operations. | 4 // Dart test program to test arithmetic operations. |
5 | 5 |
6 class ArithmeticTest { | 6 class ArithmeticTest { |
7 | 7 |
8 static bool exceptionCaughtParseInt(String s) { | 8 static bool exceptionCaughtParseInt(String s) { |
9 try { | 9 try { |
10 Math.parseInt(s); | 10 Math.parseInt(s); |
11 return false; | 11 return false; |
12 } catch (BadNumberFormatException e) { | 12 } catch (FormatException e) { |
13 return true; | 13 return true; |
14 } | 14 } |
15 } | 15 } |
16 | 16 |
17 static bool exceptionCaughtParseDouble(String s) { | 17 static bool exceptionCaughtParseDouble(String s) { |
18 try { | 18 try { |
19 Math.parseDouble(s); | 19 Math.parseDouble(s); |
20 return false; | 20 return false; |
21 } catch (BadNumberFormatException e) { | 21 } catch (FormatException e) { |
22 return true; | 22 return true; |
23 } | 23 } |
24 } | 24 } |
25 | 25 |
26 static bool toIntThrowsBadNumberFormatException(String str) { | 26 static bool toIntThrowsFormatException(String str) { |
27 // No exception allowed for parse double. | 27 // No exception allowed for parse double. |
28 double d = Math.parseDouble(str); | 28 double d = Math.parseDouble(str); |
29 try { | 29 try { |
30 var a = d.toInt(); | 30 var a = d.toInt(); |
31 return false; | 31 return false; |
32 } catch (BadNumberFormatException e) { | 32 } catch (FormatException e) { |
33 return true; | 33 return true; |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 static runOne() { | 37 static runOne() { |
38 var a = 22; | 38 var a = 22; |
39 var b = 4; | 39 var b = 4; |
40 // Smi & smi. | 40 // Smi & smi. |
41 Expect.equals(26, a + b); | 41 Expect.equals(26, a + b); |
42 Expect.equals(18, a - b); | 42 Expect.equals(18, a - b); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 Expect.equals(true, (-2.0).isNegative()); | 138 Expect.equals(true, (-2.0).isNegative()); |
139 | 139 |
140 double negateDouble(double x) { | 140 double negateDouble(double x) { |
141 return -x; | 141 return -x; |
142 } | 142 } |
143 | 143 |
144 Expect.isTrue(negateDouble(0.0).isNegative()); | 144 Expect.isTrue(negateDouble(0.0).isNegative()); |
145 Expect.isFalse(negateDouble(-0.0).isNegative()); | 145 Expect.isFalse(negateDouble(-0.0).isNegative()); |
146 Expect.isTrue(negateDouble(3.5e3).isNegative()); | 146 Expect.isTrue(negateDouble(3.5e3).isNegative()); |
147 Expect.isFalse(negateDouble(-3.5e3).isNegative()); | 147 Expect.isFalse(negateDouble(-3.5e3).isNegative()); |
148 | 148 |
149 | 149 |
150 // Constants. | 150 // Constants. |
151 final nan = 0.0/0.0; | 151 final nan = 0.0/0.0; |
152 final infinity = 1.0/0.0; | 152 final infinity = 1.0/0.0; |
153 | 153 |
154 // -- isInfinite --. | 154 // -- isInfinite --. |
155 // Smi. | 155 // Smi. |
156 Expect.equals(false, (0).isInfinite()); | 156 Expect.equals(false, (0).isInfinite()); |
157 Expect.equals(false, (1).isInfinite()); | 157 Expect.equals(false, (1).isInfinite()); |
158 Expect.equals(false, (-1).isInfinite()); | 158 Expect.equals(false, (-1).isInfinite()); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 Expect.equals(true, Math.parseDouble("-Infinity").isInfinite()); | 378 Expect.equals(true, Math.parseDouble("-Infinity").isInfinite()); |
379 | 379 |
380 Expect.equals(false, Math.parseDouble("NaN").isNegative()); | 380 Expect.equals(false, Math.parseDouble("NaN").isNegative()); |
381 Expect.equals(false, Math.parseDouble("Infinity").isNegative()); | 381 Expect.equals(false, Math.parseDouble("Infinity").isNegative()); |
382 Expect.equals(true, Math.parseDouble("-Infinity").isNegative()); | 382 Expect.equals(true, Math.parseDouble("-Infinity").isNegative()); |
383 | 383 |
384 Expect.equals("NaN", Math.parseDouble("NaN").toString()); | 384 Expect.equals("NaN", Math.parseDouble("NaN").toString()); |
385 Expect.equals("Infinity", Math.parseDouble("Infinity").toString()); | 385 Expect.equals("Infinity", Math.parseDouble("Infinity").toString()); |
386 Expect.equals("-Infinity", Math.parseDouble("-Infinity").toString()); | 386 Expect.equals("-Infinity", Math.parseDouble("-Infinity").toString()); |
387 | 387 |
388 Expect.equals(false, toIntThrowsBadNumberFormatException("1.2")); | 388 Expect.equals(false, toIntThrowsFormatException("1.2")); |
389 Expect.equals(true, toIntThrowsBadNumberFormatException("Infinity")); | 389 Expect.equals(true, toIntThrowsFormatException("Infinity")); |
390 Expect.equals(true, toIntThrowsBadNumberFormatException("-Infinity")); | 390 Expect.equals(true, toIntThrowsFormatException("-Infinity")); |
391 Expect.equals(true, toIntThrowsBadNumberFormatException("NaN")); | 391 Expect.equals(true, toIntThrowsFormatException("NaN")); |
392 | 392 |
393 // Min/max | 393 // Min/max |
394 Expect.equals(1, Math.min(1, 12)); | 394 Expect.equals(1, Math.min(1, 12)); |
395 Expect.equals(12, Math.max(1, 12)); | 395 Expect.equals(12, Math.max(1, 12)); |
396 Expect.equals(1.0, Math.min(1.0, 12.0)); | 396 Expect.equals(1.0, Math.min(1.0, 12.0)); |
397 Expect.equals(12.0, Math.max(1.0, 12.0)); | 397 Expect.equals(12.0, Math.max(1.0, 12.0)); |
398 Expect.equals(false, 1.0 < Math.min(1.0, 12.0)); | 398 Expect.equals(false, 1.0 < Math.min(1.0, 12.0)); |
399 Expect.equals(true, 1.0 < Math.max(1.0, 12.0)); | 399 Expect.equals(true, 1.0 < Math.max(1.0, 12.0)); |
400 | 400 |
401 // Hashcode | 401 // Hashcode |
402 Expect.equals(false, (3.4).hashCode() == (1.2).hashCode()); | 402 Expect.equals(false, (3.4).hashCode() == (1.2).hashCode()); |
403 Expect.equals(true, (1.2).hashCode() == (1.2).hashCode()); | 403 Expect.equals(true, (1.2).hashCode() == (1.2).hashCode()); |
404 Expect.equals(false, (3).hashCode() == (1).hashCode()); | 404 Expect.equals(false, (3).hashCode() == (1).hashCode()); |
405 Expect.equals(true, (10).hashCode() == (10).hashCode()); | 405 Expect.equals(true, (10).hashCode() == (10).hashCode()); |
406 } | 406 } |
407 | 407 |
408 static testMain() { | 408 static testMain() { |
409 for (int i = 0; i < 1500; i++) { | 409 for (int i = 0; i < 1500; i++) { |
410 runOne(); | 410 runOne(); |
411 } | 411 } |
412 } | 412 } |
413 } | 413 } |
414 | 414 |
415 main() { | 415 main() { |
416 ArithmeticTest.testMain(); | 416 ArithmeticTest.testMain(); |
417 } | 417 } |
OLD | NEW |