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

Side by Side Diff: tests/corelib/math_test.dart

Issue 10825339: Fix issue 4456 (BadNumberFormatException -> FormatException). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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/corelib/math_parse_double_test.dart ('k') | no next file » | 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) 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 // 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
7 // class entirely.
Lasse Reichstein Nielsen 2012/08/14 10:27:12 Which is Real Soon Now. Otherwise I'd suggest para
8 #import('dart:math', prefix: 'math');
9
5 class MathTest { 10 class MathTest {
6 static void testConstants() { 11 static void testConstants() {
7 // Source for mathematical constants is Wolfram Alpha. 12 // Source for mathematical constants is Wolfram Alpha.
8 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669, 13 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669,
9 Math.E); 14 Math.E);
10 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333, 15 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333,
11 Math.LN10); 16 Math.LN10);
12 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206, 17 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206,
13 Math.LN2); 18 Math.LN2);
14 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354, 19 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354,
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 testAtan(); 250 testAtan();
246 testAtan2(); 251 testAtan2();
247 testSqrt(); 252 testSqrt();
248 testLog(); 253 testLog();
249 testExp(); 254 testExp();
250 testPow(); 255 testPow();
251 testParseInt(); 256 testParseInt();
252 } 257 }
253 } 258 }
254 259
260 class MathLibraryTest {
261 static void testConstants() {
262 // Source for mathematical constants is Wolfram Alpha.
263 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669,
264 math.E);
265 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333,
266 math.LN10);
267 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206,
268 math.LN2);
269 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354,
270 math.LOG2E);
271 Expect.equals(0.4342944819032518276511289189166050822943970058036665661144,
272 math.LOG10E);
273 Expect.equals(3.1415926535897932384626433832795028841971693993751058209749,
274 math.PI);
275 Expect.equals(0.7071067811865475244008443621048490392848359376884740365883,
276 math.SQRT1_2);
277 Expect.equals(1.4142135623730950488016887242096980785696718753769480731766,
278 math.SQRT2);
279 }
280
281 static checkClose(double a, double b, EPSILON) {
282 Expect.equals(true, a - EPSILON <= b);
283 Expect.equals(true, b <= a + EPSILON);
284 }
285
286 static void testSin() {
287 // Given the imprecision of PI we can't expect better results than this.
288 final double EPSILON = 1e-15;
289 checkClose(0.0, math.sin(0.0), EPSILON);
290 checkClose(0.0, math.sin(math.PI), EPSILON);
291 checkClose(0.0, math.sin(2.0 * math.PI), EPSILON);
292 checkClose(1.0, math.sin(math.PI / 2.0), EPSILON);
293 checkClose(-1.0, math.sin(math.PI * (3.0 / 2.0)), EPSILON);
294 }
295
296 static void testCos() {
297 // Given the imprecision of PI we can't expect better results than this.
298 final double EPSILON = 1e-15;
299 checkClose(1.0, math.cos(0.0), EPSILON);
300 checkClose(-1.0, math.cos(math.PI), EPSILON);
301 checkClose(1.0, math.cos(2.0 * math.PI), EPSILON);
302 checkClose(0.0, math.cos(math.PI / 2.0), EPSILON);
303 checkClose(0.0, math.cos(math.PI * (3.0 / 2.0)), EPSILON);
304 }
305
306 static void testTan() {
307 // Given the imprecision of PI we can't expect better results than this.
308 final double EPSILON = 1e-15;
309 checkClose(0.0, math.tan(0.0), EPSILON);
310 checkClose(0.0, math.tan(math.PI), EPSILON);
311 checkClose(0.0, math.tan(2.0 * math.PI), EPSILON);
312 checkClose(1.0, math.tan(math.PI / 4.0), EPSILON);
313 }
314
315 static void testAsin() {
316 // Given the imprecision of PI we can't expect better results than this.
317 final double EPSILON = 1e-15;
318 checkClose(0.0, math.asin(0.0), EPSILON);
319 checkClose(math.PI / 2.0, math.asin(1.0), EPSILON);
320 checkClose(-math.PI / 2.0, math.asin(-1.0), EPSILON);
321 }
322
323
324 static void testAcos() {
325 // Given the imprecision of PI we can't expect better results than this.
326 final double EPSILON = 1e-15;
327 checkClose(0.0, math.acos(1.0), EPSILON);
328 checkClose(math.PI, math.acos(-1.0), EPSILON);
329 checkClose(math.PI / 2.0, math.acos(0.0), EPSILON);
330 }
331
332 static void testAtan() {
333 // Given the imprecision of PI we can't expect better results than this.
334 final double EPSILON = 1e-15;
335 checkClose(0.0, math.atan(0.0), EPSILON);
336 checkClose(math.PI / 4.0, math.atan(1.0), EPSILON);
337 checkClose(-math.PI / 4.0, math.atan(-1.0), EPSILON);
338 }
339
340 static void testAtan2() {
341 // Given the imprecision of PI we can't expect better results than this.
342 final double EPSILON = 1e-15;
343 checkClose(0.0, math.atan2(0.0, 5.0), EPSILON);
344 checkClose(math.PI / 4.0, math.atan2(2.0, 2.0), EPSILON);
345 checkClose(3 * math.PI / 4.0, math.atan2(0.5, -0.5), EPSILON);
346 checkClose(-3 * math.PI / 4.0, math.atan2(-2.5, -2.5), EPSILON);
347 }
348
349 static checkVeryClose(double a, double b) {
350 // We find a ulp (unit in the last place) by shifting the original number
351 // to the right. This only works if we are not too close to infinity or if
352 // we work with denormals.
353 // We special case or 0.0, but not for infinity.
354 if (a == 0.0) {
355 final minimalDouble = 4.9406564584124654e-324;
356 Expect.equals(true, b.abs() <= minimalDouble);
357 return;
358 }
359 if (b == 0.0) {
360 // No need to look if they are close. Otherwise the check for 'a' above
361 // whould have triggered.
362 Expect.equals(a, b);
363 }
364 final double shiftRightBy52 = 2.220446049250313080847263336181640625e-16;
365 final double shiftedA = (a * shiftRightBy52).abs();
366 // Compared to 'a', 'shiftedA' is now ~1-2 ulp.
367
368 final double limitLow = a - shiftedA;
369 final double limitHigh = a + shiftedA;
370 Expect.equals(false, a == limitLow);
371 Expect.equals(false, a == limitHigh);
372 Expect.equals(true, limitLow <= b);
373 Expect.equals(true, b <= limitHigh);
374 }
375
376 static void testSqrt() {
377 checkVeryClose(2.0, math.sqrt(4.0));
378 checkVeryClose(math.SQRT2, math.sqrt(2.0));
379 checkVeryClose(math.SQRT1_2, math.sqrt(0.5));
380 checkVeryClose(1e50, math.sqrt(1e100));
381 checkVeryClose(1.1111111061110855443054405046358901279277111935183977e56,
382 math.sqrt(12345678901234e99));
383 }
384
385 static void testExp() {
386 checkVeryClose(math.E, math.exp(1.0));
387 final EPSILON = 1e-15;
388 checkClose(10.0, math.exp(math.LN10), EPSILON);
389 checkClose(2.0, math.exp(math.LN2), EPSILON);
390 }
391
392 static void testLog() {
393 // Even though E is imprecise, it is good enough to get really close to 1.
394 // We still provide an epsilon.
395 checkClose(1.0, math.log(math.E), 1e-16);
396 checkVeryClose(math.LN10, math.log(10.0));
397 checkVeryClose(math.LN2, math.log(2.0));
398 }
399
400 static void testPow() {
401 checkVeryClose(16.0, math.pow(4.0, 2.0));
402 checkVeryClose(math.SQRT2, math.pow(2.0, 0.5));
403 checkVeryClose(math.SQRT1_2, math.pow(0.5, 0.5));
404 }
405
406 static bool parseIntThrowsFormatException(str) {
407 try {
408 math.parseInt(str);
409 return false;
410 } catch (FormatException e) {
411 return true;
412 }
413 }
414
415 static void testParseInt() {
416 Expect.equals(499, math.parseInt("499"));
417 Expect.equals(499, math.parseInt("+499"));
418 Expect.equals(-499, math.parseInt("-499"));
419 Expect.equals(499, math.parseInt(" 499 "));
420 Expect.equals(499, math.parseInt(" +499 "));
421 Expect.equals(-499, math.parseInt(" -499 "));
422 Expect.equals(0, math.parseInt("0"));
423 Expect.equals(0, math.parseInt("+0"));
424 Expect.equals(0, math.parseInt("-0"));
425 Expect.equals(0, math.parseInt(" 0 "));
426 Expect.equals(0, math.parseInt(" +0 "));
427 Expect.equals(0, math.parseInt(" -0 "));
428 Expect.equals(0x1234567890, math.parseInt("0x1234567890"));
429 Expect.equals(-0x1234567890, math.parseInt("-0x1234567890"));
430 Expect.equals(0x1234567890, math.parseInt(" 0x1234567890 "));
431 Expect.equals(-0x1234567890, math.parseInt(" -0x1234567890 "));
432 Expect.equals(256, math.parseInt("0x100"));
433 Expect.equals(-256, math.parseInt("-0x100"));
434 Expect.equals(256, math.parseInt(" 0x100 "));
435 Expect.equals(-256, math.parseInt(" -0x100 "));
436 Expect.equals(0xabcdef, math.parseInt("0xabcdef"));
437 Expect.equals(0xABCDEF, math.parseInt("0xABCDEF"));
438 Expect.equals(0xabcdef, math.parseInt("0xabCDEf"));
439 Expect.equals(-0xabcdef, math.parseInt("-0xabcdef"));
440 Expect.equals(-0xABCDEF, math.parseInt("-0xABCDEF"));
441 Expect.equals(0xabcdef, math.parseInt(" 0xabcdef "));
442 Expect.equals(0xABCDEF, math.parseInt(" 0xABCDEF "));
443 Expect.equals(-0xabcdef, math.parseInt(" -0xabcdef "));
444 Expect.equals(-0xABCDEF, math.parseInt(" -0xABCDEF "));
445 Expect.equals(0xabcdef, math.parseInt("0x00000abcdef"));
446 Expect.equals(0xABCDEF, math.parseInt("0x00000ABCDEF"));
447 Expect.equals(-0xabcdef, math.parseInt("-0x00000abcdef"));
448 Expect.equals(-0xABCDEF, math.parseInt("-0x00000ABCDEF"));
449 Expect.equals(0xabcdef, math.parseInt(" 0x00000abcdef "));
450 Expect.equals(0xABCDEF, math.parseInt(" 0x00000ABCDEF "));
451 Expect.equals(-0xabcdef, math.parseInt(" -0x00000abcdef "));
452 Expect.equals(-0xABCDEF, math.parseInt(" -0x00000ABCDEF "));
453 Expect.equals(10, math.parseInt("010"));
454 Expect.equals(-10, math.parseInt("-010"));
455 Expect.equals(10, math.parseInt(" 010 "));
456 Expect.equals(-10, math.parseInt(" -010 "));
457 Expect.equals(9, math.parseInt("09"));
458 Expect.equals(9, math.parseInt(" 09 "));
459 Expect.equals(-9, math.parseInt("-09"));
460 Expect.equals(true, parseIntThrowsFormatException("1b"));
461 Expect.equals(true, parseIntThrowsFormatException(" 1b "));
462 Expect.equals(true, parseIntThrowsFormatException(" 1 b "));
463 Expect.equals(true, parseIntThrowsFormatException("1e2"));
464 Expect.equals(true, parseIntThrowsFormatException(" 1e2 "));
465 Expect.equals(true, parseIntThrowsFormatException("00x12"));
466 Expect.equals(true, parseIntThrowsFormatException(" 00x12 "));
467 Expect.equals(true, parseIntThrowsFormatException("-1b"));
468 Expect.equals(true, parseIntThrowsFormatException(" -1b "));
469 Expect.equals(true, parseIntThrowsFormatException(" -1 b "));
470 Expect.equals(true, parseIntThrowsFormatException("-1e2"));
471 Expect.equals(true, parseIntThrowsFormatException(" -1e2 "));
472 Expect.equals(true, parseIntThrowsFormatException("-00x12"));
473 Expect.equals(true, parseIntThrowsFormatException(" -00x12 "));
474 Expect.equals(true, parseIntThrowsFormatException(" -00x12 "));
475 Expect.equals(true, parseIntThrowsFormatException("0x0x12"));
476 Expect.equals(true, parseIntThrowsFormatException("0.1"));
477 Expect.equals(true, parseIntThrowsFormatException("0x3.1"));
478 Expect.equals(true, parseIntThrowsFormatException("5."));
479 Expect.equals(true, parseIntThrowsFormatException("+-5"));
480 Expect.equals(true, parseIntThrowsFormatException("-+5"));
481 Expect.equals(true, parseIntThrowsFormatException("--5"));
482 Expect.equals(true, parseIntThrowsFormatException("++5"));
483 Expect.equals(true, parseIntThrowsFormatException("+ 5"));
484 Expect.equals(true, parseIntThrowsFormatException("- 5"));
485 Expect.equals(true, parseIntThrowsFormatException(""));
486 Expect.equals(true, parseIntThrowsFormatException(" "));
487 Expect.equals(true, parseIntThrowsFormatException("+0x1234567890"));
488 Expect.equals(true, parseIntThrowsFormatException(" +0x1234567890 "));
489 Expect.equals(true, parseIntThrowsFormatException("+0x100"));
490 Expect.equals(true, parseIntThrowsFormatException(" +0x100 "));
491 }
492
493 static testMain() {
494 testConstants();
495 testSin();
496 testCos();
497 testTan();
498 testAsin();
499 testAcos();
500 testAtan();
501 testAtan2();
502 testSqrt();
503 testLog();
504 testExp();
505 testPow();
506 testParseInt();
507 }
508 }
509
255 main() { 510 main() {
256 MathTest.testMain(); 511 MathTest.testMain();
512 MathLibraryTest.testMain();
257 } 513 }
OLDNEW
« no previous file with comments | « tests/corelib/math_parse_double_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698