1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25
26"This test checks the behavior of the Math object as described in 15.8 of the language specification."
27
28);
29
30shouldBe("Math.abs(NaN)", "NaN");
31shouldBe("Math.abs(0)", "0");
32shouldBe("Math.abs(-0)", "0");
33shouldBe("Math.abs(1)", "1");
34shouldBe("Math.abs(-1)", "1");
35shouldBe("Math.abs(Number.MIN_VALUE)", "Number.MIN_VALUE");
36shouldBe("Math.abs(-Number.MIN_VALUE)", "Number.MIN_VALUE");
37shouldBe("Math.abs(Number.MAX_VALUE)", "Number.MAX_VALUE");
38shouldBe("Math.abs(-Number.MAX_VALUE)", "Number.MAX_VALUE");
39shouldBe("Math.abs(Infinity)", "Infinity");
40shouldBe("Math.abs(-Infinity)", "Infinity");
41
42shouldBe("Math.acos(NaN)", "NaN");
43shouldBe("Math.acos(-0)", "Math.acos(0)");
44shouldBe("Math.acos(1)", "0");
45shouldBe("Math.acos(1.1)", "NaN");
46shouldBe("Math.acos(-1.1)", "NaN");
47shouldBe("Math.acos(Infinity)", "NaN");
48shouldBe("Math.acos(-Infinity)", "NaN");
49
50shouldBe("Math.asin(NaN)", "NaN");
51shouldBe("Math.asin(0)", "0");
52shouldBe("Math.asin(-0)", "-0");
53shouldBe("Math.asin(1)", "-Math.asin(-1)");
54shouldBe("Math.asin(1.1)", "NaN");
55shouldBe("Math.asin(-1.1)", "NaN");
56shouldBe("Math.asin(Infinity)", "NaN");
57shouldBe("Math.asin(-Infinity)", "NaN");
58
59shouldBe("Math.atan(NaN)", "NaN");
60shouldBe("Math.atan(0)", "0");
61shouldBe("Math.atan(-0)", "-0");
62shouldBe("Math.atan(Infinity)", "-Math.atan(-Infinity)");
63
64shouldBe("Math.atan2(NaN, NaN)", "NaN");
65shouldBe("Math.atan2(NaN, 0)", "NaN");
66shouldBe("Math.atan2(NaN, -0)", "NaN");
67shouldBe("Math.atan2(NaN, 1)", "NaN");
68shouldBe("Math.atan2(NaN, Infinity)", "NaN");
69shouldBe("Math.atan2(NaN, -Infinity)", "NaN");
70shouldBe("Math.atan2(0, NaN)", "NaN");
71shouldBe("Math.atan2(-0, NaN)", "NaN");
72shouldBe("Math.atan2(1, NaN)", "NaN");
73shouldBe("Math.atan2(Infinity, NaN)", "NaN");
74shouldBe("Math.atan2(-Infinity, NaN)", "NaN");
75
76// Regression test for Bug 26978 (https://bugs.webkit.org/show_bug.cgi?id=26978)
77var testStr = "";
78var v = { valueOf: function() { testStr += "one"; return 1; } };
79var w = { valueOf: function() { testStr += "two"; return 2; } };
80Math.atan2(v, w);
81shouldBe('testStr', '\"onetwo\"');
82
83/*
84• Ify>0andxis+0, theresult isanimplementation-dependent approximationto +π/2.
85• Ify>0andxis−0, theresult isanimplementation-dependent approximationto +π/2.
86• Ifyis+0andx>0, theresult is+0.
87• Ifyis+0andxis+0, theresult is+0.
88• Ifyis+0andxis−0, theresult isanimplementation-dependent approximationto +π.
89• Ifyis+0andx<0, theresult isanimplementation-dependent approximationto +π.
90• Ifyis−0andx>0, theresult is−0.
91• Ifyis−0andxis+0, theresult is−0.
92• Ifyis−0andxis−0, theresult isanimplementation-dependent approximationto −π.
93• Ifyis−0andx<0, theresult isanimplementation-dependent approximationto −π.
94• Ify<0andxis+0, theresult isanimplementation-dependent approximationto −π/2.
95• Ify<0andxis−0, theresult isanimplementation-dependent approximationto −π/2.
96• Ify>0andyisfiniteandxis+∞, theresult is+0.
97• Ify>0andyisfiniteandxis−∞, theresult ifanimplementation-dependent approximationto +π.
98• Ify<0andyisfiniteandxis+∞, theresult is−0.
99• Ify<0andyisfiniteandxis−∞, theresult isanimplementation-dependent approximationto−π.
100• Ifyis+∞andxisfinite, theresult isanimplementation-dependent approximationto +π/2.
101• Ifyis−∞andxisfinite, theresult isanimplementation-dependent approximationto −π/2.
102• Ifyis+∞andxis+∞, theresult isanimplementation-dependent approximationto +π/4.
103• Ifyis+∞andxis−∞, theresult isanimplementation-dependent approximationto +3π/4.
104• Ifyis−∞andxis+∞, theresult isanimplementation-dependent approximationto −π/4.
105• Ifyis−∞andxis−∞, theresult isanimplementation-dependent approximationto −3π/4.
106*/
107
108shouldBe("Math.ceil(NaN)", "NaN");
109shouldBe("Math.ceil(0)", "0");
110shouldBe("Math.ceil(-0)", "-0");
111shouldBe("Math.ceil(-0.5)", "-0");
112shouldBe("Math.ceil(1)", "1");
113shouldBe("Math.ceil(-1)", "-1");
114shouldBe("Math.ceil(1.1)", "2");
115shouldBe("Math.ceil(-1.1)", "-1");
116shouldBe("Math.ceil(Number.MIN_VALUE)", "1");
117shouldBe("Math.ceil(-Number.MIN_VALUE)", "-0");
118shouldBe("Math.ceil(Number.MAX_VALUE)", "Number.MAX_VALUE");
119shouldBe("Math.ceil(-Number.MAX_VALUE)", "-Number.MAX_VALUE");
120shouldBe("Math.ceil(Infinity)", "Infinity");
121shouldBe("Math.ceil(-Infinity)", "-Infinity");
122
123shouldBe("Math.cos(NaN)", "NaN");
124shouldBe("Math.cos(0)", "1");
125shouldBe("Math.cos(-0)", "1");
126shouldBe("Math.cos(Infinity)", "NaN");
127shouldBe("Math.cos(-Infinity)", "NaN");
128
129shouldBe("Math.exp(NaN)", "NaN");
130shouldBe("Math.exp(0)", "1");
131shouldBe("Math.exp(-0)", "1");
132shouldBe("Math.exp(Infinity)", "Infinity");
133shouldBe("Math.exp(-Infinity)", "0");
134
135shouldBe("Math.floor(NaN)", "NaN");
136shouldBe("Math.floor(0)", "0");
137shouldBe("Math.floor(-0)", "-0");
138shouldBe("Math.floor(0.5)", "0");
139shouldBe("Math.floor(1)", "1");
140shouldBe("Math.floor(-1)", "-1");
141shouldBe("Math.floor(1.1)", "1");
142shouldBe("Math.floor(-1.1)", "-2");
143shouldBe("Math.floor(Number.MIN_VALUE)", "0");
144shouldBe("Math.floor(-Number.MIN_VALUE)", "-1");
145shouldBe("Math.floor(Number.MAX_VALUE)", "Number.MAX_VALUE");
146shouldBe("Math.floor(-Number.MAX_VALUE)", "-Number.MAX_VALUE");
147shouldBe("Math.floor(Infinity)", "Infinity");
148shouldBe("Math.floor(-Infinity)", "-Infinity");
149
150shouldBe("Math.log(NaN)", "NaN");
151shouldBe("Math.log(0)", "-Infinity");
152shouldBe("Math.log(-0)", "-Infinity");
153shouldBe("Math.log(1)", "0");
154shouldBe("Math.log(-1)", "NaN");
155shouldBe("Math.log(-1.1)", "NaN");
156shouldBe("Math.log(Infinity)", "Infinity");
157shouldBe("Math.log(-Infinity)", "NaN");
158
159shouldBe("Math.max()", "-Infinity");
160shouldBe("Math.max(NaN)", "NaN");
161shouldBe("Math.max(NaN,1)", "NaN");
162shouldBe("Math.max(0)", "0");
163shouldBe("Math.max(-0)", "-0");
164shouldBe("Math.max(-0, 0)", "0");
165
166shouldBe("Math.min()", "Infinity");
167shouldBe("Math.min(NaN)", "NaN");
168shouldBe("Math.min(NaN,1)", "NaN");
169shouldBe("Math.min(0)", "0");
170shouldBe("Math.min(-0)", "-0");
171shouldBe("Math.min(-0, 0)", "-0");
172
173shouldBe("Math.pow(NaN, NaN)", "NaN");
174shouldBe("Math.pow(NaN, 0)", "1");
175shouldBe("Math.pow(NaN, -0)", "1");
176shouldBe("Math.pow(NaN, 1)", "NaN");
177shouldBe("Math.pow(NaN, Infinity)", "NaN");
178shouldBe("Math.pow(NaN, -Infinity)", "NaN");
179shouldBe("Math.pow(0, NaN)", "NaN");
180shouldBe("Math.pow(-0, NaN)", "NaN");
181shouldBe("Math.pow(1, NaN)", "NaN");
182shouldBe("Math.pow(Infinity, NaN)", "NaN");
183shouldBe("Math.pow(-Infinity, NaN)", "NaN");
184
185/*
186• Ifabs(x)>1andyis+∞, theresult is+∞.
187• Ifabs(x)>1andyis−∞, theresult is+0.
188• Ifabs(x)==1andyis+∞, theresult isNaN.
189• Ifabs(x)==1andyis−∞, theresult isNaN.
190• Ifabs(x)<1andyis+∞, theresult is+0.
191• Ifabs(x)<1andyis−∞, theresult is+∞.
192• Ifxis+∞andy>0, theresult is+∞.
193• Ifxis+∞andy<0, theresult is+0.
194• Ifxis−∞andy>0andyisanoddinteger, theresult is−∞.
195• Ifxis−∞andy>0andyisnot anoddinteger, theresult is+∞.
196• Ifxis−∞andy<0andyisanoddinteger, theresult is−0.
197• Ifxis−∞andy<0andyisnot anoddinteger, theresult is+0.
198• Ifxis+0andy>0, theresult is+0.
199• Ifxis+0andy<0, theresult is+∞.
200• Ifxis−0andy>0andyisanoddinteger, theresult is−0.
201• Ifxis−0andy>0andyisnot anoddinteger, theresult is+0.
202• Ifxis−0andy<0andyisanoddinteger, theresult is−∞.
203• Ifxis−0andy<0andyisnot anoddinteger, theresult is+∞.
204• Ifx<0andxisfiniteandyisfiniteandyisnot aninteger, theresult isNaN.
205*/
206
207shouldBe("Math.round(NaN)", "NaN");
208shouldBe("Math.round(0)", "0");
209shouldBe("Math.round(-0)", "-0");
210shouldBe("Math.round(0.4)", "0");
211shouldBe("Math.round(-0.4)", "-0");
212shouldBe("Math.round(0.5)", "1");
213shouldBe("Math.round(-0.5)", "-0");
214shouldBe("Math.round(0.6)", "1");
215shouldBe("Math.round(-0.6)", "-1");
216shouldBe("Math.round(1)", "1");
217shouldBe("Math.round(-1)", "-1");
218shouldBe("Math.round(1.1)", "1");
219shouldBe("Math.round(-1.1)", "-1");
220shouldBe("Math.round(1.5)", "2");
221shouldBe("Math.round(-1.5)", "-1");
222shouldBe("Math.round(1.6)", "2");
223shouldBe("Math.round(-1.6)", "-2");
224shouldBe("Math.round(8640000000000000)", "8640000000000000");
225// The following is expected. Double can't represent .5 in this case.
226shouldBe("Math.round(8640000000000000.5)", "8640000000000000");
227shouldBe("Math.round(8640000000000001)", "8640000000000001");
228shouldBe("Math.round(8640000000000002)", "8640000000000002");
229shouldBe("Math.round(9007199254740990)", "9007199254740990");
230shouldBe("Math.round(9007199254740991)", "9007199254740991");
231shouldBe("Math.round(1.7976931348623157e+308)", "1.7976931348623157e+308");
232shouldBe("Math.round(-8640000000000000)", "-8640000000000000");
233shouldBe("Math.round(-8640000000000001)", "-8640000000000001");
234shouldBe("Math.round(-8640000000000002)", "-8640000000000002");
235shouldBe("Math.round(-9007199254740990)", "-9007199254740990");
236shouldBe("Math.round(-9007199254740991)", "-9007199254740991");
237shouldBe("Math.round(-1.7976931348623157e+308)", "-1.7976931348623157e+308");
238shouldBe("Math.round(Infinity)", "Infinity");
239shouldBe("Math.round(-Infinity)", "-Infinity");
240
241shouldBe("Math.sin(NaN)", "NaN");
242shouldBe("Math.sin(0)", "0");
243shouldBe("Math.sin(-0)", "-0");
244shouldBe("Math.sin(Infinity)", "NaN");
245shouldBe("Math.sin(-Infinity)", "NaN");
246
247shouldBe("Math.sqrt(NaN)", "NaN");
248shouldBe("Math.sqrt(0)", "0");
249shouldBe("Math.sqrt(-0)", "-0");
250shouldBe("Math.sqrt(1)", "1");
251shouldBe("Math.sqrt(-1)", "NaN");
252shouldBe("Math.sqrt(Infinity)", "Infinity");
253shouldBe("Math.sqrt(-Infinity)", "NaN");
254
255shouldBe("Math.tan(NaN)", "NaN");
256shouldBe("Math.tan(0)", "0");
257shouldBe("Math.tan(-0)", "-0");
258shouldBe("Math.tan(Infinity)", "NaN");
259shouldBe("Math.tan(-Infinity)", "NaN");
260
261var __Math = Math;
262shouldBeTrue("delete Math;");
263
264function global() { return this; }
265shouldBeFalse("'Math' in global()");
266
267Math = __Math;
268