Home | History | Annotate | Download | only in webkit
      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(
     25 
     26 "This test checks the behavior of the Math object as described in 15.8 of the language specification."
     27 
     28 );
     29 
     30 shouldBe("Math.abs(NaN)", "NaN");
     31 shouldBe("Math.abs(0)", "0");
     32 shouldBe("Math.abs(-0)", "0");
     33 shouldBe("Math.abs(1)", "1");
     34 shouldBe("Math.abs(-1)", "1");
     35 shouldBe("Math.abs(Number.MIN_VALUE)", "Number.MIN_VALUE");
     36 shouldBe("Math.abs(-Number.MIN_VALUE)", "Number.MIN_VALUE");
     37 shouldBe("Math.abs(Number.MAX_VALUE)", "Number.MAX_VALUE");
     38 shouldBe("Math.abs(-Number.MAX_VALUE)", "Number.MAX_VALUE");
     39 shouldBe("Math.abs(Infinity)", "Infinity");
     40 shouldBe("Math.abs(-Infinity)", "Infinity");
     41 
     42 shouldBe("Math.acos(NaN)", "NaN");
     43 shouldBe("Math.acos(-0)", "Math.acos(0)");
     44 shouldBe("Math.acos(1)", "0");
     45 shouldBe("Math.acos(1.1)", "NaN");
     46 shouldBe("Math.acos(-1.1)", "NaN");
     47 shouldBe("Math.acos(Infinity)", "NaN");
     48 shouldBe("Math.acos(-Infinity)", "NaN");
     49 
     50 shouldBe("Math.asin(NaN)", "NaN");
     51 shouldBe("Math.asin(0)", "0");
     52 shouldBe("Math.asin(-0)", "-0");
     53 shouldBe("Math.asin(1)", "-Math.asin(-1)");
     54 shouldBe("Math.asin(1.1)", "NaN");
     55 shouldBe("Math.asin(-1.1)", "NaN");
     56 shouldBe("Math.asin(Infinity)", "NaN");
     57 shouldBe("Math.asin(-Infinity)", "NaN");
     58 
     59 shouldBe("Math.atan(NaN)", "NaN");
     60 shouldBe("Math.atan(0)", "0");
     61 shouldBe("Math.atan(-0)", "-0");
     62 shouldBe("Math.atan(Infinity)", "-Math.atan(-Infinity)");
     63 
     64 shouldBe("Math.atan2(NaN, NaN)", "NaN");
     65 shouldBe("Math.atan2(NaN, 0)", "NaN");
     66 shouldBe("Math.atan2(NaN, -0)", "NaN");
     67 shouldBe("Math.atan2(NaN, 1)", "NaN");
     68 shouldBe("Math.atan2(NaN, Infinity)", "NaN");
     69 shouldBe("Math.atan2(NaN, -Infinity)", "NaN");
     70 shouldBe("Math.atan2(0, NaN)", "NaN");
     71 shouldBe("Math.atan2(-0, NaN)", "NaN");
     72 shouldBe("Math.atan2(1, NaN)", "NaN");
     73 shouldBe("Math.atan2(Infinity, NaN)", "NaN");
     74 shouldBe("Math.atan2(-Infinity, NaN)", "NaN");
     75 
     76 // Regression test for Bug 26978 (https://bugs.webkit.org/show_bug.cgi?id=26978)
     77 var testStr = "";
     78 var v = { valueOf: function() { testStr += "one"; return 1; } };
     79 var w = { valueOf: function() { testStr += "two"; return 2; } };
     80 Math.atan2(v, w);
     81 shouldBe('testStr', '\"onetwo\"');
     82 
     83 /*
     84  Ify>0andxis+0, theresult isanimplementation-dependent approximationto +/2.
     85  Ify>0andxis0, theresult isanimplementation-dependent approximationto +/2.
     86  Ifyis+0andx>0, theresult is+0.
     87  Ifyis+0andxis+0, theresult is+0.
     88  Ifyis+0andxis0, theresult isanimplementation-dependent approximationto +.
     89  Ifyis+0andx<0, theresult isanimplementation-dependent approximationto +.
     90  Ifyis0andx>0, theresult is0.
     91  Ifyis0andxis+0, theresult is0.
     92  Ifyis0andxis0, theresult isanimplementation-dependent approximationto .
     93  Ifyis0andx<0, theresult isanimplementation-dependent approximationto .
     94  Ify<0andxis+0, theresult isanimplementation-dependent approximationto /2.
     95  Ify<0andxis0, theresult isanimplementation-dependent approximationto /2.
     96  Ify>0andyisfiniteandxis+, theresult is+0.
     97  Ify>0andyisfiniteandxis, theresult ifanimplementation-dependent approximationto +.
     98  Ify<0andyisfiniteandxis+, theresult is0.
     99  Ify<0andyisfiniteandxis, theresult isanimplementation-dependent approximationto.
    100  Ifyis+andxisfinite, theresult isanimplementation-dependent approximationto +/2.
    101  Ifyisandxisfinite, theresult isanimplementation-dependent approximationto /2.
    102  Ifyis+andxis+, theresult isanimplementation-dependent approximationto +/4.
    103  Ifyis+andxis, theresult isanimplementation-dependent approximationto +3/4.
    104  Ifyisandxis+, theresult isanimplementation-dependent approximationto /4.
    105  Ifyisandxis, theresult isanimplementation-dependent approximationto 3/4.
    106 */
    107 
    108 shouldBe("Math.ceil(NaN)", "NaN");
    109 shouldBe("Math.ceil(0)", "0");
    110 shouldBe("Math.ceil(-0)", "-0");
    111 shouldBe("Math.ceil(-0.5)", "-0");
    112 shouldBe("Math.ceil(1)", "1");
    113 shouldBe("Math.ceil(-1)", "-1");
    114 shouldBe("Math.ceil(1.1)", "2");
    115 shouldBe("Math.ceil(-1.1)", "-1");
    116 shouldBe("Math.ceil(Number.MIN_VALUE)", "1");
    117 shouldBe("Math.ceil(-Number.MIN_VALUE)", "-0");
    118 shouldBe("Math.ceil(Number.MAX_VALUE)", "Number.MAX_VALUE");
    119 shouldBe("Math.ceil(-Number.MAX_VALUE)", "-Number.MAX_VALUE");
    120 shouldBe("Math.ceil(Infinity)", "Infinity");
    121 shouldBe("Math.ceil(-Infinity)", "-Infinity");
    122 
    123 shouldBe("Math.cos(NaN)", "NaN");
    124 shouldBe("Math.cos(0)", "1");
    125 shouldBe("Math.cos(-0)", "1");
    126 shouldBe("Math.cos(Infinity)", "NaN");
    127 shouldBe("Math.cos(-Infinity)", "NaN");
    128 
    129 shouldBe("Math.exp(NaN)", "NaN");
    130 shouldBe("Math.exp(0)", "1");
    131 shouldBe("Math.exp(-0)", "1");
    132 shouldBe("Math.exp(Infinity)", "Infinity");
    133 shouldBe("Math.exp(-Infinity)", "0");
    134 
    135 shouldBe("Math.floor(NaN)", "NaN");
    136 shouldBe("Math.floor(0)", "0");
    137 shouldBe("Math.floor(-0)", "-0");
    138 shouldBe("Math.floor(0.5)", "0");
    139 shouldBe("Math.floor(1)", "1");
    140 shouldBe("Math.floor(-1)", "-1");
    141 shouldBe("Math.floor(1.1)", "1");
    142 shouldBe("Math.floor(-1.1)", "-2");
    143 shouldBe("Math.floor(Number.MIN_VALUE)", "0");
    144 shouldBe("Math.floor(-Number.MIN_VALUE)", "-1");
    145 shouldBe("Math.floor(Number.MAX_VALUE)", "Number.MAX_VALUE");
    146 shouldBe("Math.floor(-Number.MAX_VALUE)", "-Number.MAX_VALUE");
    147 shouldBe("Math.floor(Infinity)", "Infinity");
    148 shouldBe("Math.floor(-Infinity)", "-Infinity");
    149 
    150 shouldBe("Math.log(NaN)", "NaN");
    151 shouldBe("Math.log(0)", "-Infinity");
    152 shouldBe("Math.log(-0)", "-Infinity");
    153 shouldBe("Math.log(1)", "0");
    154 shouldBe("Math.log(-1)", "NaN");
    155 shouldBe("Math.log(-1.1)", "NaN");
    156 shouldBe("Math.log(Infinity)", "Infinity");
    157 shouldBe("Math.log(-Infinity)", "NaN");
    158 
    159 shouldBe("Math.max()", "-Infinity");
    160 shouldBe("Math.max(NaN)", "NaN");
    161 shouldBe("Math.max(NaN,1)", "NaN");
    162 shouldBe("Math.max(0)", "0");
    163 shouldBe("Math.max(-0)", "-0");
    164 shouldBe("Math.max(-0, 0)", "0");
    165 
    166 shouldBe("Math.min()", "Infinity");
    167 shouldBe("Math.min(NaN)", "NaN");
    168 shouldBe("Math.min(NaN,1)", "NaN");
    169 shouldBe("Math.min(0)", "0");
    170 shouldBe("Math.min(-0)", "-0");
    171 shouldBe("Math.min(-0, 0)", "-0");
    172 
    173 shouldBe("Math.pow(NaN, NaN)", "NaN");
    174 shouldBe("Math.pow(NaN, 0)", "1");
    175 shouldBe("Math.pow(NaN, -0)", "1");
    176 shouldBe("Math.pow(NaN, 1)", "NaN");
    177 shouldBe("Math.pow(NaN, Infinity)", "NaN");
    178 shouldBe("Math.pow(NaN, -Infinity)", "NaN");
    179 shouldBe("Math.pow(0, NaN)", "NaN");
    180 shouldBe("Math.pow(-0, NaN)", "NaN");
    181 shouldBe("Math.pow(1, NaN)", "NaN");
    182 shouldBe("Math.pow(Infinity, NaN)", "NaN");
    183 shouldBe("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  Ifxisandy>0andyisanoddinteger, theresult is.
    195  Ifxisandy>0andyisnot anoddinteger, theresult is+.
    196  Ifxisandy<0andyisanoddinteger, theresult is0.
    197  Ifxisandy<0andyisnot anoddinteger, theresult is+0.
    198  Ifxis+0andy>0, theresult is+0.
    199  Ifxis+0andy<0, theresult is+.
    200  Ifxis0andy>0andyisanoddinteger, theresult is0.
    201  Ifxis0andy>0andyisnot anoddinteger, theresult is+0.
    202  Ifxis0andy<0andyisanoddinteger, theresult is.
    203  Ifxis0andy<0andyisnot anoddinteger, theresult is+.
    204  Ifx<0andxisfiniteandyisfiniteandyisnot aninteger, theresult isNaN.
    205 */
    206 
    207 shouldBe("Math.round(NaN)", "NaN");
    208 shouldBe("Math.round(0)", "0");
    209 shouldBe("Math.round(-0)", "-0");
    210 shouldBe("Math.round(0.4)", "0");
    211 shouldBe("Math.round(-0.4)", "-0");
    212 shouldBe("Math.round(0.5)", "1");
    213 shouldBe("Math.round(-0.5)", "-0");
    214 shouldBe("Math.round(0.6)", "1");
    215 shouldBe("Math.round(-0.6)", "-1");
    216 shouldBe("Math.round(1)", "1");
    217 shouldBe("Math.round(-1)", "-1");
    218 shouldBe("Math.round(1.1)", "1");
    219 shouldBe("Math.round(-1.1)", "-1");
    220 shouldBe("Math.round(1.5)", "2");
    221 shouldBe("Math.round(-1.5)", "-1");
    222 shouldBe("Math.round(1.6)", "2");
    223 shouldBe("Math.round(-1.6)", "-2");
    224 shouldBe("Math.round(8640000000000000)", "8640000000000000");
    225 // The following is expected. Double can't represent .5 in this case.
    226 shouldBe("Math.round(8640000000000000.5)", "8640000000000000");
    227 shouldBe("Math.round(8640000000000001)", "8640000000000001");
    228 shouldBe("Math.round(8640000000000002)", "8640000000000002");
    229 shouldBe("Math.round(9007199254740990)", "9007199254740990");
    230 shouldBe("Math.round(9007199254740991)", "9007199254740991");
    231 shouldBe("Math.round(1.7976931348623157e+308)", "1.7976931348623157e+308");
    232 shouldBe("Math.round(-8640000000000000)", "-8640000000000000");
    233 shouldBe("Math.round(-8640000000000001)", "-8640000000000001");
    234 shouldBe("Math.round(-8640000000000002)", "-8640000000000002");
    235 shouldBe("Math.round(-9007199254740990)", "-9007199254740990");
    236 shouldBe("Math.round(-9007199254740991)", "-9007199254740991");
    237 shouldBe("Math.round(-1.7976931348623157e+308)", "-1.7976931348623157e+308");
    238 shouldBe("Math.round(Infinity)", "Infinity");
    239 shouldBe("Math.round(-Infinity)", "-Infinity");
    240 
    241 shouldBe("Math.sin(NaN)", "NaN");
    242 shouldBe("Math.sin(0)", "0");
    243 shouldBe("Math.sin(-0)", "-0");
    244 shouldBe("Math.sin(Infinity)", "NaN");
    245 shouldBe("Math.sin(-Infinity)", "NaN");
    246 
    247 shouldBe("Math.sqrt(NaN)", "NaN");
    248 shouldBe("Math.sqrt(0)", "0");
    249 shouldBe("Math.sqrt(-0)", "-0");
    250 shouldBe("Math.sqrt(1)", "1");
    251 shouldBe("Math.sqrt(-1)", "NaN");
    252 shouldBe("Math.sqrt(Infinity)", "Infinity");
    253 shouldBe("Math.sqrt(-Infinity)", "NaN");
    254 
    255 shouldBe("Math.tan(NaN)", "NaN");
    256 shouldBe("Math.tan(0)", "0");
    257 shouldBe("Math.tan(-0)", "-0");
    258 shouldBe("Math.tan(Infinity)", "NaN");
    259 shouldBe("Math.tan(-Infinity)", "NaN");
    260 
    261 var __Math = Math;
    262 shouldBeTrue("delete Math;");
    263 
    264 function global() { return this; }
    265 shouldBeFalse("'Math' in global()");
    266 
    267 Math = __Math;
    268