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