OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // Dart test for testing Math.min and Math.max. |
| 5 |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 negate(x) => -x; |
| 9 |
| 10 main() { |
| 11 // Test matrix: |
| 12 var minNonZero = 5e-324; |
| 13 var maxDenormal = 2.225073858507201e-308; |
| 14 var minNormal = 2.2250738585072014e-308; |
| 15 var maxFraction = 0.9999999999999999; |
| 16 var minAbove1 = 1.0000000000000002; |
| 17 var maxNonInt = 4503599627370495.5; |
| 18 var maxNonIntFloorAsInt = maxNonInt.floor(); |
| 19 var maxNonIntFloorAsDouble = maxNonIntFloorAsInt.toDouble(); |
| 20 var maxExactIntAsDouble = 9007199254740992.0; |
| 21 var maxExactIntAsInt = 9007199254740992; |
| 22 var two53 = 1 << 53; // Same as maxExactIntAsInt. |
| 23 var two53p1 = two53 + 1; |
| 24 var maxFiniteAsDouble = 1.7976931348623157e+308; |
| 25 var maxFiniteAsInt = maxFiniteAsDouble.truncate(); |
| 26 int huge = 1 << 2000; |
| 27 int hugeP1 = huge + 1; |
| 28 var inf = double.INFINITY; |
| 29 var nan = double.NAN; |
| 30 var mnan = negate(nan); |
| 31 var matrix = [ |
| 32 -inf, |
| 33 -hugeP1, |
| 34 -huge, |
| 35 [-maxFiniteAsDouble, -maxFiniteAsInt], |
| 36 -two53p1, |
| 37 [-two53, -maxExactIntAsInt, -maxExactIntAsDouble], |
| 38 -maxNonInt, |
| 39 [-maxNonIntFloorAsDouble, -maxNonIntFloorAsInt], |
| 40 [-499.0, -499], |
| 41 -minAbove1, |
| 42 [-1.0, -1], |
| 43 -maxFraction, |
| 44 -minNormal, |
| 45 -maxDenormal, |
| 46 -minNonZero, |
| 47 -0.0, |
| 48 [0,0, 0], |
| 49 minNonZero, |
| 50 maxDenormal, |
| 51 minNormal, |
| 52 maxFraction, |
| 53 [1.0, 1], |
| 54 minAbove1, |
| 55 [499.0, 499], |
| 56 [maxNonIntFloorAsDouble, maxNonIntFloorAsInt], |
| 57 maxNonInt, |
| 58 [two53, maxExactIntAsInt, maxExactIntAsDouble], |
| 59 two53p1, |
| 60 [maxFiniteAsDouble, maxFiniteAsInt], |
| 61 huge, |
| 62 hugeP1, |
| 63 inf, |
| 64 [nan, mnan], |
| 65 ]; |
| 66 |
| 67 check(left, right, expectedResult) { |
| 68 if (left is List) { |
| 69 for(var x in left) check(x, right, expectedResult); |
| 70 return; |
| 71 } |
| 72 if (right is List) { |
| 73 for(var x in right) check(left, x, expectedResult); |
| 74 return; |
| 75 } |
| 76 int actual = left.compareTo(right); |
| 77 Expect.equals(expectedResult, actual, |
| 78 "($left).compareTo($right) failed " |
| 79 "(should have been $expectedResult, was $actual"); |
| 80 } |
| 81 |
| 82 for (int i = 0; i < matrix.length; i++) { |
| 83 for (int j = 0; j < matrix.length; j++) { |
| 84 var left = matrix[i]; |
| 85 var right = matrix[j]; |
| 86 if (left is List) { |
| 87 check(left, left, 0); |
| 88 } |
| 89 check(left, right, i == j ? 0 : (i < j ? -1 : 1)); |
| 90 } |
| 91 } |
| 92 } |
OLD | NEW |