2 function mathRoundOnIntegers(value
)
4 return Math
.round(value
);
6 noInline(mathRoundOnIntegers
);
8 function mathRoundOnDoubles(value
)
10 return Math
.round(value
);
12 noInline(mathRoundOnDoubles
);
14 function mathRoundOnBooleans(value
)
16 return Math
.round(value
);
18 noInline(mathRoundOnBooleans
);
20 // The trivial cases first.
21 for (var i
= 1; i
< 1e4
; ++i
) {
22 var roundedValue
= mathRoundOnIntegers(i
);
23 if (roundedValue
!== i
)
24 throw "mathRoundOnIntegers(" + i
+ ") = " + roundedValue
;
26 var roundedValue
= mathRoundOnIntegers(-i
);
27 if (roundedValue
!== -i
)
28 throw "mathRoundOnIntegers(" + -i
+ ") = " + roundedValue
;
30 var doubleLow
= i
+ 0.4;
31 var roundedValue
= mathRoundOnDoubles(doubleLow
);
32 if (roundedValue
!== i
)
33 throw "mathRoundOnDoubles(" + doubleLow
+ ") = " + roundedValue
;
35 var doubleHigh
= i
+ 0.6;
36 var roundedValue
= mathRoundOnDoubles(doubleHigh
);
37 if (roundedValue
!== i
+ 1)
38 throw "mathRoundOnDoubles(" + doubleHigh
+ ") = " + roundedValue
;
40 var doubleMid
= i
+ 0.5;
41 var roundedValue
= mathRoundOnDoubles(doubleMid
);
42 if (roundedValue
!== i
+ 1)
43 throw "mathRoundOnDoubles(" + doubleMid
+ ") = " + roundedValue
;
45 var roundedValue
= mathRoundOnDoubles(-0.6);
46 if (roundedValue
!== -1)
47 throw "mathRoundOnDoubles(-0.6) = " + roundedValue
;
50 // Some more interesting cases, some of them well OSR exit when the return value is zero.
51 for (var i
= 0; i
< 1e4
; ++i
) {
52 var roundedValue
= mathRoundOnIntegers(i
);
53 if (roundedValue
!== i
)
54 throw "mathRoundOnIntegers(" + i
+ ") = " + roundedValue
;
56 var roundedValue
= mathRoundOnIntegers(-i
);
57 if (roundedValue
!== -i
)
58 throw "mathRoundOnIntegers(-" + i
+ ") = " + roundedValue
;
60 var roundedValue
= mathRoundOnDoubles(-0.4);
61 if (roundedValue
!== 0)
62 throw "mathRoundOnDoubles(-0.4) = " + roundedValue
;
64 var roundedValue
= mathRoundOnDoubles(-0.5);
65 if (roundedValue
!== 0)
66 throw "mathRoundOnDoubles(-0.5) = " + roundedValue
;
68 var roundedValue
= mathRoundOnDoubles(-0);
69 if (!(roundedValue
=== 0 && (1/roundedValue
) === -Infinity
))
70 throw "mathRoundOnDoubles(-0) = " + roundedValue
;
72 var roundedValue
= mathRoundOnDoubles(NaN
);
73 if (roundedValue
=== roundedValue
)
74 throw "mathRoundOnDoubles(NaN) = " + roundedValue
;
76 var roundedValue
= mathRoundOnDoubles(Number
.POSITIVE_INFINITY
);
77 if (roundedValue
!== Number
.POSITIVE_INFINITY
)
78 throw "mathRoundOnDoubles(Number.POSITIVE_INFINITY) = " + roundedValue
;
80 var roundedValue
= mathRoundOnDoubles(Number
.NEGATIVE_INFINITY
);
81 if (roundedValue
!== Number
.NEGATIVE_INFINITY
)
82 throw "mathRoundOnDoubles(Number.NEGATIVE_INFINITY) = " + roundedValue
;
84 var boolean = !!(i
% 2);
85 var roundedBoolean
= mathRoundOnBooleans(boolean);
86 if (roundedBoolean
!= boolean)
87 throw "mathRoundOnDoubles(" + boolean + ") = " + roundedBoolean
;
90 function uselessMathRound(value
)
92 return Math
.round(value
|0);
94 noInline(uselessMathRound
);
96 for (var i
= 0; i
< 1e4
; ++i
) {
97 var roundedValue
= uselessMathRound(i
);
98 if (roundedValue
!== i
)
99 throw "uselessMathRound(" + i
+ ") = " + roundedValue
;
101 var doubleLow
= i
+ 0.4;
102 var roundedValue
= uselessMathRound(doubleLow
);
103 if (roundedValue
!== i
)
104 throw "uselessMathRound(" + doubleLow
+ ") = " + roundedValue
;
106 var doubleHigh
= i
+ 0.6;
107 var roundedValue
= uselessMathRound(doubleHigh
);
108 if (roundedValue
!== i
)
109 throw "uselessMathRound(" + doubleHigh
+ ") = " + roundedValue
;
111 var doubleMid
= i
+ 0.5;
112 var roundedValue
= uselessMathRound(doubleMid
);
113 if (roundedValue
!== i
)
114 throw "uselessMathRound(" + doubleMid
+ ") = " + roundedValue
;
116 var roundedValue
= uselessMathRound(-0.4);
117 if (roundedValue
!== 0)
118 throw "uselessMathRound(-0.4) = " + roundedValue
;
120 var roundedValue
= uselessMathRound(-0.5);
121 if (roundedValue
!== 0)
122 throw "uselessMathRound(-0.5) = " + roundedValue
;
124 var roundedValue
= uselessMathRound(-0.6);
125 if (roundedValue
!== 0)
126 throw "uselessMathRound(-0.6) = " + roundedValue
;
129 function mathRoundWithOverflow(value
)
131 return Math
.round(value
);
133 noInline(mathRoundWithOverflow
);
135 for (var i
= 0; i
< 1e4
; ++i
) {
136 var bigValue
= 1000000000000;
137 var roundedValue
= mathRoundWithOverflow(bigValue
);
138 if (roundedValue
!== bigValue
)
139 throw "mathRoundWithOverflow(" + bigValue
+ ") = " + roundedValue
;
142 function mathRoundConsumedAsDouble(value
)
144 return Math
.round(value
) * 0.5;
146 noInline(mathRoundConsumedAsDouble
);
148 for (var i
= 0; i
< 1e4
; ++i
) {
149 var doubleValue
= i
+ 0.1;
150 var roundedValue
= mathRoundConsumedAsDouble(doubleValue
);
151 if (roundedValue
!== (i
* 0.5))
152 throw "mathRoundConsumedAsDouble(" + doubleValue
+ ") = " + roundedValue
;
154 var doubleValue
= i
+ 0.6;
155 var roundedValue
= mathRoundConsumedAsDouble(doubleValue
);
156 if (roundedValue
!== ((i
+ 1) * 0.5))
157 throw "mathRoundConsumedAsDouble(" + doubleValue
+ ") = " + roundedValue
;
161 function mathRoundDoesNotCareAboutMinusZero(value
)
163 return Math
.round(value
)|0;
165 noInline(mathRoundDoesNotCareAboutMinusZero
);
167 for (var i
= 0; i
< 1e4
; ++i
) {
168 var doubleMid
= i
+ 0.5;
169 var roundedValue
= mathRoundDoesNotCareAboutMinusZero(doubleMid
);
170 if (roundedValue
!== i
+ 1)
171 throw "mathRoundDoesNotCareAboutMinusZero(" + doubleMid
+ ") = " + roundedValue
;
175 // *** Function arguments. ***
176 function mathRoundNoArguments()
180 noInline(mathRoundNoArguments
);
182 function mathRoundTooManyArguments(a
, b
, c
)
184 return Math
.round(a
, b
, c
);
186 noInline(mathRoundTooManyArguments
);
188 for (var i
= 0; i
< 1e4
; ++i
) {
189 var value
= mathRoundNoArguments();
191 throw "mathRoundNoArguments() = " + value
;
193 var value
= mathRoundTooManyArguments(2.1, 3, 5);
195 throw "mathRoundTooManyArguments() = " + value
;
199 // *** Constant as arguments. ***
200 function testMathRoundOnConstants()
202 var value
= Math
.round(0);
204 throw "Math.round(0) = " + value
;
205 var value
= Math
.round(-0);
206 if (!(value
=== 0 && (1/value
) === -Infinity
))
207 throw "Math.round(-0) = " + value
;
208 var value
= Math
.round(1);
210 throw "Math.round(1) = " + value
;
211 var value
= Math
.round(-1);
213 throw "Math.round(-1) = " + value
;
214 var value
= Math
.round(42);
216 throw "Math.round(42) = " + value
;
217 var value
= Math
.round(-42.2);
219 throw "Math.round(-42.2) = " + value
;
220 var value
= Math
.round(NaN
);
222 throw "Math.round(NaN) = " + value
;
223 var value
= Math
.round(Number
.POSITIVE_INFINITI
);
225 throw "Math.round(Number.POSITIVE_INFINITI) = " + value
;
226 var value
= Math
.round(Number
.NEGATIVE_INFINITI
);
228 throw "Math.round(Number.NEGATIVE_INFINITI) = " + value
;
229 var value
= Math
.round(Math
.E
);
231 throw "Math.round(Math.E) = " + value
;
233 noInline(testMathRoundOnConstants
);
235 for (var i
= 0; i
< 1e4
; ++i
) {
236 testMathRoundOnConstants();
240 // *** Struct transition. ***
241 function mathRoundStructTransition(value
)
243 return Math
.round(value
);
245 noInline(mathRoundStructTransition
);
247 for (var i
= 0; i
< 1e4
; ++i
) {
248 var value
= mathRoundStructTransition(42.5);
250 throw "mathRoundStructTransition(42.5) = " + value
;
253 Math
.round = function() { return arguments
[0] + 5; }
255 var value
= mathRoundStructTransition(42);
257 throw "mathRoundStructTransition(42) after transition = " + value
;