Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 
     29 // Flags: --nodead-code-elimination --fold-constants --allow-natives-syntax --nostress-opt
     30 
     31 function test(f) {
     32   f();
     33   f();
     34   %OptimizeFunctionOnNextCall(f);
     35   f();
     36   // Assert that there has been no deopt.
     37   assertOptimized(f);
     38 }
     39 
     40 test(function add() {
     41   assertEquals(2, 1 + 1);
     42   assertEquals(2.5, 1.25 + 1.25);
     43   assertEquals("Infinity", String(Infinity + Infinity));
     44   assertEquals("Infinity", String(Infinity + 3));
     45   assertEquals("NaN", String(Infinity + (-Infinity)));
     46   assertEquals("NaN", String(NaN + 2));
     47   assertEquals("-Infinity", String(1 / (-0.0 + (-0.0))));
     48   assertEquals("Infinity", String(1 / (-0.0 + 0.0)));
     49 });
     50 
     51 test(function inc() {
     52   var a = 1;
     53   var b = Infinity;
     54   var c = -Infinity;
     55   var d = NaN;
     56   assertEquals(2, ++a);
     57   assertEquals("Infinity", String(++b));
     58   assertEquals("-Infinity", String(++c));
     59   assertEquals("NaN", String(++d));
     60 });
     61 
     62 test(function dec() {
     63   var a = 1;
     64   var b = Infinity;
     65   var c = -Infinity;
     66   var d = NaN;
     67   assertEquals(0, --a);
     68   assertEquals("Infinity", String(--b));
     69   assertEquals("-Infinity", String(--c));
     70   assertEquals("NaN", String(--d));
     71 });
     72 
     73 test(function sub() {
     74   assertEquals(0, 1 - 1);
     75   assertEquals(0.5, 1.5 - 1);
     76   assertEquals("Infinity", String(Infinity - (-Infinity)));
     77   assertEquals("Infinity", String(Infinity - 3));
     78   assertEquals("NaN", String(Infinity - Infinity));
     79   assertEquals("NaN", String(NaN - 2));
     80   assertEquals("-Infinity", String(1 / (-0.0 - 0.0)));
     81   assertEquals("Infinity", String(1 / (0.0 - 0.0)));
     82 });
     83 
     84 test(function mul() {
     85   assertEquals(1, 1 * 1);
     86   assertEquals(2.25, 1.5 * 1.5);
     87   assertEquals("Infinity", String(Infinity * Infinity));
     88   assertEquals("-Infinity", String(Infinity * (-Infinity)));
     89   assertEquals("Infinity", String(Infinity * 3));
     90   assertEquals("-Infinity", String(Infinity * (-3)));
     91   assertEquals("NaN", String(NaN * 3));
     92   assertEquals("-Infinity", String(1 / (-0.0 * 0.0)));
     93   assertEquals("Infinity", String(1 / (0.0 * 0.0)));
     94 });
     95 
     96 test(function div() {
     97   assertEquals(1, 1 / 1);
     98   assertEquals(1.5, 2.25 / 1.5);
     99   assertEquals("NaN", String(Infinity / Infinity));
    100   assertEquals("Infinity", String(Infinity / 3));
    101   assertEquals("-Infinity", String(Infinity / (-3)));
    102   assertEquals("NaN", String(NaN / 3));
    103   assertEquals("-Infinity", String(1 / (-0.0)));
    104   assertEquals("Infinity", String(Infinity/0.0));
    105 });
    106 
    107 test(function mathMin() {
    108   assertEquals(1, Math.min(1, 10));
    109   assertEquals(1.5, Math.min(1.5, 2.5));
    110   assertEquals(0, Math.min(Infinity, 0));
    111   assertEquals("Infinity", String(Math.min(Infinity, Infinity)));
    112   assertEquals("-Infinity", String(Math.min(Infinity, -Infinity)));
    113   assertEquals("NaN", String(Math.min(NaN, 1)));
    114   assertEquals("Infinity", String(1 / Math.min(0.0, 0.0)));
    115   assertEquals("-Infinity", String(1 / Math.min(-0.0, -0.0)));
    116   assertEquals("-Infinity", String(1 / Math.min(0.0, -0.0)));
    117 });
    118 
    119 test(function mathMax() {
    120   assertEquals(10, Math.max(1, 10));
    121   assertEquals(2.5, Math.max(1.5, 2.5));
    122   assertEquals(Infinity, Math.max(Infinity, 0));
    123   assertEquals("-Infinity", String(Math.max(-Infinity, -Infinity)));
    124   assertEquals("Infinity", String(Math.max(Infinity, -Infinity)));
    125   assertEquals("NaN", String(Math.max(NaN, 1)));
    126   assertEquals("Infinity", String(1 / Math.max(0.0, 0.0)));
    127   assertEquals("-Infinity", String(1 / Math.max(-0.0, -0.0)));
    128   assertEquals("Infinity", String(1 / Math.max(0.0, -0.0)));
    129 });
    130 
    131 test(function mathExp() {
    132   assertEquals(1.0, Math.exp(0.0));
    133   assertTrue(2.7 < Math.exp(1) && Math.exp(1) < 2.8);
    134   assertEquals("Infinity", String(Math.exp(Infinity)));
    135   assertEquals("0", String(Math.exp(-Infinity)));
    136   assertEquals("NaN", String(Math.exp(NaN)));
    137 });
    138 
    139 test(function mathLog() {
    140   assertEquals(0.0, Math.log(1.0));
    141   assertTrue(1 < Math.log(3) && Math.log(3) < 1.5);
    142   assertEquals("Infinity", String(Math.log(Infinity)));
    143   assertEquals("NaN", String(Math.log(-Infinity)));
    144   assertEquals("NaN", String(Math.exp(NaN)));
    145 });
    146 
    147 test(function mathSqrt() {
    148   assertEquals(1.0, Math.sqrt(1.0));
    149   assertEquals("NaN", String(Math.sqrt(-1.0)));
    150   assertEquals("Infinity", String(Math.sqrt(Infinity)));
    151   assertEquals("NaN", String(Math.sqrt(-Infinity)));
    152   assertEquals("NaN", String(Math.sqrt(NaN)));
    153 });
    154 
    155 test(function mathPowHalf() {
    156   assertEquals(1.0, Math.pow(1.0, 0.5));
    157   assertEquals("NaN", String(Math.sqrt(-1.0)));
    158   assertEquals("Infinity", String(Math.pow(Infinity, 0.5)));
    159   assertEquals("NaN", String(Math.sqrt(-Infinity, 0.5)));
    160   assertEquals(0, Math.pow(Infinity, -0.5));
    161   assertEquals("NaN", String(Math.sqrt(-Infinity, -0.5)));
    162   assertEquals("NaN", String(Math.sqrt(NaN, 0.5)));
    163 });
    164 
    165 test(function mathAbs() {
    166   assertEquals(1.5, Math.abs(1.5));
    167   assertEquals(1.5, Math.abs(-1.5));
    168   assertEquals("Infinity", String(Math.abs(Infinity)));
    169   assertEquals("Infinity", String(Math.abs(-Infinity)));
    170   assertEquals("NaN", String(Math.abs(NaN)));
    171 });
    172 
    173 test(function mathRound() {
    174   assertEquals(2, Math.round(1.5));
    175   assertEquals(-1, Math.round(-1.5));
    176   assertEquals("Infinity", String(Math.round(Infinity)));
    177   assertEquals("-Infinity", String(Math.round(-Infinity)));
    178   assertEquals("Infinity", String(1 / Math.round(0.0)));
    179   assertEquals("-Infinity", String(1 / Math.round(-0.0)));
    180   assertEquals("NaN", String(Math.round(NaN)));
    181   assertEquals(Math.pow(2, 52) + 1, Math.round(Math.pow(2, 52) + 1));
    182 });
    183 
    184 test(function mathFround() {
    185   assertTrue(isNaN(Math.fround(NaN)));
    186   assertEquals("Infinity", String(1/Math.fround(0)));
    187   assertEquals("-Infinity", String(1/Math.fround(-0)));
    188   assertEquals("Infinity", String(Math.fround(Infinity)));
    189   assertEquals("-Infinity", String(Math.fround(-Infinity)));
    190   assertEquals("Infinity", String(Math.fround(1E200)));
    191   assertEquals("-Infinity", String(Math.fround(-1E200)));
    192   assertEquals(3.1415927410125732, Math.fround(Math.PI));
    193 });
    194 
    195 test(function mathFloor() {
    196   assertEquals(1, Math.floor(1.5));
    197   assertEquals(-2, Math.floor(-1.5));
    198   assertEquals("Infinity", String(Math.floor(Infinity)));
    199   assertEquals("-Infinity", String(Math.floor(-Infinity)));
    200   assertEquals("Infinity", String(1 / Math.floor(0.0)));
    201   assertEquals("-Infinity", String(1 / Math.floor(-0.0)));
    202   assertEquals("NaN", String(Math.floor(NaN)));
    203   assertEquals(Math.pow(2, 52) + 1, Math.floor(Math.pow(2, 52) + 1));
    204 });
    205 
    206 test(function mathPow() {
    207   assertEquals(2.25, Math.pow(1.5, 2));
    208   assertTrue(1.8 < Math.pow(1.5, 1.5) && Math.pow(1.5, 1.5) < 1.9);
    209   assertEquals("Infinity", String(Math.pow(Infinity, 0.5)));
    210   assertEquals("Infinity", String(Math.pow(-Infinity, 0.5)));
    211   assertEquals(0, Math.pow(Infinity, -0.5));
    212   assertEquals(0, Math.pow(Infinity, -0.5));
    213   assertEquals("Infinity", String(Math.pow(Infinity, Infinity)));
    214   assertEquals(0, Math.pow(Infinity, -Infinity));
    215   assertEquals("NaN", String(Math.pow(Infinity, NaN)));
    216   assertEquals("NaN", String(Math.pow(NaN, 2)));
    217 });
    218 
    219 test(function stringAdd() {
    220   assertEquals("", "" + "");
    221   assertEquals("folded constant", "folded " + "constant");
    222   assertEquals("not folded constant1", "not folded constant" + 1);
    223 });
    224 
    225 test(function stringLength() {
    226   assertEquals(6, "abcdef".length);
    227   assertEquals(0, "".length);
    228   assertEquals(-5, { length: -5 }.length);
    229 });
    230 
    231 test(function stringCharCodeAt() {
    232   assertEquals(99, "abc".charCodeAt(2));
    233   assertEquals("NaN", String("abc".charCodeAt(-1)));
    234   assertEquals("NaN", String("abc".charCodeAt(4)));
    235   assertEquals(98, "abc".charCodeAt(1.1));
    236   assertEquals("NaN", String("abc".charCodeAt(4.1)));
    237 });
    238 
    239 test(function stringCharAt() {
    240   assertEquals("c", "abc".charAt(2));
    241   assertEquals("", "abc".charAt(-1));
    242   assertEquals("", "abc".charAt(4));
    243   assertEquals("b", "abc".charAt(1.1));
    244   assertEquals("", "abc".charAt(4.1));
    245 });
    246 
    247 
    248 test(function int32Mod() {
    249   assertEquals(-0, -2147483648 % (-1));
    250 });
    251 
    252 test(function int32Div() {
    253   assertEquals(2147483648, -2147483648 / (-1));
    254 });
    255