]> git.saurik.com Git - apple/javascriptcore.git/blame - tests/stress/math-round-basics.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / math-round-basics.js
CommitLineData
ed1e77d3
A
1
2function mathRoundOnIntegers(value)
3{
4 return Math.round(value);
5}
6noInline(mathRoundOnIntegers);
7
8function mathRoundOnDoubles(value)
9{
10 return Math.round(value);
11}
12noInline(mathRoundOnDoubles);
13
14function mathRoundOnBooleans(value)
15{
16 return Math.round(value);
17}
18noInline(mathRoundOnBooleans);
19
20// The trivial cases first.
21for (var i = 1; i < 1e4; ++i) {
22 var roundedValue = mathRoundOnIntegers(i);
23 if (roundedValue !== i)
24 throw "mathRoundOnIntegers(" + i + ") = " + roundedValue;
25
26 var roundedValue = mathRoundOnIntegers(-i);
27 if (roundedValue !== -i)
28 throw "mathRoundOnIntegers(" + -i + ") = " + roundedValue;
29
30 var doubleLow = i + 0.4;
31 var roundedValue = mathRoundOnDoubles(doubleLow);
32 if (roundedValue !== i)
33 throw "mathRoundOnDoubles(" + doubleLow + ") = " + roundedValue;
34
35 var doubleHigh = i + 0.6;
36 var roundedValue = mathRoundOnDoubles(doubleHigh);
37 if (roundedValue !== i + 1)
38 throw "mathRoundOnDoubles(" + doubleHigh + ") = " + roundedValue;
39
40 var doubleMid = i + 0.5;
41 var roundedValue = mathRoundOnDoubles(doubleMid);
42 if (roundedValue !== i + 1)
43 throw "mathRoundOnDoubles(" + doubleMid + ") = " + roundedValue;
44
45 var roundedValue = mathRoundOnDoubles(-0.6);
46 if (roundedValue !== -1)
47 throw "mathRoundOnDoubles(-0.6) = " + roundedValue;
48}
49
50// Some more interesting cases, some of them well OSR exit when the return value is zero.
51for (var i = 0; i < 1e4; ++i) {
52 var roundedValue = mathRoundOnIntegers(i);
53 if (roundedValue !== i)
54 throw "mathRoundOnIntegers(" + i + ") = " + roundedValue;
55
56 var roundedValue = mathRoundOnIntegers(-i);
57 if (roundedValue !== -i)
58 throw "mathRoundOnIntegers(-" + i + ") = " + roundedValue;
59
60 var roundedValue = mathRoundOnDoubles(-0.4);
61 if (roundedValue !== 0)
62 throw "mathRoundOnDoubles(-0.4) = " + roundedValue;
63
64 var roundedValue = mathRoundOnDoubles(-0.5);
65 if (roundedValue !== 0)
66 throw "mathRoundOnDoubles(-0.5) = " + roundedValue;
67
68 var roundedValue = mathRoundOnDoubles(-0);
69 if (!(roundedValue === 0 && (1/roundedValue) === -Infinity))
70 throw "mathRoundOnDoubles(-0) = " + roundedValue;
71
72 var roundedValue = mathRoundOnDoubles(NaN);
73 if (roundedValue === roundedValue)
74 throw "mathRoundOnDoubles(NaN) = " + roundedValue;
75
76 var roundedValue = mathRoundOnDoubles(Number.POSITIVE_INFINITY);
77 if (roundedValue !== Number.POSITIVE_INFINITY)
78 throw "mathRoundOnDoubles(Number.POSITIVE_INFINITY) = " + roundedValue;
79
80 var roundedValue = mathRoundOnDoubles(Number.NEGATIVE_INFINITY);
81 if (roundedValue !== Number.NEGATIVE_INFINITY)
82 throw "mathRoundOnDoubles(Number.NEGATIVE_INFINITY) = " + roundedValue;
83
84 var boolean = !!(i % 2);
85 var roundedBoolean = mathRoundOnBooleans(boolean);
86 if (roundedBoolean != boolean)
87 throw "mathRoundOnDoubles(" + boolean + ") = " + roundedBoolean;
88}
89
90function uselessMathRound(value)
91{
92 return Math.round(value|0);
93}
94noInline(uselessMathRound);
95
96for (var i = 0; i < 1e4; ++i) {
97 var roundedValue = uselessMathRound(i);
98 if (roundedValue !== i)
99 throw "uselessMathRound(" + i + ") = " + roundedValue;
100
101 var doubleLow = i + 0.4;
102 var roundedValue = uselessMathRound(doubleLow);
103 if (roundedValue !== i)
104 throw "uselessMathRound(" + doubleLow + ") = " + roundedValue;
105
106 var doubleHigh = i + 0.6;
107 var roundedValue = uselessMathRound(doubleHigh);
108 if (roundedValue !== i)
109 throw "uselessMathRound(" + doubleHigh + ") = " + roundedValue;
110
111 var doubleMid = i + 0.5;
112 var roundedValue = uselessMathRound(doubleMid);
113 if (roundedValue !== i)
114 throw "uselessMathRound(" + doubleMid + ") = " + roundedValue;
115
116 var roundedValue = uselessMathRound(-0.4);
117 if (roundedValue !== 0)
118 throw "uselessMathRound(-0.4) = " + roundedValue;
119
120 var roundedValue = uselessMathRound(-0.5);
121 if (roundedValue !== 0)
122 throw "uselessMathRound(-0.5) = " + roundedValue;
123
124 var roundedValue = uselessMathRound(-0.6);
125 if (roundedValue !== 0)
126 throw "uselessMathRound(-0.6) = " + roundedValue;
127}
128
129function mathRoundWithOverflow(value)
130{
131 return Math.round(value);
132}
133noInline(mathRoundWithOverflow);
134
135for (var i = 0; i < 1e4; ++i) {
136 var bigValue = 1000000000000;
137 var roundedValue = mathRoundWithOverflow(bigValue);
138 if (roundedValue !== bigValue)
139 throw "mathRoundWithOverflow(" + bigValue + ") = " + roundedValue;
140}
141
142function mathRoundConsumedAsDouble(value)
143{
144 return Math.round(value) * 0.5;
145}
146noInline(mathRoundConsumedAsDouble);
147
148for (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;
153
154 var doubleValue = i + 0.6;
155 var roundedValue = mathRoundConsumedAsDouble(doubleValue);
156 if (roundedValue !== ((i + 1) * 0.5))
157 throw "mathRoundConsumedAsDouble(" + doubleValue + ") = " + roundedValue;
158
159}
160
161function mathRoundDoesNotCareAboutMinusZero(value)
162{
163 return Math.round(value)|0;
164}
165noInline(mathRoundDoesNotCareAboutMinusZero);
166
167for (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;
172}
173
174
175// *** Function arguments. ***
176function mathRoundNoArguments()
177{
178 return Math.round();
179}
180noInline(mathRoundNoArguments);
181
182function mathRoundTooManyArguments(a, b, c)
183{
184 return Math.round(a, b, c);
185}
186noInline(mathRoundTooManyArguments);
187
188for (var i = 0; i < 1e4; ++i) {
189 var value = mathRoundNoArguments();
190 if (value === value)
191 throw "mathRoundNoArguments() = " + value;
192
193 var value = mathRoundTooManyArguments(2.1, 3, 5);
194 if (value !== 2)
195 throw "mathRoundTooManyArguments() = " + value;
196}
197
198
199// *** Constant as arguments. ***
200function testMathRoundOnConstants()
201{
202 var value = Math.round(0);
203 if (value !== 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);
209 if (value !== 1)
210 throw "Math.round(1) = " + value;
211 var value = Math.round(-1);
212 if (value !== -1)
213 throw "Math.round(-1) = " + value;
214 var value = Math.round(42);
215 if (value !== 42)
216 throw "Math.round(42) = " + value;
217 var value = Math.round(-42.2);
218 if (value !== -42)
219 throw "Math.round(-42.2) = " + value;
220 var value = Math.round(NaN);
221 if (value === value)
222 throw "Math.round(NaN) = " + value;
223 var value = Math.round(Number.POSITIVE_INFINITI);
224 if (value === value)
225 throw "Math.round(Number.POSITIVE_INFINITI) = " + value;
226 var value = Math.round(Number.NEGATIVE_INFINITI);
227 if (value === value)
228 throw "Math.round(Number.NEGATIVE_INFINITI) = " + value;
229 var value = Math.round(Math.E);
230 if (value !== 3)
231 throw "Math.round(Math.E) = " + value;
232}
233noInline(testMathRoundOnConstants);
234
235for (var i = 0; i < 1e4; ++i) {
236 testMathRoundOnConstants();
237}
238
239
240// *** Struct transition. ***
241function mathRoundStructTransition(value)
242{
243 return Math.round(value);
244}
245noInline(mathRoundStructTransition);
246
247for (var i = 0; i < 1e4; ++i) {
248 var value = mathRoundStructTransition(42.5);
249 if (value !== 43)
250 throw "mathRoundStructTransition(42.5) = " + value;
251}
252
253Math.round = function() { return arguments[0] + 5; }
254
255var value = mathRoundStructTransition(42);
256if (value !== 47)
257 throw "mathRoundStructTransition(42) after transition = " + value;