Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: tests/lib/math/math2_test.dart

Issue 10829459: Deprecate Math object in corelib in favor of dart:math library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/language/math_vm_test.dart ('k') | tests/lib/math/math_parse_double_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // We temporarily test both the new math library and the old Math 5 // We temporarily test both the new math library and the old Math
6 // class. This can easily be simplified once we get rid of the Math 6 // class. This can easily be simplified once we get rid of the Math
7 // class entirely. 7 // class entirely.
8 #library('math_test'); 8 #library('math_test');
9 #import('dart:math', prefix: 'math'); 9 #import('dart:math', prefix: 'math');
10 10
11 class MathTest {
12 static void testConstants() {
13 // Source for mathematical constants is Wolfram Alpha.
14 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669,
15 Math.E);
16 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333,
17 Math.LN10);
18 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206,
19 Math.LN2);
20 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354,
21 Math.LOG2E);
22 Expect.equals(0.4342944819032518276511289189166050822943970058036665661144,
23 Math.LOG10E);
24 Expect.equals(3.1415926535897932384626433832795028841971693993751058209749,
25 Math.PI);
26 Expect.equals(0.7071067811865475244008443621048490392848359376884740365883,
27 Math.SQRT1_2);
28 Expect.equals(1.4142135623730950488016887242096980785696718753769480731766,
29 Math.SQRT2);
30 }
31
32 static checkClose(double a, double b, EPSILON) {
33 Expect.equals(true, a - EPSILON <= b);
34 Expect.equals(true, b <= a + EPSILON);
35 }
36
37 static void testSin() {
38 // Given the imprecision of PI we can't expect better results than this.
39 final double EPSILON = 1e-15;
40 checkClose(0.0, Math.sin(0.0), EPSILON);
41 checkClose(0.0, Math.sin(Math.PI), EPSILON);
42 checkClose(0.0, Math.sin(2.0 * Math.PI), EPSILON);
43 checkClose(1.0, Math.sin(Math.PI / 2.0), EPSILON);
44 checkClose(-1.0, Math.sin(Math.PI * (3.0 / 2.0)), EPSILON);
45 }
46
47 static void testCos() {
48 // Given the imprecision of PI we can't expect better results than this.
49 final double EPSILON = 1e-15;
50 checkClose(1.0, Math.cos(0.0), EPSILON);
51 checkClose(-1.0, Math.cos(Math.PI), EPSILON);
52 checkClose(1.0, Math.cos(2.0 * Math.PI), EPSILON);
53 checkClose(0.0, Math.cos(Math.PI / 2.0), EPSILON);
54 checkClose(0.0, Math.cos(Math.PI * (3.0 / 2.0)), EPSILON);
55 }
56
57 static void testTan() {
58 // Given the imprecision of PI we can't expect better results than this.
59 final double EPSILON = 1e-15;
60 checkClose(0.0, Math.tan(0.0), EPSILON);
61 checkClose(0.0, Math.tan(Math.PI), EPSILON);
62 checkClose(0.0, Math.tan(2.0 * Math.PI), EPSILON);
63 checkClose(1.0, Math.tan(Math.PI / 4.0), EPSILON);
64 }
65
66 static void testAsin() {
67 // Given the imprecision of PI we can't expect better results than this.
68 final double EPSILON = 1e-15;
69 checkClose(0.0, Math.asin(0.0), EPSILON);
70 checkClose(Math.PI / 2.0, Math.asin(1.0), EPSILON);
71 checkClose(-Math.PI / 2.0, Math.asin(-1.0), EPSILON);
72 }
73
74
75 static void testAcos() {
76 // Given the imprecision of PI we can't expect better results than this.
77 final double EPSILON = 1e-15;
78 checkClose(0.0, Math.acos(1.0), EPSILON);
79 checkClose(Math.PI, Math.acos(-1.0), EPSILON);
80 checkClose(Math.PI / 2.0, Math.acos(0.0), EPSILON);
81 }
82
83 static void testAtan() {
84 // Given the imprecision of PI we can't expect better results than this.
85 final double EPSILON = 1e-15;
86 checkClose(0.0, Math.atan(0.0), EPSILON);
87 checkClose(Math.PI / 4.0, Math.atan(1.0), EPSILON);
88 checkClose(-Math.PI / 4.0, Math.atan(-1.0), EPSILON);
89 }
90
91 static void testAtan2() {
92 // Given the imprecision of PI we can't expect better results than this.
93 final double EPSILON = 1e-15;
94 checkClose(0.0, Math.atan2(0.0, 5.0), EPSILON);
95 checkClose(Math.PI / 4.0, Math.atan2(2.0, 2.0), EPSILON);
96 checkClose(3 * Math.PI / 4.0, Math.atan2(0.5, -0.5), EPSILON);
97 checkClose(-3 * Math.PI / 4.0, Math.atan2(-2.5, -2.5), EPSILON);
98 }
99
100 static checkVeryClose(double a, double b) {
101 // We find a ulp (unit in the last place) by shifting the original number
102 // to the right. This only works if we are not too close to infinity or if
103 // we work with denormals.
104 // We special case or 0.0, but not for infinity.
105 if (a == 0.0) {
106 final minimalDouble = 4.9406564584124654e-324;
107 Expect.equals(true, b.abs() <= minimalDouble);
108 return;
109 }
110 if (b == 0.0) {
111 // No need to look if they are close. Otherwise the check for 'a' above
112 // whould have triggered.
113 Expect.equals(a, b);
114 }
115 final double shiftRightBy52 = 2.220446049250313080847263336181640625e-16;
116 final double shiftedA = (a * shiftRightBy52).abs();
117 // Compared to 'a', 'shiftedA' is now ~1-2 ulp.
118
119 final double limitLow = a - shiftedA;
120 final double limitHigh = a + shiftedA;
121 Expect.equals(false, a == limitLow);
122 Expect.equals(false, a == limitHigh);
123 Expect.equals(true, limitLow <= b);
124 Expect.equals(true, b <= limitHigh);
125 }
126
127 static void testSqrt() {
128 checkVeryClose(2.0, Math.sqrt(4.0));
129 checkVeryClose(Math.SQRT2, Math.sqrt(2.0));
130 checkVeryClose(Math.SQRT1_2, Math.sqrt(0.5));
131 checkVeryClose(1e50, Math.sqrt(1e100));
132 checkVeryClose(1.1111111061110855443054405046358901279277111935183977e56,
133 Math.sqrt(12345678901234e99));
134 }
135
136 static void testExp() {
137 checkVeryClose(Math.E, Math.exp(1.0));
138 final EPSILON = 1e-15;
139 checkClose(10.0, Math.exp(Math.LN10), EPSILON);
140 checkClose(2.0, Math.exp(Math.LN2), EPSILON);
141 }
142
143 static void testLog() {
144 // Even though E is imprecise, it is good enough to get really close to 1.
145 // We still provide an epsilon.
146 checkClose(1.0, Math.log(Math.E), 1e-16);
147 checkVeryClose(Math.LN10, Math.log(10.0));
148 checkVeryClose(Math.LN2, Math.log(2.0));
149 }
150
151 static void testPow() {
152 checkVeryClose(16.0, Math.pow(4.0, 2.0));
153 checkVeryClose(Math.SQRT2, Math.pow(2.0, 0.5));
154 checkVeryClose(Math.SQRT1_2, Math.pow(0.5, 0.5));
155 }
156
157 static bool parseIntThrowsFormatException(str) {
158 try {
159 Math.parseInt(str);
160 return false;
161 } catch (FormatException e) {
162 return true;
163 }
164 }
165
166 static void testParseInt() {
167 Expect.equals(499, Math.parseInt("499"));
168 Expect.equals(499, Math.parseInt("+499"));
169 Expect.equals(-499, Math.parseInt("-499"));
170 Expect.equals(499, Math.parseInt(" 499 "));
171 Expect.equals(499, Math.parseInt(" +499 "));
172 Expect.equals(-499, Math.parseInt(" -499 "));
173 Expect.equals(0, Math.parseInt("0"));
174 Expect.equals(0, Math.parseInt("+0"));
175 Expect.equals(0, Math.parseInt("-0"));
176 Expect.equals(0, Math.parseInt(" 0 "));
177 Expect.equals(0, Math.parseInt(" +0 "));
178 Expect.equals(0, Math.parseInt(" -0 "));
179 Expect.equals(0x1234567890, Math.parseInt("0x1234567890"));
180 Expect.equals(-0x1234567890, Math.parseInt("-0x1234567890"));
181 Expect.equals(0x1234567890, Math.parseInt(" 0x1234567890 "));
182 Expect.equals(-0x1234567890, Math.parseInt(" -0x1234567890 "));
183 Expect.equals(256, Math.parseInt("0x100"));
184 Expect.equals(-256, Math.parseInt("-0x100"));
185 Expect.equals(256, Math.parseInt(" 0x100 "));
186 Expect.equals(-256, Math.parseInt(" -0x100 "));
187 Expect.equals(0xabcdef, Math.parseInt("0xabcdef"));
188 Expect.equals(0xABCDEF, Math.parseInt("0xABCDEF"));
189 Expect.equals(0xabcdef, Math.parseInt("0xabCDEf"));
190 Expect.equals(-0xabcdef, Math.parseInt("-0xabcdef"));
191 Expect.equals(-0xABCDEF, Math.parseInt("-0xABCDEF"));
192 Expect.equals(0xabcdef, Math.parseInt(" 0xabcdef "));
193 Expect.equals(0xABCDEF, Math.parseInt(" 0xABCDEF "));
194 Expect.equals(-0xabcdef, Math.parseInt(" -0xabcdef "));
195 Expect.equals(-0xABCDEF, Math.parseInt(" -0xABCDEF "));
196 Expect.equals(0xabcdef, Math.parseInt("0x00000abcdef"));
197 Expect.equals(0xABCDEF, Math.parseInt("0x00000ABCDEF"));
198 Expect.equals(-0xabcdef, Math.parseInt("-0x00000abcdef"));
199 Expect.equals(-0xABCDEF, Math.parseInt("-0x00000ABCDEF"));
200 Expect.equals(0xabcdef, Math.parseInt(" 0x00000abcdef "));
201 Expect.equals(0xABCDEF, Math.parseInt(" 0x00000ABCDEF "));
202 Expect.equals(-0xabcdef, Math.parseInt(" -0x00000abcdef "));
203 Expect.equals(-0xABCDEF, Math.parseInt(" -0x00000ABCDEF "));
204 Expect.equals(10, Math.parseInt("010"));
205 Expect.equals(-10, Math.parseInt("-010"));
206 Expect.equals(10, Math.parseInt(" 010 "));
207 Expect.equals(-10, Math.parseInt(" -010 "));
208 Expect.equals(9, Math.parseInt("09"));
209 Expect.equals(9, Math.parseInt(" 09 "));
210 Expect.equals(-9, Math.parseInt("-09"));
211 Expect.equals(true, parseIntThrowsFormatException("1b"));
212 Expect.equals(true, parseIntThrowsFormatException(" 1b "));
213 Expect.equals(true, parseIntThrowsFormatException(" 1 b "));
214 Expect.equals(true, parseIntThrowsFormatException("1e2"));
215 Expect.equals(true, parseIntThrowsFormatException(" 1e2 "));
216 Expect.equals(true, parseIntThrowsFormatException("00x12"));
217 Expect.equals(true, parseIntThrowsFormatException(" 00x12 "));
218 Expect.equals(true, parseIntThrowsFormatException("-1b"));
219 Expect.equals(true, parseIntThrowsFormatException(" -1b "));
220 Expect.equals(true, parseIntThrowsFormatException(" -1 b "));
221 Expect.equals(true, parseIntThrowsFormatException("-1e2"));
222 Expect.equals(true, parseIntThrowsFormatException(" -1e2 "));
223 Expect.equals(true, parseIntThrowsFormatException("-00x12"));
224 Expect.equals(true, parseIntThrowsFormatException(" -00x12 "));
225 Expect.equals(true, parseIntThrowsFormatException(" -00x12 "));
226 Expect.equals(true, parseIntThrowsFormatException("0x0x12"));
227 Expect.equals(true, parseIntThrowsFormatException("0.1"));
228 Expect.equals(true, parseIntThrowsFormatException("0x3.1"));
229 Expect.equals(true, parseIntThrowsFormatException("5."));
230 Expect.equals(true, parseIntThrowsFormatException("+-5"));
231 Expect.equals(true, parseIntThrowsFormatException("-+5"));
232 Expect.equals(true, parseIntThrowsFormatException("--5"));
233 Expect.equals(true, parseIntThrowsFormatException("++5"));
234 Expect.equals(true, parseIntThrowsFormatException("+ 5"));
235 Expect.equals(true, parseIntThrowsFormatException("- 5"));
236 Expect.equals(true, parseIntThrowsFormatException(""));
237 Expect.equals(true, parseIntThrowsFormatException(" "));
238 Expect.equals(true, parseIntThrowsFormatException("+0x1234567890"));
239 Expect.equals(true, parseIntThrowsFormatException(" +0x1234567890 "));
240 Expect.equals(true, parseIntThrowsFormatException("+0x100"));
241 Expect.equals(true, parseIntThrowsFormatException(" +0x100 "));
242 }
243
244 static testMain() {
245 testConstants();
246 testSin();
247 testCos();
248 testTan();
249 testAsin();
250 testAcos();
251 testAtan();
252 testAtan2();
253 testSqrt();
254 testLog();
255 testExp();
256 testPow();
257 testParseInt();
258 }
259 }
260
261 class MathLibraryTest { 11 class MathLibraryTest {
262 static void testConstants() { 12 static void testConstants() {
263 // Source for mathematical constants is Wolfram Alpha. 13 // Source for mathematical constants is Wolfram Alpha.
264 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669, 14 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669,
265 math.E); 15 math.E);
266 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333, 16 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333,
267 math.LN10); 17 math.LN10);
268 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206, 18 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206,
269 math.LN2); 19 math.LN2);
270 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354, 20 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354,
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 testAtan2(); 252 testAtan2();
503 testSqrt(); 253 testSqrt();
504 testLog(); 254 testLog();
505 testExp(); 255 testExp();
506 testPow(); 256 testPow();
507 testParseInt(); 257 testParseInt();
508 } 258 }
509 } 259 }
510 260
511 main() { 261 main() {
512 MathTest.testMain();
513 MathLibraryTest.testMain(); 262 MathLibraryTest.testMain();
514 } 263 }
OLDNEW
« no previous file with comments | « tests/language/math_vm_test.dart ('k') | tests/lib/math/math_parse_double_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698