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("KDE JS Test");
25var negativeZero = Math.atan2(-1, Infinity); // ### any nicer way?
26
27function isNegativeZero(n)
28{
29  return n == 0 && 1 / n < 0;
30}
31
32// self tests
33shouldBeTrue("isNegativeZero(negativeZero)");
34shouldBeFalse("isNegativeZero(0)");
35
36// Constants
37shouldBe("String()+Math.E", "'2.718281828459045'");
38shouldBe("String()+Math.LN2", "'0.6931471805599453'");
39shouldBe("String()+Math.LN10", "'2.302585092994046'");
40shouldBe("String()+Math.LOG2E", "'1.4426950408889634'");
41shouldBe("String()+Math.LOG10E", "'0.4342944819032518'");
42shouldBe("String()+Math.PI", "'3.141592653589793'");
43shouldBe("String()+Math.SQRT1_2", "'0.7071067811865476'");
44shouldBe("String()+Math.SQRT2", "'1.4142135623730951'");
45
46shouldBe("String()+Number.NaN", "'NaN'");
47shouldBe("String()+Number.NEGATIVE_INFINITY", "'-Infinity'");
48shouldBe("String()+Number.POSITIVE_INFINITY", "'Infinity'");
49
50// Functions
51shouldBe("Math.abs(-5)", "5");
52shouldBe("Math.acos(0)", "Math.PI/2");
53shouldBe("Math.acos(1)", "0");
54shouldBe("Math.ceil(1.1)", "2");
55shouldBe("String()+Math.sqrt(2)", "String()+Math.SQRT2");
56shouldBe("Math.ceil(1.6)", "2");
57shouldBe("Math.round(0)", "0");
58shouldBeFalse("isNegativeZero(Math.round(0))");
59shouldBeTrue("isNegativeZero(Math.round(negativeZero))");
60shouldBe("Math.round(0.2)", "0");
61shouldBeTrue("isNegativeZero(Math.round(-0.2))");
62shouldBeTrue("isNegativeZero(Math.round(-0.5))");
63shouldBe("Math.round(1.1)", "1");
64shouldBe("Math.round(1.6)", "2");
65shouldBe("Math.round(-3.5)", "-3");
66shouldBe("Math.round(-3.6)", "-4");
67shouldBeTrue("isNaN(Math.round())");
68shouldBeTrue("isNaN(Math.round(NaN))");
69shouldBe("Math.round(-Infinity)", "-Infinity");
70shouldBe("Math.round(Infinity)", "Infinity");
71shouldBe("Math.round(99999999999999999999.99)", "100000000000000000000");
72shouldBe("Math.round(-99999999999999999999.99)", "-100000000000000000000");
73
74// Math.log()
75shouldBe("Math.log(Math.E*Math.E)", "2");
76shouldBeTrue("isNaN(Math.log(NaN))");
77shouldBeTrue("isNaN(Math.log(-1))");
78shouldBeFalse("isFinite(Math.log(0))");
79shouldBe("Math.log(1)", "0");
80shouldBeFalse("isFinite(Math.log(Infinity))");
81
82// Math.min()
83shouldBeTrue("isNegativeZero(Math.min(negativeZero, 0))");
84
85// Math.max()
86shouldBeFalse("isFinite(Math.max())");
87shouldBe("Math.max(1)", "1"); // NS 4.x and IE 5.x seem to know about 2 arg version only
88shouldBe("Math.max(1, 2, 3)", "3"); // NS 4.x and IE 5.x seem to know about 2 arg version only
89shouldBeTrue("isNaN(Math.max(1,NaN,3))");
90shouldBeTrue("!isNegativeZero(Math.max(negativeZero, 0))");
91
92
93list=""
94for ( var i in Math ) { list += i + ','; }
95shouldBe("list","''");
96
97var my = new Object;
98my.v = 1;
99
100// Deleting/assigning
101shouldBe("delete my.v", "true")
102shouldBeUndefined("my.v");
103shouldBe("delete Math.PI", "false")
104function myfunc( num ) { return num+1; }
105shouldBe("my = myfunc, myfunc(4)", "5");
106
107// Conversions
108shouldBe("Boolean(Math)", "true");
109shouldBeTrue("isNaN(Number(Math));");
110
111// Unicity
112shouldBe("Math.abs===Math.abs", "true")
113shouldBe("Math.abs===Math.round", "false")
114
115// Iteration
116obj = new Object;
117obj.a = 1;
118obj.b = 2;
119list=""
120for ( var i in obj ) { list += i + ','; }
121shouldBe("list","'a,b,'");
122
123// (check that Math's properties and functions are not enumerable)
124list=""
125for ( var i in Math ) { list += i + ','; }
126shouldBe("list","''");
127
128Math.myprop=true; // adding a custom property to the math object (why not?)
129list=""
130for ( var i in Math ) { list += i + ','; }
131shouldBe("list","'myprop,'");
132