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