Home | History | Annotate | Download | only in regress
      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 // Flags: --allow-natives-syntax
     29 
     30 // general tests
     31 var e31 = Math.pow(2, 31);
     32 
     33 assertEquals(-e31, -1*e31);
     34 assertEquals(e31, -1*e31*(-1));
     35 assertEquals(e31, -1*-e31);
     36 assertEquals(e31, -e31*(-1));
     37 
     38 var x = {toString : function() {return 1}}
     39 function add(a,b){return a+b;}
     40 add(1,x);
     41 add(1,x);
     42 %OptimizeFunctionOnNextCall(add);
     43 add(1,x);
     44 x.toString = function() {return "2"};
     45 
     46 assertEquals(add(1,x), "12");
     47 
     48 // Test the correct placement of the simulates in TruncateToNumber:
     49 function Checker() {
     50   this.str = "1";
     51   var toStringCalled = 0;
     52   var toStringExpected = 0;
     53   this.toString = function() {
     54     toStringCalled++;
     55     return this.str;
     56   };
     57   this.check = function() {
     58     toStringExpected++;
     59     assertEquals(toStringExpected, toStringCalled);
     60   };
     61 };
     62 var left = new Checker();
     63 var right = new Checker();
     64 
     65 function test(fun,check_fun,a,b,does_throw) {
     66   left.str = a;
     67   right.str = b;
     68   try {
     69     assertEquals(check_fun(a,b), fun(left, right));
     70     assertTrue(!does_throw);
     71   } catch(e) {
     72     if (e instanceof TypeError) {
     73       assertTrue(!!does_throw);
     74     } else {
     75       throw e;
     76     }
     77   } finally {
     78     left.check();
     79     if (!does_throw || does_throw>1) {
     80       right.check();
     81     }
     82   }
     83 }
     84 
     85 function minus(a,b) { return a-b };
     86 function check_minus(a,b) { return a-b };
     87 function mod(a,b) { return a%b };
     88 function check_mod(a,b) { return a%b };
     89 
     90 test(minus,check_minus,1,2);
     91 // Bailout on left
     92 test(minus,check_minus,1<<30,1);
     93 // Bailout on right
     94 test(minus,check_minus,1,1<<30);
     95 // Bailout on result
     96 test(minus,check_minus,1<<30,-(1<<30));
     97 
     98 // Some more interesting things
     99 test(minus,check_minus,1,1.4);
    100 test(minus,check_minus,1.3,4);
    101 test(minus,check_minus,1.3,1.4);
    102 test(minus,check_minus,1,2);
    103 test(minus,check_minus,1,undefined);
    104 test(minus,check_minus,1,2);
    105 test(minus,check_minus,1,true);
    106 test(minus,check_minus,1,2);
    107 test(minus,check_minus,1,null);
    108 test(minus,check_minus,1,2);
    109 test(minus,check_minus,1,"");
    110 test(minus,check_minus,1,2);
    111 
    112 // Throw on left
    113 test(minus,check_minus,{},1,1);
    114 // Throw on right
    115 test(minus,check_minus,1,{},2);
    116 // Throw both
    117 test(minus,check_minus,{},{},1);
    118 
    119 test(minus,check_minus,1,2);
    120 
    121 // Now with optimized code
    122 test(mod,check_mod,1,2);
    123 %OptimizeFunctionOnNextCall(mod);
    124 test(mod,check_mod,1,2);
    125 
    126 test(mod,check_mod,1<<30,1);
    127 %OptimizeFunctionOnNextCall(mod);
    128 test(mod,check_mod,1<<30,1);
    129 test(mod,check_mod,1,1<<30);
    130 %OptimizeFunctionOnNextCall(mod);
    131 test(mod,check_mod,1,1<<30);
    132 test(mod,check_mod,1<<30,-(1<<30));
    133 %OptimizeFunctionOnNextCall(mod);
    134 test(mod,check_mod,1<<30,-(1<<30));
    135 
    136 test(mod,check_mod,1,{},2);
    137 %OptimizeFunctionOnNextCall(mod);
    138 test(mod,check_mod,1,{},2);
    139 
    140 test(mod,check_mod,1,2);
    141 
    142 
    143 // test oddballs
    144 function t1(a, b) {return a-b}
    145 assertEquals(t1(1,2), 1-2);
    146 assertEquals(t1(2,true), 2-1);
    147 assertEquals(t1(false,2), 0-2);
    148 assertEquals(t1(1,2.4), 1-2.4);
    149 assertEquals(t1(1.3,2.4), 1.3-2.4);
    150 assertEquals(t1(true,2.4), 1-2.4);
    151 assertEquals(t1(1,undefined), 1-NaN);
    152 assertEquals(t1(1,1<<30), 1-(1<<30));
    153 assertEquals(t1(1,2), 1-2);
    154 
    155 function t2(a, b) {return a/b}
    156 assertEquals(t2(1,2), 1/2);
    157 assertEquals(t2(null,2), 0/2);
    158 assertEquals(t2(null,-2), 0/-2);
    159 assertEquals(t2(2,null), 2/0);
    160 assertEquals(t2(-2,null), -2/0);
    161 assertEquals(t2(1,2.4), 1/2.4);
    162 assertEquals(t2(1.3,2.4), 1.3/2.4);
    163 assertEquals(t2(null,2.4), 0/2.4);
    164 assertEquals(t2(1.3,null), 1.3/0);
    165 assertEquals(t2(undefined,2), NaN/2);
    166 assertEquals(t2(1,1<<30), 1/(1<<30));
    167 assertEquals(t2(1,2), 1/2);
    168 
    169 
    170 // Assert that the hole is not truncated to nan for string add.
    171 function string_add(a,i) {
    172   var d = [0.1, ,0.3];
    173   return a + d[i];
    174 }
    175 
    176 string_add(1.1, 0);
    177 string_add("", 0);
    178 %OptimizeFunctionOnNextCall(string_add);
    179 string_add(1.1, 0);
    180 // There comes the hole
    181 assertEquals("undefined", string_add("", 1));
    182