| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013, 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 // Test num.clamp. |
| 5 |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 // Pedestrian implementation of sign, following its specification directly. |
| 9 num sign(num value) { |
| 10 if (value is int) { |
| 11 if (value < 0) return -1; |
| 12 if (value > 0) return 1; |
| 13 return 0; |
| 14 } |
| 15 if (value.isNaN) return value; |
| 16 if (value == 0.0) return value; |
| 17 if (value > 0.0) return 1.0; |
| 18 return -1.0; |
| 19 } |
| 20 |
| 21 var numbers = [ |
| 22 // Integers |
| 23 0, |
| 24 1, |
| 25 2, |
| 26 0x7f, // ~7 bits |
| 27 0x80, |
| 28 0xff, // ~8 bits |
| 29 0x100, |
| 30 0xffff, // ~16 bits |
| 31 0x10000, |
| 32 0x3fffffff, // ~30 bits (max positive 32-bit tagged smi) |
| 33 0x40000000, |
| 34 0x40000001, |
| 35 0x7fffffff, // ~31 bits |
| 36 0x80000000, |
| 37 0x80000001, |
| 38 0xfffffffff, // ~32 bits |
| 39 0x100000000, |
| 40 0x100000001, |
| 41 0x10000000000000, // ~53 bits |
| 42 0x10000000000001, |
| 43 0x1fffffffffffff, |
| 44 0x20000000000000, |
| 45 0x20000000000001, // first integer not representable as double. |
| 46 0x20000000000002, |
| 47 0x7fffffffffffffff, // ~63 bits |
| 48 0x8000000000000000, |
| 49 0x8000000000000001, |
| 50 0xffffffffffffffff, // ~64 bits |
| 51 0x10000000000000000, |
| 52 0x10000000000000001, |
| 53 // Integers around the max-double range (2^1024, ~1025 bits). |
| 54 0xfffffffffffff800000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000, |
| 55 0xfffffffffffffc00000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000, |
| 56 0x1000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000, |
| 57 // Doubles. |
| 58 0.0, |
| 59 5e-324, // min positive |
| 60 2.225073858507201e-308, // max denormal |
| 61 2.2250738585072014e-308, // min normal |
| 62 0.49999999999999994, // ~0.5 |
| 63 0.5, |
| 64 0.5000000000000001, |
| 65 0.9999999999999999, // ~1.0 |
| 66 1.0, |
| 67 1.0000000000000002, |
| 68 4294967295.0, // ~32 bits |
| 69 4294967296.0, |
| 70 4503599627370495.5, // max fractional |
| 71 4503599627370497.0, |
| 72 9007199254740991.0, |
| 73 9007199254740992.0, // max exact (+1 is not a double) |
| 74 1.7976931348623157e+308, // max finite double |
| 75 1.0 / 0.0, // Infinity |
| 76 0.0 / 0.0, // NaN |
| 77 ]; |
| 78 |
| 79 main() { |
| 80 for (num number in numbers) { |
| 81 test(number); |
| 82 test(-number); |
| 83 } |
| 84 } |
| 85 |
| 86 void test(number) { |
| 87 num expectSign = sign(number); |
| 88 num actualSign = number.sign; |
| 89 if (expectSign.isNaN) { |
| 90 Expect.isTrue(actualSign.isNaN, "$number: $actualSign != NaN"); |
| 91 } else { |
| 92 if (number is int) { |
| 93 Expect.isTrue(actualSign is int, "$number.sign is int"); |
| 94 } else { |
| 95 Expect.isTrue(actualSign is double, "$number.sign is double"); |
| 96 } |
| 97 Expect.equals(expectSign, actualSign, "$number"); |
| 98 Expect.equals(number.isNegative, actualSign.isNegative, "$number:negative"); |
| 99 var renumber = actualSign * number.abs(); |
| 100 Expect.equals(number, renumber, "$number (sign*abs)"); |
| 101 if (number is int) { |
| 102 Expect.isTrue(renumber is int, "$number (sign*abs) is int"); |
| 103 } else { |
| 104 Expect.isTrue(renumber is double, "$number (sign*abs) is double"); |
| 105 } |
| 106 } |
| 107 } |
| OLD | NEW |