Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2008 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 function toInt32(x) {
     29   return x | 0;
     30 }
     31 
     32 assertEquals(0, toInt32(Infinity), "Inf");
     33 assertEquals(0, toInt32(-Infinity), "-Inf");
     34 assertEquals(0, toInt32(NaN), "NaN");
     35 assertEquals(0, toInt32(0.0), "zero");
     36 assertEquals(0, toInt32(-0.0), "-zero");
     37 
     38 assertEquals(0, toInt32(Number.MIN_VALUE));
     39 assertEquals(0, toInt32(-Number.MIN_VALUE));
     40 assertEquals(0, toInt32(0.1));
     41 assertEquals(0, toInt32(-0.1));
     42 assertEquals(1, toInt32(1), "one");
     43 assertEquals(1, toInt32(1.1), "onepointone");
     44 assertEquals(-1, toInt32(-1), "-one");
     45 assertEquals(0, toInt32(0.6), "truncate positive (0.6)");
     46 assertEquals(1, toInt32(1.6), "truncate positive (1.6)");
     47 assertEquals(0, toInt32(-0.6), "truncate negative (-0.6)");
     48 assertEquals(-1, toInt32(-1.6), "truncate negative (-1.6)");
     49 
     50 assertEquals(2147483647, toInt32(2147483647));
     51 assertEquals(-2147483648, toInt32(2147483648));
     52 assertEquals(-2147483647, toInt32(2147483649));
     53 
     54 assertEquals(-1, toInt32(4294967295));
     55 assertEquals(0, toInt32(4294967296));
     56 assertEquals(1, toInt32(4294967297));
     57 
     58 assertEquals(-2147483647, toInt32(-2147483647));
     59 assertEquals(-2147483648, toInt32(-2147483648));
     60 assertEquals(2147483647, toInt32(-2147483649));
     61 
     62 assertEquals(1, toInt32(-4294967295));
     63 assertEquals(0, toInt32(-4294967296));
     64 assertEquals(-1, toInt32(-4294967297));
     65 
     66 assertEquals(-2147483648, toInt32(2147483648.25));
     67 assertEquals(-2147483648, toInt32(2147483648.5));
     68 assertEquals(-2147483648, toInt32(2147483648.75));
     69 assertEquals(-1, toInt32(4294967295.25));
     70 assertEquals(-1, toInt32(4294967295.5));
     71 assertEquals(-1, toInt32(4294967295.75));
     72 assertEquals(-1294967296, toInt32(3000000000.25));
     73 assertEquals(-1294967296, toInt32(3000000000.5));
     74 assertEquals(-1294967296, toInt32(3000000000.75));
     75 
     76 assertEquals(-2147483648, toInt32(-2147483648.25));
     77 assertEquals(-2147483648, toInt32(-2147483648.5));
     78 assertEquals(-2147483648, toInt32(-2147483648.75));
     79 assertEquals(1, toInt32(-4294967295.25));
     80 assertEquals(1, toInt32(-4294967295.5));
     81 assertEquals(1, toInt32(-4294967295.75));
     82 assertEquals(1294967296, toInt32(-3000000000.25));
     83 assertEquals(1294967296, toInt32(-3000000000.5));
     84 assertEquals(1294967296, toInt32(-3000000000.75));
     85 
     86 var base = Math.pow(2, 64);
     87 assertEquals(0, toInt32(base + 0));
     88 assertEquals(0, toInt32(base + 1117));
     89 assertEquals(4096, toInt32(base + 2234));
     90 assertEquals(4096, toInt32(base + 3351));
     91 assertEquals(4096, toInt32(base + 4468));
     92 assertEquals(4096, toInt32(base + 5585));
     93 assertEquals(8192, toInt32(base + 6702));
     94 assertEquals(8192, toInt32(base + 7819));
     95 assertEquals(8192, toInt32(base + 8936));
     96 assertEquals(8192, toInt32(base + 10053));
     97 assertEquals(12288, toInt32(base + 11170));
     98 assertEquals(12288, toInt32(base + 12287));
     99 assertEquals(12288, toInt32(base + 13404));
    100 assertEquals(16384, toInt32(base + 14521));
    101 assertEquals(16384, toInt32(base + 15638));
    102 assertEquals(16384, toInt32(base + 16755));
    103 assertEquals(16384, toInt32(base + 17872));
    104 assertEquals(20480, toInt32(base + 18989));
    105 assertEquals(20480, toInt32(base + 20106));
    106 assertEquals(20480, toInt32(base + 21223));
    107 assertEquals(20480, toInt32(base + 22340));
    108 assertEquals(24576, toInt32(base + 23457));
    109 assertEquals(24576, toInt32(base + 24574));
    110 assertEquals(24576, toInt32(base + 25691));
    111 assertEquals(28672, toInt32(base + 26808));
    112 assertEquals(28672, toInt32(base + 27925));
    113 assertEquals(28672, toInt32(base + 29042));
    114 assertEquals(28672, toInt32(base + 30159));
    115 assertEquals(32768, toInt32(base + 31276));
    116 
    117 // bignum is (2^53 - 1) * 2^31 - highest number with bit 31 set.
    118 var bignum = Math.pow(2, 84) - Math.pow(2, 31);
    119 assertEquals(-Math.pow(2,31), toInt32(bignum));
    120 assertEquals(-Math.pow(2,31), toInt32(-bignum));
    121 assertEquals(0, toInt32(2 * bignum));
    122 assertEquals(0, toInt32(-(2 * bignum)));
    123 assertEquals(0, toInt32(bignum - Math.pow(2,31)));
    124 assertEquals(0, toInt32(-(bignum - Math.pow(2,31))));
    125 
    126 // max_fraction is largest number below 1.
    127 var max_fraction = (1 - Math.pow(2,-53));
    128 assertEquals(0, toInt32(max_fraction));
    129 assertEquals(0, toInt32(-max_fraction));
    130