File indexing completed on 2024-04-28 15:28:38

0001 var negativeZero = Math.atan2(-1, Infinity); // ### any nicer way?
0002 
0003 function isNegativeZero(n)
0004 {
0005   return n == 0 && 1 / n < 0;
0006 }
0007 
0008 function isPositiveZero(n)
0009 {
0010   return n == 0 && 1 / n > 0;
0011 }
0012 
0013 // self tests
0014 shouldBeTrue("isNegativeZero(negativeZero)");
0015 shouldBeFalse("isNegativeZero(0)");
0016 
0017 // Constants
0018 shouldBe("String()+Math.E", "'2.718281828459045'");
0019 shouldBe("String()+Math.LN2", "'0.6931471805599453'");
0020 shouldBe("String()+Math.LN10", "'2.302585092994046'");
0021 shouldBe("String()+Math.LOG2E", "'1.4426950408889634'");
0022 shouldBe("String()+Math.LOG10E", "'0.43429448190325176'");
0023 shouldBe("String()+Math.PI", "'3.141592653589793'");
0024 shouldBe("String()+Math.SQRT1_2", "'0.7071067811865476'");
0025 shouldBe("String()+Math.SQRT2", "'1.4142135623730951'");
0026 
0027 shouldBe("String()+Number.NaN", "'NaN'");
0028 shouldBe("String()+Number.NEGATIVE_INFINITY", "'-Infinity'");
0029 shouldBe("String()+Number.POSITIVE_INFINITY", "'Infinity'");
0030 
0031 // Functions
0032 shouldBe("Math.abs(-5)", "5");
0033 shouldBe("Math.acos(0)", "Math.PI/2");
0034 shouldBe("Math.acos(1)", "0");
0035 shouldBe("Math.ceil(1.1)", "2");
0036 shouldBe("String()+Math.sqrt(2)", "String()+Math.SQRT2");
0037 shouldBe("Math.ceil(1.6)", "2");
0038 shouldBe("Math.round(Math.exp(3))", "20");
0039 shouldBeTrue("isNaN(Math.exp(NaN))");
0040 shouldBe("Math.exp(+0)", "1");
0041 shouldBe("Math.exp(-0)", "1");
0042 shouldBeFalse("isFinite(Math.exp(Infinity))");
0043 shouldBeTrue("isPositiveZero(Math.exp(-Infinity))");
0044 shouldBe("Math.round(0)", "0");
0045 shouldBeFalse("isNegativeZero(Math.round(0))");
0046 shouldBeTrue("isNegativeZero(Math.round(negativeZero))");
0047 shouldBe("Math.round(0.2)", "0");
0048 shouldBeTrue("isNegativeZero(Math.round(-0.2))");
0049 shouldBeTrue("isNegativeZero(Math.round(-0.5))");
0050 shouldBe("Math.round(1.1)", "1");
0051 shouldBe("Math.round(1.6)", "2");
0052 shouldBe("Math.round(-3.5)", "-3");
0053 shouldBe("Math.round(-3.6)", "-4");
0054 shouldBeTrue("isNaN(Math.round())");
0055 shouldBeTrue("isNaN(Math.round(NaN))");
0056 shouldBe("Math.round(-Infinity)", "-Infinity");
0057 shouldBe("Math.round(Infinity)", "Infinity");
0058 shouldBe("Math.round(99999999999999999999.99)", "100000000000000000000");
0059 shouldBe("Math.round(-99999999999999999999.99)", "-100000000000000000000");
0060 
0061 // Math.log()
0062 shouldBe("Math.log(Math.E*Math.E)", "2");
0063 shouldBeTrue("isNaN(Math.log(NaN))");
0064 shouldBeTrue("isNaN(Math.log(-1))");
0065 shouldBeFalse("isFinite(Math.log(0))");
0066 shouldBe("Math.log(1)", "0");
0067 shouldBeFalse("isFinite(Math.log(Infinity))");
0068 
0069 // Math.min()
0070 shouldBeTrue("Math.min() > 0");
0071 shouldBe("Math.min(1)", "1");
0072 shouldBe("Math.min(2, 1)", "1");
0073 shouldBe("Math.min(3, 2, 1)", "1");
0074 shouldBeTrue("isNaN(Math.min(1,NaN,3))");
0075 shouldBeTrue("isNegativeZero(Math.min(negativeZero))");
0076 shouldBeTrue("isNegativeZero(Math.min(negativeZero, 0))");
0077 
0078 // Math.max()
0079 shouldBeFalse("isFinite(Math.max())");
0080 shouldBeTrue("Math.max() < 0");
0081 shouldBe("Math.max(1)", "1"); // NS 4.x and IE 5.x seem to know about 2 arg version only
0082 shouldBe("Math.max(1, 2, 3)", "3"); // NS 4.x and IE 5.x seem to know about 2 arg version only
0083 shouldBeTrue("isNaN(Math.max(1,NaN,3))");
0084 shouldBeTrue("isNegativeZero(Math.max(negativeZero))");
0085 shouldBeTrue("!isNegativeZero(Math.max(negativeZero, 0))");
0086 
0087 
0088 list=""
0089 for ( var i in Math ) { list += i + ','; }
0090 shouldBe("list","''");
0091 
0092 var my = new Object;
0093 my.v = 1;
0094 
0095 // Deleting/assigning
0096 shouldBe("delete my.v", "true")
0097 shouldBeUndefined("my.v");
0098 shouldBe("delete Math.PI", "false")
0099 function myfunc( num ) { return num+1; }
0100 shouldBe("my = myfunc, myfunc(4)", "5");
0101 
0102 // Conversions
0103 shouldBe("Boolean(Math)", "true");
0104 shouldBeTrue("isNaN(Number(Math));");
0105 
0106 // Unicity
0107 shouldBe("Math.abs===Math.abs", "true")
0108 shouldBe("Math.abs===Math.round", "false")
0109 
0110 // Iteration
0111 obj = new Object;
0112 obj.a = 1;
0113 obj.b = 2;
0114 list=""
0115 for ( var i in obj ) { list += i + ','; }
0116 shouldBe("list","'a,b,'");
0117 
0118 // (check that Math's properties and functions are not enumerable)
0119 list=""
0120 for ( var i in Math ) { list += i + ','; }
0121 shouldBe("list","''");
0122 
0123 Math.myprop=true; // adding a custom property to the math object (why not?)
0124 list=""
0125 for ( var i in Math ) { list += i + ','; }
0126 shouldBe("list","'myprop,'");
0127