Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2011 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 var test_id = 0;
     31 function testRound(expect, input) {
     32   // Make source code different on each invocation to make
     33   // sure it gets optimized each time.
     34   var doRound = new Function('input',
     35                              '"' + (test_id++) + '";return Math.round(input)');
     36   assertEquals(expect, doRound(input));
     37   assertEquals(expect, doRound(input));
     38   assertEquals(expect, doRound(input));
     39   %OptimizeFunctionOnNextCall(doRound);
     40   assertEquals(expect, doRound(input));
     41 }
     42 
     43 testRound(0, 0);
     44 testRound(-0, -0);
     45 testRound(Infinity, Infinity);
     46 testRound(-Infinity, -Infinity);
     47 testRound(NaN, NaN);
     48 
     49 // Regression test for a bug where a negative zero coming from Math.round
     50 // was not properly handled by other operations.
     51 function roundsum(i, n) {
     52   var ret = Math.round(n);
     53   while (--i > 0) {
     54     ret += Math.round(n);
     55   }
     56   return ret;
     57 }
     58 assertEquals(-0, roundsum(1, -0));
     59 %OptimizeFunctionOnNextCall(roundsum);
     60 // The optimized function will deopt.  Run it with enough iterations to try
     61 // to optimize via OSR (triggering the bug).
     62 assertEquals(-0, roundsum(100000, -0));
     63 
     64 testRound(1, 0.5);
     65 testRound(1, 0.7);
     66 testRound(1, 1);
     67 testRound(1, 1.1);
     68 testRound(1, 1.49999);
     69 testRound(-0, -0.5);
     70 testRound(-1, -0.5000000000000001);
     71 testRound(-1, -0.7);
     72 testRound(-1, -1);
     73 testRound(-1, -1.1);
     74 testRound(-1, -1.49999);
     75 testRound(-1, -1.5);
     76 
     77 testRound(9007199254740990, 9007199254740990);
     78 testRound(9007199254740991, 9007199254740991);
     79 testRound(-9007199254740990, -9007199254740990);
     80 testRound(-9007199254740991, -9007199254740991);
     81 testRound(Number.MAX_VALUE, Number.MAX_VALUE);
     82 testRound(-Number.MAX_VALUE, -Number.MAX_VALUE);
     83 
     84 testRound(536870911, 536870910.5);
     85 testRound(536870911, 536870911);
     86 testRound(536870911, 536870911.4);
     87 testRound(536870912, 536870911.5);
     88 testRound(536870912, 536870912);
     89 testRound(536870912, 536870912.4);
     90 testRound(536870913, 536870912.5);
     91 testRound(536870913, 536870913);
     92 testRound(536870913, 536870913.4);
     93 testRound(1073741823, 1073741822.5);
     94 testRound(1073741823, 1073741823);
     95 testRound(1073741823, 1073741823.4);
     96 testRound(1073741824, 1073741823.5);
     97 testRound(1073741824, 1073741824);
     98 testRound(1073741824, 1073741824.4);
     99 testRound(1073741825, 1073741824.5);
    100 testRound(2147483647, 2147483646.5);
    101 testRound(2147483647, 2147483647);
    102 testRound(2147483647, 2147483647.4);
    103 testRound(2147483648, 2147483647.5);
    104 testRound(2147483648, 2147483648);
    105 testRound(2147483648, 2147483648.4);
    106 testRound(2147483649, 2147483648.5);
    107 
    108 // Tests based on WebKit LayoutTests
    109 
    110 testRound(0, 0.4);
    111 testRound(-0, -0.4);
    112 testRound(-0, -0.5);
    113 testRound(1, 0.6);
    114 testRound(-1, -0.6);
    115 testRound(2, 1.5);
    116 testRound(2, 1.6);
    117 testRound(-2, -1.6);
    118 testRound(8640000000000000, 8640000000000000);
    119 testRound(8640000000000001, 8640000000000001);
    120 testRound(8640000000000002, 8640000000000002);
    121 testRound(9007199254740990, 9007199254740990);
    122 testRound(9007199254740991, 9007199254740991);
    123 testRound(1.7976931348623157e+308, 1.7976931348623157e+308);
    124 testRound(-8640000000000000, -8640000000000000);
    125 testRound(-8640000000000001, -8640000000000001);
    126 testRound(-8640000000000002, -8640000000000002);
    127 testRound(-9007199254740990, -9007199254740990);
    128 testRound(-9007199254740991, -9007199254740991);
    129 testRound(-1.7976931348623157e+308, -1.7976931348623157e+308);
    130 testRound(Infinity, Infinity);
    131 testRound(-Infinity, -Infinity);
    132 
    133   // Some special double number cases.
    134 var ulp = Math.pow(2, -1022 - 52);
    135 var max_denormal = (Math.pow(2, 52) - 1) * ulp;
    136 var min_normal = Math.pow(2, -1022);
    137 var max_fraction = Math.pow(2, 52) - 0.5;
    138 var min_nonfraction = Math.pow(2, 52);
    139 var max_non_infinite = Number.MAX_VALUE;
    140 
    141 var max_smi31 = Math.pow(2,30) - 1;
    142 var min_smi31 = -Math.pow(2,30);
    143 var max_smi32 = Math.pow(2,31) - 1;
    144 var min_smi32 = -Math.pow(2,31);
    145 
    146 testRound(0, ulp);
    147 testRound(0, max_denormal);
    148 testRound(0, min_normal);
    149 testRound(0, 0.49999999999999994);
    150 testRound(1, 0.5);
    151 testRound(Math.pow(2,52), max_fraction);
    152 testRound(min_nonfraction, min_nonfraction);
    153 testRound(max_non_infinite, max_non_infinite);
    154 
    155 testRound(max_smi31, max_smi31 - 0.5);
    156 testRound(max_smi31 + 1, max_smi31 + 0.5);
    157 testRound(max_smi32, max_smi32 - 0.5);
    158 testRound(max_smi32 + 1, max_smi32 + 0.5);
    159 
    160 testRound(-0, -ulp);
    161 testRound(-0, -max_denormal);
    162 testRound(-0, -min_normal);
    163 testRound(-0, -0.49999999999999994);
    164 testRound(-0, -0.5);
    165 testRound(-Math.pow(2,52)+1, -max_fraction);
    166 testRound(-min_nonfraction, -min_nonfraction);
    167 testRound(-max_non_infinite, -max_non_infinite);
    168 
    169 testRound(min_smi31, min_smi31 - 0.5);
    170 testRound(min_smi31 + 1, min_smi31 + 0.5);
    171 testRound(min_smi32, min_smi32 - 0.5);
    172 testRound(min_smi32 + 1, min_smi32 + 0.5);
    173 
    174 
    175