OLD | NEW |
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 // Dart test program to test arithmetic operations. | 4 // Dart test program to test arithmetic operations. |
5 | 5 |
| 6 #library('arithmetic_test'); |
| 7 #import('dart:math'); |
| 8 |
6 class ArithmeticTest { | 9 class ArithmeticTest { |
7 | 10 |
8 static bool exceptionCaughtParseInt(String s) { | 11 static bool exceptionCaughtParseInt(String s) { |
9 try { | 12 try { |
10 Math.parseInt(s); | 13 parseInt(s); |
11 return false; | 14 return false; |
12 } catch (FormatException e) { | 15 } catch (FormatException e) { |
13 return true; | 16 return true; |
14 } | 17 } |
15 } | 18 } |
16 | 19 |
17 static bool exceptionCaughtParseDouble(String s) { | 20 static bool exceptionCaughtParseDouble(String s) { |
18 try { | 21 try { |
19 Math.parseDouble(s); | 22 parseDouble(s); |
20 return false; | 23 return false; |
21 } catch (FormatException e) { | 24 } catch (FormatException e) { |
22 return true; | 25 return true; |
23 } | 26 } |
24 } | 27 } |
25 | 28 |
26 static bool toIntThrowsFormatException(String str) { | 29 static bool toIntThrowsFormatException(String str) { |
27 // No exception allowed for parse double. | 30 // No exception allowed for parse double. |
28 double d = Math.parseDouble(str); | 31 double d = parseDouble(str); |
29 try { | 32 try { |
30 var a = d.toInt(); | 33 var a = d.toInt(); |
31 return false; | 34 return false; |
32 } catch (FormatException e) { | 35 } catch (FormatException e) { |
33 return true; | 36 return true; |
34 } | 37 } |
35 } | 38 } |
36 | 39 |
37 static runOne() { | 40 static runOne() { |
38 var a = 22; | 41 var a = 22; |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 { double d = (0).toDouble(); } | 333 { double d = (0).toDouble(); } |
331 { double d = (1).toDouble(); } | 334 { double d = (1).toDouble(); } |
332 { double d = (-1).toDouble(); } | 335 { double d = (-1).toDouble(); } |
333 // Big. | 336 // Big. |
334 Expect.equals(big, big.toInt()); | 337 Expect.equals(big, big.toInt()); |
335 Expect.equals(-big, (-big).toInt()); | 338 Expect.equals(-big, (-big).toInt()); |
336 { int i = big.toInt(); } | 339 { int i = big.toInt(); } |
337 { int i = (-big).toInt(); } | 340 { int i = (-big).toInt(); } |
338 | 341 |
339 // Math functions. | 342 // Math functions. |
340 Expect.equals(2.0, Math.sqrt(4.0)); | 343 Expect.equals(2.0, sqrt(4.0)); |
341 Expect.approxEquals(1.0, Math.sin(3.14159265 / 2.0)); | 344 Expect.approxEquals(1.0, sin(3.14159265 / 2.0)); |
342 Expect.approxEquals(-1.0, Math.cos(3.14159265)); | 345 Expect.approxEquals(-1.0, cos(3.14159265)); |
343 | 346 |
344 Expect.equals(12, Math.parseInt("12")); | 347 Expect.equals(12, parseInt("12")); |
345 Expect.equals(-12, Math.parseInt("-12")); | 348 Expect.equals(-12, parseInt("-12")); |
346 Expect.equals(12345678901234567890, | 349 Expect.equals(12345678901234567890, |
347 Math.parseInt("12345678901234567890")); | 350 parseInt("12345678901234567890")); |
348 Expect.equals(-12345678901234567890, | 351 Expect.equals(-12345678901234567890, |
349 Math.parseInt("-12345678901234567890")); | 352 parseInt("-12345678901234567890")); |
350 // Type checks. | 353 // Type checks. |
351 { int i = Math.parseInt("12"); } | 354 { int i = parseInt("12"); } |
352 { int i = Math.parseInt("-12"); } | 355 { int i = parseInt("-12"); } |
353 { int i = Math.parseInt("12345678901234567890"); } | 356 { int i = parseInt("12345678901234567890"); } |
354 { int i = Math.parseInt("-12345678901234567890"); } | 357 { int i = parseInt("-12345678901234567890"); } |
355 | 358 |
356 Expect.equals(1.2, Math.parseDouble("1.2")); | 359 Expect.equals(1.2, parseDouble("1.2")); |
357 Expect.equals(-1.2, Math.parseDouble("-1.2")); | 360 Expect.equals(-1.2, parseDouble("-1.2")); |
358 // Type checks. | 361 // Type checks. |
359 { double d = Math.parseDouble("1.2"); } | 362 { double d = parseDouble("1.2"); } |
360 { double d = Math.parseDouble("-1.2"); } | 363 { double d = parseDouble("-1.2"); } |
361 { double d = Math.parseDouble("0"); } | 364 { double d = parseDouble("0"); } |
362 | 365 |
363 // Random | 366 // Random |
364 { double d = Math.random(); } | 367 { |
| 368 Random rand = new Random(); |
| 369 double d = rand.nextDouble(); |
| 370 } |
365 | 371 |
366 Expect.equals(false, exceptionCaughtParseInt("22")); | 372 Expect.equals(false, exceptionCaughtParseInt("22")); |
367 Expect.equals(true, exceptionCaughtParseInt("alpha")); | 373 Expect.equals(true, exceptionCaughtParseInt("alpha")); |
368 Expect.equals(true, exceptionCaughtParseInt("-alpha")); | 374 Expect.equals(true, exceptionCaughtParseInt("-alpha")); |
369 Expect.equals(false, exceptionCaughtParseDouble("22.2")); | 375 Expect.equals(false, exceptionCaughtParseDouble("22.2")); |
370 Expect.equals(true, exceptionCaughtParseDouble("alpha")); | 376 Expect.equals(true, exceptionCaughtParseDouble("alpha")); |
371 Expect.equals(true, exceptionCaughtParseDouble("-alpha")); | 377 Expect.equals(true, exceptionCaughtParseDouble("-alpha")); |
372 | 378 |
373 Expect.equals(false, Math.parseDouble("1.2").isNaN()); | 379 Expect.equals(false, parseDouble("1.2").isNaN()); |
374 Expect.equals(false, Math.parseDouble("1.2").isInfinite()); | 380 Expect.equals(false, parseDouble("1.2").isInfinite()); |
375 | 381 |
376 Expect.equals(true, Math.parseDouble("NaN").isNaN()); | 382 Expect.equals(true, parseDouble("NaN").isNaN()); |
377 Expect.equals(true, Math.parseDouble("Infinity").isInfinite()); | 383 Expect.equals(true, parseDouble("Infinity").isInfinite()); |
378 Expect.equals(true, Math.parseDouble("-Infinity").isInfinite()); | 384 Expect.equals(true, parseDouble("-Infinity").isInfinite()); |
379 | 385 |
380 Expect.equals(false, Math.parseDouble("NaN").isNegative()); | 386 Expect.equals(false, parseDouble("NaN").isNegative()); |
381 Expect.equals(false, Math.parseDouble("Infinity").isNegative()); | 387 Expect.equals(false, parseDouble("Infinity").isNegative()); |
382 Expect.equals(true, Math.parseDouble("-Infinity").isNegative()); | 388 Expect.equals(true, parseDouble("-Infinity").isNegative()); |
383 | 389 |
384 Expect.equals("NaN", Math.parseDouble("NaN").toString()); | 390 Expect.equals("NaN", parseDouble("NaN").toString()); |
385 Expect.equals("Infinity", Math.parseDouble("Infinity").toString()); | 391 Expect.equals("Infinity", parseDouble("Infinity").toString()); |
386 Expect.equals("-Infinity", Math.parseDouble("-Infinity").toString()); | 392 Expect.equals("-Infinity", parseDouble("-Infinity").toString()); |
387 | 393 |
388 Expect.equals(false, toIntThrowsFormatException("1.2")); | 394 Expect.equals(false, toIntThrowsFormatException("1.2")); |
389 Expect.equals(true, toIntThrowsFormatException("Infinity")); | 395 Expect.equals(true, toIntThrowsFormatException("Infinity")); |
390 Expect.equals(true, toIntThrowsFormatException("-Infinity")); | 396 Expect.equals(true, toIntThrowsFormatException("-Infinity")); |
391 Expect.equals(true, toIntThrowsFormatException("NaN")); | 397 Expect.equals(true, toIntThrowsFormatException("NaN")); |
392 | 398 |
393 // Min/max | 399 // Min/max |
394 Expect.equals(1, Math.min(1, 12)); | 400 Expect.equals(1, min(1, 12)); |
395 Expect.equals(12, Math.max(1, 12)); | 401 Expect.equals(12, max(1, 12)); |
396 Expect.equals(1.0, Math.min(1.0, 12.0)); | 402 Expect.equals(1.0, min(1.0, 12.0)); |
397 Expect.equals(12.0, Math.max(1.0, 12.0)); | 403 Expect.equals(12.0, max(1.0, 12.0)); |
398 Expect.equals(false, 1.0 < Math.min(1.0, 12.0)); | 404 Expect.equals(false, 1.0 < min(1.0, 12.0)); |
399 Expect.equals(true, 1.0 < Math.max(1.0, 12.0)); | 405 Expect.equals(true, 1.0 < max(1.0, 12.0)); |
400 | 406 |
401 // Hashcode | 407 // Hashcode |
402 Expect.equals(false, (3.4).hashCode() == (1.2).hashCode()); | 408 Expect.equals(false, (3.4).hashCode() == (1.2).hashCode()); |
403 Expect.equals(true, (1.2).hashCode() == (1.2).hashCode()); | 409 Expect.equals(true, (1.2).hashCode() == (1.2).hashCode()); |
404 Expect.equals(false, (3).hashCode() == (1).hashCode()); | 410 Expect.equals(false, (3).hashCode() == (1).hashCode()); |
405 Expect.equals(true, (10).hashCode() == (10).hashCode()); | 411 Expect.equals(true, (10).hashCode() == (10).hashCode()); |
406 } | 412 } |
407 | 413 |
408 static testMain() { | 414 static testMain() { |
409 for (int i = 0; i < 1500; i++) { | 415 for (int i = 0; i < 1500; i++) { |
410 runOne(); | 416 runOne(); |
411 } | 417 } |
412 } | 418 } |
413 } | 419 } |
414 | 420 |
415 main() { | 421 main() { |
416 ArithmeticTest.testMain(); | 422 ArithmeticTest.testMain(); |
417 } | 423 } |
OLD | NEW |