OLD | NEW |
---|---|
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 assertEquals(0, Math.round(0)); | 28 // Flags: --allow-natives-syntax |
29 assertEquals(-0, Math.round(-0)); | |
30 assertEquals(Infinity, Math.round(Infinity)); | |
31 assertEquals(-Infinity, Math.round(-Infinity)); | |
32 assertNaN(Math.round(NaN)); | |
33 | 29 |
34 assertEquals(1, Math.round(0.5)); | 30 function testRound(expect, input) { |
35 assertEquals(1, Math.round(0.7)); | 31 function doRound(input) { |
36 assertEquals(1, Math.round(1)); | 32 return Math.round(input); |
37 assertEquals(1, Math.round(1.1)); | 33 } |
38 assertEquals(1, Math.round(1.49999)); | 34 assertEquals(expect, doRound(input)); |
William Hesse
2011/04/13 09:12:45
assertEquals(1/expect, 1/doRound(input));
You can
Lasse Reichstein
2011/04/13 09:35:23
Not necessary, assertEquals correctly distinguishe
| |
39 assertEquals(1/-0, 1/Math.round(-0.5)); // Test for -0 result. | 35 %OptimizeFunctionOnNextCall(doRound); |
40 assertEquals(-1, Math.round(-0.5000000000000001)); | 36 assertEquals(expect, doRound(input)); |
41 assertEquals(-1, Math.round(-0.7)); | 37 } |
42 assertEquals(-1, Math.round(-1)); | |
43 assertEquals(-1, Math.round(-1.1)); | |
44 assertEquals(-1, Math.round(-1.49999)); | |
45 assertEquals(-1, Math.round(-1.5)); | |
46 | 38 |
47 assertEquals(9007199254740990, Math.round(9007199254740990)); | 39 testRound(0, 0); |
48 assertEquals(9007199254740991, Math.round(9007199254740991)); | 40 testRound(-0, -0); |
49 assertEquals(-9007199254740990, Math.round(-9007199254740990)); | 41 testRound(Infinity, Infinity); |
50 assertEquals(-9007199254740991, Math.round(-9007199254740991)); | 42 testRound(-Infinity, -Infinity); |
51 assertEquals(Number.MAX_VALUE, Math.round(Number.MAX_VALUE)); | 43 testRound(NaN, NaN); |
52 assertEquals(-Number.MAX_VALUE, Math.round(-Number.MAX_VALUE)); | |
53 | 44 |
54 assertEquals(536870911, Math.round(536870910.5)); | 45 testRound(1, 0.5); |
55 assertEquals(536870911, Math.round(536870911)); | 46 testRound(1, 0.7); |
56 assertEquals(536870911, Math.round(536870911.4)); | 47 testRound(1, 1); |
57 assertEquals(536870912, Math.round(536870911.5)); | 48 testRound(1, 1.1); |
58 assertEquals(536870912, Math.round(536870912)); | 49 testRound(1, 1.49999); |
59 assertEquals(536870912, Math.round(536870912.4)); | 50 testRound(-0, -0.5); |
William Hesse
2011/04/13 09:12:45
To test for -0, you must compare 1/-0 to 1/result,
Lasse Reichstein
2011/04/13 09:35:23
Not any more.
| |
60 assertEquals(536870913, Math.round(536870912.5)); | 51 testRound(-1, -0.5000000000000001); |
61 assertEquals(536870913, Math.round(536870913)); | 52 testRound(-1, -0.7); |
62 assertEquals(536870913, Math.round(536870913.4)); | 53 testRound(-1, -1); |
63 assertEquals(1073741823, Math.round(1073741822.5)); | 54 testRound(-1, -1.1); |
64 assertEquals(1073741823, Math.round(1073741823)); | 55 testRound(-1, -1.49999); |
65 assertEquals(1073741823, Math.round(1073741823.4)); | 56 testRound(-1, -1.5); |
66 assertEquals(1073741824, Math.round(1073741823.5)); | 57 |
67 assertEquals(1073741824, Math.round(1073741824)); | 58 testRound(9007199254740990, 9007199254740990); |
68 assertEquals(1073741824, Math.round(1073741824.4)); | 59 testRound(9007199254740991, 9007199254740991); |
69 assertEquals(1073741825, Math.round(1073741824.5)); | 60 testRound(-9007199254740990, -9007199254740990); |
70 assertEquals(2147483647, Math.round(2147483646.5)); | 61 testRound(-9007199254740991, -9007199254740991); |
71 assertEquals(2147483647, Math.round(2147483647)); | 62 testRound(Number.MAX_VALUE, Number.MAX_VALUE); |
72 assertEquals(2147483647, Math.round(2147483647.4)); | 63 testRound(-Number.MAX_VALUE, -Number.MAX_VALUE); |
73 assertEquals(2147483648, Math.round(2147483647.5)); | 64 |
74 assertEquals(2147483648, Math.round(2147483648)); | 65 testRound(536870911, 536870910.5); |
75 assertEquals(2147483648, Math.round(2147483648.4)); | 66 testRound(536870911, 536870911); |
76 assertEquals(2147483649, Math.round(2147483648.5)); | 67 testRound(536870911, 536870911.4); |
68 testRound(536870912, 536870911.5); | |
69 testRound(536870912, 536870912); | |
70 testRound(536870912, 536870912.4); | |
71 testRound(536870913, 536870912.5); | |
72 testRound(536870913, 536870913); | |
73 testRound(536870913, 536870913.4); | |
74 testRound(1073741823, 1073741822.5); | |
75 testRound(1073741823, 1073741823); | |
76 testRound(1073741823, 1073741823.4); | |
77 testRound(1073741824, 1073741823.5); | |
78 testRound(1073741824, 1073741824); | |
79 testRound(1073741824, 1073741824.4); | |
80 testRound(1073741825, 1073741824.5); | |
81 testRound(2147483647, 2147483646.5); | |
82 testRound(2147483647, 2147483647); | |
83 testRound(2147483647, 2147483647.4); | |
84 testRound(2147483648, 2147483647.5); | |
85 testRound(2147483648, 2147483648); | |
86 testRound(2147483648, 2147483648.4); | |
87 testRound(2147483649, 2147483648.5); | |
77 | 88 |
78 // Tests based on WebKit LayoutTests | 89 // Tests based on WebKit LayoutTests |
79 | 90 |
80 assertEquals(0, Math.round(0.4)); | 91 testRound(0, 0.4); |
81 assertEquals(-0, Math.round(-0.4)); | 92 testRound(-0, -0.4); |
82 assertEquals(-0, Math.round(-0.5)); | 93 testRound(-0, -0.5); |
83 assertEquals(1, Math.round(0.6)); | 94 testRound(1, 0.6); |
84 assertEquals(-1, Math.round(-0.6)); | 95 testRound(-1, -0.6); |
85 assertEquals(2, Math.round(1.5)); | 96 testRound(2, 1.5); |
86 assertEquals(2, Math.round(1.6)); | 97 testRound(2, 1.6); |
87 assertEquals(-2, Math.round(-1.6)); | 98 testRound(-2, -1.6); |
88 assertEquals(8640000000000000, Math.round(8640000000000000)); | 99 testRound(8640000000000000, 8640000000000000); |
89 assertEquals(8640000000000001, Math.round(8640000000000001)); | 100 testRound(8640000000000001, 8640000000000001); |
90 assertEquals(8640000000000002, Math.round(8640000000000002)); | 101 testRound(8640000000000002, 8640000000000002); |
91 assertEquals(9007199254740990, Math.round(9007199254740990)); | 102 testRound(9007199254740990, 9007199254740990); |
92 assertEquals(9007199254740991, Math.round(9007199254740991)); | 103 testRound(9007199254740991, 9007199254740991); |
93 assertEquals(1.7976931348623157e+308, Math.round(1.7976931348623157e+308)); | 104 testRound(1.7976931348623157e+308, 1.7976931348623157e+308); |
94 assertEquals(-8640000000000000, Math.round(-8640000000000000)); | 105 testRound(-8640000000000000, -8640000000000000); |
95 assertEquals(-8640000000000001, Math.round(-8640000000000001)); | 106 testRound(-8640000000000001, -8640000000000001); |
96 assertEquals(-8640000000000002, Math.round(-8640000000000002)); | 107 testRound(-8640000000000002, -8640000000000002); |
97 assertEquals(-9007199254740990, Math.round(-9007199254740990)); | 108 testRound(-9007199254740990, -9007199254740990); |
98 assertEquals(-9007199254740991, Math.round(-9007199254740991)); | 109 testRound(-9007199254740991, -9007199254740991); |
99 assertEquals(-1.7976931348623157e+308, Math.round(-1.7976931348623157e+308)); | 110 testRound(-1.7976931348623157e+308, -1.7976931348623157e+308); |
100 assertEquals(Infinity, Math.round(Infinity)); | 111 testRound(Infinity, Infinity); |
101 assertEquals(-Infinity, Math.round(-Infinity)); | 112 testRound(-Infinity, -Infinity); |
113 | |
114 // Some special double number cases. | |
115 var ulp = Math.pow(2, -1022 - 52); | |
116 var max_denormal = (Math.pow(2, 52) - 1) * ulp; | |
117 var min_normal = Math.pow(2, -1022); | |
118 var max_fraction = Math.pow(2, 52) - 0.5; | |
119 var min_nonfraction = Math.pow(2, 52); | |
120 var max_non_infinite = Number.MAX_VALUE; | |
121 | |
122 var max_smi31 = Math.pow(2,30) - 1; | |
123 var min_smi31 = -Math.pow(2,30); | |
124 var max_smi32 = Math.pow(2,31) - 1; | |
125 var min_smi32 = -Math.pow(2,31); | |
126 | |
127 testRound(0, ulp); | |
128 testRound(0, max_denormal); | |
129 testRound(0, min_normal); | |
130 testRound(0, 0.49999999999999994); | |
131 testRound(1, 0.5); | |
132 testRound(Math.pow(2,52), max_fraction); | |
133 testRound(min_nonfraction, min_nonfraction); | |
134 testRound(max_non_infinite, max_non_infinite); | |
135 | |
136 testRound(max_smi31, max_smi31 - 0.5); | |
137 testRound(max_smi31 + 1, max_smi31 + 0.5); | |
138 testRound(max_smi32, max_smi32 - 0.5); | |
139 testRound(max_smi32 + 1, max_smi32 + 0.5); | |
140 | |
141 testRound(-0, -ulp); | |
142 testRound(-0, -max_denormal); | |
143 testRound(-0, -min_normal); | |
144 testRound(-0, -0.49999999999999994); | |
145 testRound(-0, -0.5); | |
146 testRound(-Math.pow(2,52)+1, -max_fraction); | |
147 testRound(-min_nonfraction, -min_nonfraction); | |
148 testRound(-max_non_infinite, -max_non_infinite); | |
149 | |
150 testRound(min_smi31, min_smi31 - 0.5); | |
151 testRound(min_smi31 + 1, min_smi31 + 0.5); | |
152 testRound(min_smi32, min_smi32 - 0.5); | |
153 testRound(min_smi32 + 1, min_smi32 + 0.5); | |
154 | |
155 | |
OLD | NEW |