| OLD | NEW |
| 1 // Copyright (c) 2012, 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 // Dart test program to test arithmetic operations. | 4 // Dart test program to test arithmetic operations. |
| 5 | 5 |
| 6 library arithmetic_test; | 6 library arithmetic_test; |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 | 8 |
| 9 class ArithmeticTest { | 9 class ArithmeticTest { |
| 10 | 10 |
| 11 static bool exceptionCaughtParseInt(String s) { | 11 static bool exceptionCaughtParseInt(String s) { |
| 12 try { | 12 try { |
| 13 parseInt(s); | 13 int.parse(s); |
| 14 return false; | 14 return false; |
| 15 } on FormatException catch (e) { | 15 } on FormatException catch (e) { |
| 16 return true; | 16 return true; |
| 17 } | 17 } |
| 18 } | 18 } |
| 19 | 19 |
| 20 static bool exceptionCaughtParseDouble(String s) { | 20 static bool exceptionCaughtParseDouble(String s) { |
| 21 try { | 21 try { |
| 22 parseDouble(s); | 22 double.parse(s); |
| 23 return false; | 23 return false; |
| 24 } on FormatException catch (e) { | 24 } on FormatException catch (e) { |
| 25 return true; | 25 return true; |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 static bool toIntThrowsFormatException(String str) { | 29 static bool toIntThrowsUnsupportedError(String str) { |
| 30 // No exception allowed for parse double. | 30 // No exception allowed for parse double. |
| 31 double d = parseDouble(str); | 31 double d = double.parse(str); |
| 32 try { | 32 try { |
| 33 var a = d.toInt(); | 33 var a = d.toInt(); |
| 34 return false; | 34 return false; |
| 35 } on FormatException catch (e) { | 35 } on UnsupportedError catch (e) { |
| 36 return true; | 36 return true; |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 static runOne() { | 40 static runOne() { |
| 41 var a = 22; | 41 var a = 22; |
| 42 var b = 4; | 42 var b = 4; |
| 43 // Smi & smi. | 43 // Smi & smi. |
| 44 Expect.equals(26, a + b); | 44 Expect.equals(26, a + b); |
| 45 Expect.equals(18, a - b); | 45 Expect.equals(18, a - b); |
| 46 Expect.equals(88, a * b); | 46 Expect.equals(88, a * b); |
| 47 Expect.equals(5, a ~/ b); | 47 Expect.equals(5, a ~/ b); |
| 48 Expect.equals(5.5, a / b); | 48 Expect.equals(5.5, a / b); |
| 49 Expect.equals(2.0, 10 / 5); | 49 Expect.equals(2.0, 10 / 5); |
| 50 Expect.equals(2, a % b); | 50 Expect.equals(2, a % b); |
| 51 Expect.equals(2, a.remainder(b)); | 51 Expect.equals(2, a.remainder(b)); |
| 52 // Smi corner cases. | 52 // Smi corner cases. |
| 53 for (int i = 0; i < 80; i++) { | 53 for (int i = 0; i < 80; i++) { |
| 54 a = -(1 << i); | 54 a = -(1 << i); |
| 55 b = -1; | 55 b = -1; |
| 56 Expect.equals(1 << i, a ~/ b); | 56 Expect.equals(1 << i, a ~/ b); |
| 57 } | 57 } |
| 58 a = 22; | 58 a = 22; |
| 59 b = 4.0; | 59 b = 4.0; |
| 60 // Smi & double. | 60 // Smi & double. |
| 61 Expect.equals(26.0, a + b); | 61 Expect.equals(26.0, a + b); |
| 62 Expect.equals(18.0, a - b); | 62 Expect.equals(18.0, a - b); |
| 63 Expect.equals(88.0, a * b); | 63 Expect.equals(88.0, a * b); |
| 64 Expect.equals(5.0, a ~/ b); | 64 Expect.equals(5, a ~/ b); |
| 65 Expect.equals(5.5, a / b); | 65 Expect.equals(5.5, a / b); |
| 66 Expect.equals(2.0, a % b); | 66 Expect.equals(2.0, a % b); |
| 67 Expect.equals(2.0, a.remainder(b)); | 67 Expect.equals(2.0, a.remainder(b)); |
| 68 a = 22.0; | 68 a = 22.0; |
| 69 b = 4; | 69 b = 4; |
| 70 // Double & smi. | 70 // Double & smi. |
| 71 Expect.equals(26.0, a + b); | 71 Expect.equals(26.0, a + b); |
| 72 Expect.equals(18.0, a - b); | 72 Expect.equals(18.0, a - b); |
| 73 Expect.equals(88.0, a * b); | 73 Expect.equals(88.0, a * b); |
| 74 Expect.equals(5.0, a ~/ b); | 74 Expect.equals(5, a ~/ b); |
| 75 Expect.equals(5.5, a / b); | 75 Expect.equals(5.5, a / b); |
| 76 Expect.equals(2.0, a % b); | 76 Expect.equals(2.0, a % b); |
| 77 Expect.equals(2.0, a.remainder(b)); | 77 Expect.equals(2.0, a.remainder(b)); |
| 78 a = 22.0; | 78 a = 22.0; |
| 79 b = 4.0; | 79 b = 4.0; |
| 80 // Double & double. | 80 // Double & double. |
| 81 Expect.equals(26.0, a + b); | 81 Expect.equals(26.0, a + b); |
| 82 Expect.equals(18.0, a - b); | 82 Expect.equals(18.0, a - b); |
| 83 Expect.equals(88.0, a * b); | 83 Expect.equals(88.0, a * b); |
| 84 Expect.equals(5.0, a ~/ b); | 84 Expect.equals(5.0, a ~/ b); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 Expect.equals(big, big.toInt()); | 338 Expect.equals(big, big.toInt()); |
| 339 Expect.equals(-big, (-big).toInt()); | 339 Expect.equals(-big, (-big).toInt()); |
| 340 { int i = big.toInt(); } | 340 { int i = big.toInt(); } |
| 341 { int i = (-big).toInt(); } | 341 { int i = (-big).toInt(); } |
| 342 | 342 |
| 343 // Math functions. | 343 // Math functions. |
| 344 Expect.equals(2.0, sqrt(4.0)); | 344 Expect.equals(2.0, sqrt(4.0)); |
| 345 Expect.approxEquals(1.0, sin(3.14159265 / 2.0)); | 345 Expect.approxEquals(1.0, sin(3.14159265 / 2.0)); |
| 346 Expect.approxEquals(-1.0, cos(3.14159265)); | 346 Expect.approxEquals(-1.0, cos(3.14159265)); |
| 347 | 347 |
| 348 Expect.equals(12, parseInt("12")); | 348 Expect.equals(12, int.parse("12")); |
| 349 Expect.equals(-12, parseInt("-12")); | 349 Expect.equals(-12, int.parse("-12")); |
| 350 Expect.equals(12345678901234567890, | 350 Expect.equals(12345678901234567890, |
| 351 parseInt("12345678901234567890")); | 351 int.parse("12345678901234567890")); |
| 352 Expect.equals(-12345678901234567890, | 352 Expect.equals(-12345678901234567890, |
| 353 parseInt("-12345678901234567890")); | 353 int.parse("-12345678901234567890")); |
| 354 // Type checks. | 354 // Type checks. |
| 355 { int i = parseInt("12"); } | 355 { int i = int.parse("12"); } |
| 356 { int i = parseInt("-12"); } | 356 { int i = int.parse("-12"); } |
| 357 { int i = parseInt("12345678901234567890"); } | 357 { int i = int.parse("12345678901234567890"); } |
| 358 { int i = parseInt("-12345678901234567890"); } | 358 { int i = int.parse("-12345678901234567890"); } |
| 359 | 359 |
| 360 Expect.equals(1.2, parseDouble("1.2")); | 360 Expect.equals(1.2, double.parse("1.2")); |
| 361 Expect.equals(-1.2, parseDouble("-1.2")); | 361 Expect.equals(-1.2, double.parse("-1.2")); |
| 362 // Type checks. | 362 // Type checks. |
| 363 { double d = parseDouble("1.2"); } | 363 { double d = double.parse("1.2"); } |
| 364 { double d = parseDouble("-1.2"); } | 364 { double d = double.parse("-1.2"); } |
| 365 { double d = parseDouble("0"); } | 365 { double d = double.parse("0"); } |
| 366 | 366 |
| 367 // Random | 367 // Random |
| 368 { | 368 { |
| 369 Random rand = new Random(); | 369 Random rand = new Random(); |
| 370 double d = rand.nextDouble(); | 370 double d = rand.nextDouble(); |
| 371 } | 371 } |
| 372 | 372 |
| 373 Expect.equals(false, exceptionCaughtParseInt("22")); | 373 Expect.equals(false, exceptionCaughtParseInt("22")); |
| 374 Expect.equals(true, exceptionCaughtParseInt("alpha")); | 374 Expect.equals(true, exceptionCaughtParseInt("alpha")); |
| 375 Expect.equals(true, exceptionCaughtParseInt("-alpha")); | 375 Expect.equals(true, exceptionCaughtParseInt("-alpha")); |
| 376 Expect.equals(false, exceptionCaughtParseDouble("22.2")); | 376 Expect.equals(false, exceptionCaughtParseDouble("22.2")); |
| 377 Expect.equals(true, exceptionCaughtParseDouble("alpha")); | 377 Expect.equals(true, exceptionCaughtParseDouble("alpha")); |
| 378 Expect.equals(true, exceptionCaughtParseDouble("-alpha")); | 378 Expect.equals(true, exceptionCaughtParseDouble("-alpha")); |
| 379 | 379 |
| 380 Expect.equals(false, parseDouble("1.2").isNaN); | 380 Expect.equals(false, double.parse("1.2").isNaN); |
| 381 Expect.equals(false, parseDouble("1.2").isInfinite); | 381 Expect.equals(false, double.parse("1.2").isInfinite); |
| 382 | 382 |
| 383 Expect.equals(true, parseDouble("NaN").isNaN); | 383 Expect.equals(true, double.parse("NaN").isNaN); |
| 384 Expect.equals(true, parseDouble("Infinity").isInfinite); | 384 Expect.equals(true, double.parse("Infinity").isInfinite); |
| 385 Expect.equals(true, parseDouble("-Infinity").isInfinite); | 385 Expect.equals(true, double.parse("-Infinity").isInfinite); |
| 386 | 386 |
| 387 Expect.equals(false, parseDouble("NaN").isNegative); | 387 Expect.equals(false, double.parse("NaN").isNegative); |
| 388 Expect.equals(false, parseDouble("Infinity").isNegative); | 388 Expect.equals(false, double.parse("Infinity").isNegative); |
| 389 Expect.equals(true, parseDouble("-Infinity").isNegative); | 389 Expect.equals(true, double.parse("-Infinity").isNegative); |
| 390 | 390 |
| 391 Expect.equals("NaN", parseDouble("NaN").toString()); | 391 Expect.equals("NaN", double.parse("NaN").toString()); |
| 392 Expect.equals("Infinity", parseDouble("Infinity").toString()); | 392 Expect.equals("Infinity", double.parse("Infinity").toString()); |
| 393 Expect.equals("-Infinity", parseDouble("-Infinity").toString()); | 393 Expect.equals("-Infinity", double.parse("-Infinity").toString()); |
| 394 | 394 |
| 395 Expect.equals(false, toIntThrowsFormatException("1.2")); | 395 Expect.equals(false, toIntThrowsUnsupportedError("1.2")); |
| 396 Expect.equals(true, toIntThrowsFormatException("Infinity")); | 396 Expect.equals(true, toIntThrowsUnsupportedError("Infinity")); |
| 397 Expect.equals(true, toIntThrowsFormatException("-Infinity")); | 397 Expect.equals(true, toIntThrowsUnsupportedError("-Infinity")); |
| 398 Expect.equals(true, toIntThrowsFormatException("NaN")); | 398 Expect.equals(true, toIntThrowsUnsupportedError("NaN")); |
| 399 | 399 |
| 400 // Min/max | 400 // Min/max |
| 401 Expect.equals(1, min(1, 12)); | 401 Expect.equals(1, min(1, 12)); |
| 402 Expect.equals(12, max(1, 12)); | 402 Expect.equals(12, max(1, 12)); |
| 403 Expect.equals(1.0, min(1.0, 12.0)); | 403 Expect.equals(1.0, min(1.0, 12.0)); |
| 404 Expect.equals(12.0, max(1.0, 12.0)); | 404 Expect.equals(12.0, max(1.0, 12.0)); |
| 405 Expect.equals(false, 1.0 < min(1.0, 12.0)); | 405 Expect.equals(false, 1.0 < min(1.0, 12.0)); |
| 406 Expect.equals(true, 1.0 < max(1.0, 12.0)); | 406 Expect.equals(true, 1.0 < max(1.0, 12.0)); |
| 407 | 407 |
| 408 // Hashcode | 408 // Hashcode |
| (...skipping 15 matching lines...) Expand all Loading... |
| 424 for (int i = 0; i < 1500; i++) { | 424 for (int i = 0; i < 1500; i++) { |
| 425 runOne(); | 425 runOne(); |
| 426 testSmiDivDeopt(); | 426 testSmiDivDeopt(); |
| 427 } | 427 } |
| 428 } | 428 } |
| 429 } | 429 } |
| 430 | 430 |
| 431 main() { | 431 main() { |
| 432 ArithmeticTest.testMain(); | 432 ArithmeticTest.testMain(); |
| 433 } | 433 } |
| OLD | NEW |