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 library math_test; | 5 library math_test; |
| 6 |
6 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
7 import 'dart:math'; | 8 import 'dart:math'; |
8 | 9 |
9 class MathTest { | 10 class MathTest { |
10 static void testConstants() { | 11 static void testConstants() { |
11 // Source for mathematical constants is Wolfram Alpha. | 12 // Source for mathematical constants is Wolfram Alpha. |
12 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669, | 13 Expect.equals( |
13 E); | 14 2.7182818284590452353602874713526624977572470936999595749669, E); |
14 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333, | 15 Expect.equals( |
15 LN10); | 16 2.3025850929940456840179914546843642076011014886287729760333, LN10); |
16 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206, | 17 Expect.equals( |
17 LN2); | 18 0.6931471805599453094172321214581765680755001343602552541206, LN2); |
18 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354, | 19 Expect.equals( |
19 LOG2E); | 20 1.4426950408889634073599246810018921374266459541529859341354, LOG2E); |
20 Expect.equals(0.4342944819032518276511289189166050822943970058036665661144, | 21 Expect.equals( |
21 LOG10E); | 22 0.4342944819032518276511289189166050822943970058036665661144, LOG10E); |
22 Expect.equals(3.1415926535897932384626433832795028841971693993751058209749, | 23 Expect.equals( |
23 PI); | 24 3.1415926535897932384626433832795028841971693993751058209749, PI); |
24 Expect.equals(0.7071067811865475244008443621048490392848359376884740365883, | 25 Expect.equals( |
25 SQRT1_2); | 26 0.7071067811865475244008443621048490392848359376884740365883, SQRT1_2); |
26 Expect.equals(1.4142135623730950488016887242096980785696718753769480731766, | 27 Expect.equals( |
27 SQRT2); | 28 1.4142135623730950488016887242096980785696718753769480731766, SQRT2); |
28 } | 29 } |
29 | 30 |
30 static checkClose(double a, double b, EPSILON) { | 31 static checkClose(double a, double b, EPSILON) { |
31 Expect.equals(true, a - EPSILON <= b); | 32 Expect.equals(true, a - EPSILON <= b); |
32 Expect.equals(true, b <= a + EPSILON); | 33 Expect.equals(true, b <= a + EPSILON); |
33 } | 34 } |
34 | 35 |
35 static void testSin() { | 36 static void testSin() { |
36 // Given the imprecision of PI we can't expect better results than this. | 37 // Given the imprecision of PI we can't expect better results than this. |
37 final double EPSILON = 1e-15; | 38 final double EPSILON = 1e-15; |
(...skipping 24 matching lines...) Expand all Loading... |
62 } | 63 } |
63 | 64 |
64 static void testAsin() { | 65 static void testAsin() { |
65 // Given the imprecision of PI we can't expect better results than this. | 66 // Given the imprecision of PI we can't expect better results than this. |
66 final double EPSILON = 1e-15; | 67 final double EPSILON = 1e-15; |
67 checkClose(0.0, asin(0.0), EPSILON); | 68 checkClose(0.0, asin(0.0), EPSILON); |
68 checkClose(PI / 2.0, asin(1.0), EPSILON); | 69 checkClose(PI / 2.0, asin(1.0), EPSILON); |
69 checkClose(-PI / 2.0, asin(-1.0), EPSILON); | 70 checkClose(-PI / 2.0, asin(-1.0), EPSILON); |
70 } | 71 } |
71 | 72 |
72 | |
73 static void testAcos() { | 73 static void testAcos() { |
74 // Given the imprecision of PI we can't expect better results than this. | 74 // Given the imprecision of PI we can't expect better results than this. |
75 final double EPSILON = 1e-15; | 75 final double EPSILON = 1e-15; |
76 checkClose(0.0, acos(1.0), EPSILON); | 76 checkClose(0.0, acos(1.0), EPSILON); |
77 checkClose(PI, acos(-1.0), EPSILON); | 77 checkClose(PI, acos(-1.0), EPSILON); |
78 checkClose(PI / 2.0, acos(0.0), EPSILON); | 78 checkClose(PI / 2.0, acos(0.0), EPSILON); |
79 } | 79 } |
80 | 80 |
81 static void testAtan() { | 81 static void testAtan() { |
82 // Given the imprecision of PI we can't expect better results than this. | 82 // Given the imprecision of PI we can't expect better results than this. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 Expect.equals(true, limitLow <= b); | 121 Expect.equals(true, limitLow <= b); |
122 Expect.equals(true, b <= limitHigh); | 122 Expect.equals(true, b <= limitHigh); |
123 } | 123 } |
124 | 124 |
125 static void testSqrt() { | 125 static void testSqrt() { |
126 checkVeryClose(2.0, sqrt(4.0)); | 126 checkVeryClose(2.0, sqrt(4.0)); |
127 checkVeryClose(SQRT2, sqrt(2.0)); | 127 checkVeryClose(SQRT2, sqrt(2.0)); |
128 checkVeryClose(SQRT1_2, sqrt(0.5)); | 128 checkVeryClose(SQRT1_2, sqrt(0.5)); |
129 checkVeryClose(1e50, sqrt(1e100)); | 129 checkVeryClose(1e50, sqrt(1e100)); |
130 checkVeryClose(1.1111111061110855443054405046358901279277111935183977e56, | 130 checkVeryClose(1.1111111061110855443054405046358901279277111935183977e56, |
131 sqrt(12345678901234e99)); | 131 sqrt(12345678901234e99)); |
132 } | 132 } |
133 | 133 |
134 static void testExp() { | 134 static void testExp() { |
135 checkVeryClose(E, exp(1.0)); | 135 checkVeryClose(E, exp(1.0)); |
136 final EPSILON = 1e-15; | 136 final EPSILON = 1e-15; |
137 checkClose(10.0, exp(LN10), EPSILON); | 137 checkClose(10.0, exp(LN10), EPSILON); |
138 checkClose(2.0, exp(LN2), EPSILON); | 138 checkClose(2.0, exp(LN2), EPSILON); |
139 } | 139 } |
140 | 140 |
141 static void testLog() { | 141 static void testLog() { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 Expect.equals(-0xabcdef, int.parse(" -0x00000abcdef ")); | 194 Expect.equals(-0xabcdef, int.parse(" -0x00000abcdef ")); |
195 Expect.equals(-0xABCDEF, int.parse(" -0x00000ABCDEF ")); | 195 Expect.equals(-0xABCDEF, int.parse(" -0x00000ABCDEF ")); |
196 Expect.equals(10, int.parse("010")); | 196 Expect.equals(10, int.parse("010")); |
197 Expect.equals(-10, int.parse("-010")); | 197 Expect.equals(-10, int.parse("-010")); |
198 Expect.equals(10, int.parse(" 010 ")); | 198 Expect.equals(10, int.parse(" 010 ")); |
199 Expect.equals(-10, int.parse(" -010 ")); | 199 Expect.equals(-10, int.parse(" -010 ")); |
200 Expect.equals(9, int.parse("09")); | 200 Expect.equals(9, int.parse("09")); |
201 Expect.equals(9, int.parse(" 09 ")); | 201 Expect.equals(9, int.parse(" 09 ")); |
202 Expect.equals(-9, int.parse("-09")); | 202 Expect.equals(-9, int.parse("-09")); |
203 Expect.equals(0x1234567890, int.parse("+0x1234567890")); | 203 Expect.equals(0x1234567890, int.parse("+0x1234567890")); |
204 Expect.equals(0x1234567890,int.parse(" +0x1234567890 ")); | 204 Expect.equals(0x1234567890, int.parse(" +0x1234567890 ")); |
205 Expect.equals(0x100, int.parse("+0x100")); | 205 Expect.equals(0x100, int.parse("+0x100")); |
206 Expect.equals(0x100, int.parse(" +0x100 ")); | 206 Expect.equals(0x100, int.parse(" +0x100 ")); |
207 Expect.equals(true, parseIntThrowsFormatException("1b")); | 207 Expect.equals(true, parseIntThrowsFormatException("1b")); |
208 Expect.equals(true, parseIntThrowsFormatException(" 1b ")); | 208 Expect.equals(true, parseIntThrowsFormatException(" 1b ")); |
209 Expect.equals(true, parseIntThrowsFormatException(" 1 b ")); | 209 Expect.equals(true, parseIntThrowsFormatException(" 1 b ")); |
210 Expect.equals(true, parseIntThrowsFormatException("1e2")); | 210 Expect.equals(true, parseIntThrowsFormatException("1e2")); |
211 Expect.equals(true, parseIntThrowsFormatException(" 1e2 ")); | 211 Expect.equals(true, parseIntThrowsFormatException(" 1e2 ")); |
212 Expect.equals(true, parseIntThrowsFormatException("00x12")); | 212 Expect.equals(true, parseIntThrowsFormatException("00x12")); |
213 Expect.equals(true, parseIntThrowsFormatException(" 00x12 ")); | 213 Expect.equals(true, parseIntThrowsFormatException(" 00x12 ")); |
214 Expect.equals(true, parseIntThrowsFormatException("-1b")); | 214 Expect.equals(true, parseIntThrowsFormatException("-1b")); |
(...skipping 30 matching lines...) Expand all Loading... |
245 testSqrt(); | 245 testSqrt(); |
246 testLog(); | 246 testLog(); |
247 testExp(); | 247 testExp(); |
248 testParseInt(); | 248 testParseInt(); |
249 } | 249 } |
250 } | 250 } |
251 | 251 |
252 main() { | 252 main() { |
253 MathTest.testMain(); | 253 MathTest.testMain(); |
254 } | 254 } |
OLD | NEW |