OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 // VMOptions=--enable_asserts | |
5 | 4 |
6 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
7 | 6 |
8 bool testAssociativity(Function f) { | 7 bool testAssociativity(Function f) { |
9 // Example from https://en.wikipedia.org/wiki/Floating_point | 8 // Example from https://en.wikipedia.org/wiki/Floating_point |
10 // Test that (a + b) + c == a + (b + c). | 9 // Test that (a + b) + c == a + (b + c). |
11 double a = f(1234.567); // Chop literals. | 10 double a = f(1234.567); // Chop literals. |
12 double b = f(45.67834); | 11 double b = f(45.67834); |
13 double c = f(0.0004); | 12 double c = f(0.0004); |
14 double x = (a + b) + c; // Chop result of multiplication or division only. | 13 double x = (a + b) + c; // Chop result of multiplication or division only. |
(...skipping 14 matching lines...) Expand all Loading... |
29 print("x: $x"); | 28 print("x: $x"); |
30 print("y: $y"); | 29 print("y: $y"); |
31 return x == y; | 30 return x == y; |
32 } | 31 } |
33 | 32 |
34 // Simulate precision checking with assert. | 33 // Simulate precision checking with assert. |
35 assertP(double d) { | 34 assertP(double d) { |
36 assert(d == d.p); | 35 assert(d == d.p); |
37 } | 36 } |
38 | 37 |
| 38 bool assertionsEnabled() { |
| 39 try { |
| 40 assert(false); |
| 41 return false; |
| 42 } on AssertionError catch (e) { |
| 43 return true; |
| 44 } |
| 45 return false; |
| 46 } |
| 47 |
39 main() { | 48 main() { |
40 // The getter p keeps only 20 (by default) bits after the decimal point. | 49 // The getter p keeps only 20 (by default) bits after the decimal point. |
41 Expect.equals(0.0, 0.0.p); // 0.0 has no 1-bit after the decimal point. | 50 Expect.equals(0.0, 0.0.p); // 0.0 has no 1-bit after the decimal point. |
42 Expect.equals(1.5, 1.5.p); // 1.5 has a single 1-bit after the decimal point. | 51 Expect.equals(1.5, 1.5.p); // 1.5 has a single 1-bit after the decimal point. |
43 Expect.notEquals(1.1, 1.1.p); // 1.1 has many 1-bits after the decimal point. | 52 Expect.notEquals(1.1, 1.1.p); // 1.1 has many 1-bits after the decimal point. |
44 Expect.notEquals(1/3, (1/3).p); // 0.33333333... ditto. | 53 Expect.notEquals(1/3, (1/3).p); // 0.33333333... ditto. |
45 | 54 |
46 Expect.equals(1.1 + 1/3, 1/3 + 1.1); // Test addition commutativity. | 55 Expect.equals(1.1 + 1/3, 1/3 + 1.1); // Test addition commutativity. |
47 Expect.equals(1.1.p + (1/3).p, (1/3).p + 1.1.p); | 56 Expect.equals(1.1.p + (1/3).p, (1/3).p + 1.1.p); |
48 Expect.equals(1.1 * 1/3, 1/3 * 1.1); // Test multiplication commutativity. | 57 Expect.equals(1.1 * 1/3, 1/3 * 1.1); // Test multiplication commutativity. |
49 Expect.equals(1.1.p * (1/3).p, (1/3).p * 1.1.p); | 58 Expect.equals(1.1.p * (1/3).p, (1/3).p * 1.1.p); |
50 | 59 |
51 print("Without chopping fractional bits:"); | 60 print("Without chopping fractional bits:"); |
52 Expect.isFalse(testAssociativity((x) => x)); | 61 Expect.isFalse(testAssociativity((x) => x)); |
53 Expect.isFalse(testDistributivity((x) => x)); | 62 Expect.isFalse(testDistributivity((x) => x)); |
54 print("With chopping fractional bits:"); | 63 print("With chopping fractional bits:"); |
55 Expect.isTrue(testAssociativity((x) => x.p)); | 64 Expect.isTrue(testAssociativity((x) => x.p)); |
56 Expect.isTrue(testDistributivity((x) => x.p)); | 65 Expect.isTrue(testDistributivity((x) => x.p)); |
57 | 66 |
58 // Check that p works with NaN and Infinity. | 67 // Check that p works with NaN and Infinity. |
59 Expect.isTrue(double.NAN.p.isNaN); | 68 Expect.isTrue(double.NAN.p.isNaN); |
60 Expect.isTrue(double.INFINITY.p.isInfinite); | 69 Expect.isTrue(double.INFINITY.p.isInfinite); |
61 Expect.isFalse(double.INFINITY.p.isNegative); | 70 Expect.isFalse(double.INFINITY.p.isNegative); |
62 Expect.isTrue(double.NEGATIVE_INFINITY.p.isInfinite); | 71 Expect.isTrue(double.NEGATIVE_INFINITY.p.isInfinite); |
63 Expect.isTrue(double.NEGATIVE_INFINITY.p.isNegative); | 72 Expect.isTrue(double.NEGATIVE_INFINITY.p.isNegative); |
64 | 73 |
65 // Check use of assert to verify precision. | 74 // Check use of assert to verify precision. |
66 assertP(1.5); | 75 if (assertionsEnabled()) { |
67 assertP(1.1.p); | 76 assertP(1.5); |
68 Expect.throws(() => assertP(1.1), (e) => e is AssertionError); | 77 assertP(1.1.p); |
69 assertP(1.23456789.p); | 78 Expect.throws(() => assertP(1.1), (e) => e is AssertionError); |
70 Expect.throws(() => assertP(1.23456789), (e) => e is AssertionError); | 79 assertP(1.23456789.p); |
| 80 Expect.throws(() => assertP(1.23456789), (e) => e is AssertionError); |
| 81 } |
71 } | 82 } |
OLD | NEW |