Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2012 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 // Test number predicates that Harmony adds to the Number constructor:
     29 // isFinite(), isNaN(), isInteger(), isSafeInteger().
     30 
     31 assertTrue(Number.isFinite(0));
     32 assertTrue(Number.isFinite(Number.MIN_VALUE));
     33 assertTrue(Number.isFinite(Number.MAX_VALUE));
     34 assertTrue(Number.isFinite(Number.MIN_SAFE_INTEGER));
     35 assertTrue(Number.isFinite(Number.MIN_SAFE_INTEGER - 13));
     36 assertTrue(Number.isFinite(Number.MAX_SAFE_INTEGER));
     37 assertTrue(Number.isFinite(Number.MAX_SAFE_INTEGER + 23));
     38 assertFalse(Number.isFinite(Number.NaN));
     39 assertFalse(Number.isFinite(Number.POSITIVE_INFINITY));
     40 assertFalse(Number.isFinite(Number.NEGATIVE_INFINITY));
     41 assertFalse(Number.isFinite(new Number(0)));
     42 assertFalse(Number.isFinite(1/0));
     43 assertFalse(Number.isFinite(-1/0));
     44 assertFalse(Number.isFinite({}));
     45 assertFalse(Number.isFinite([]));
     46 assertFalse(Number.isFinite("s"));
     47 assertFalse(Number.isFinite(null));
     48 assertFalse(Number.isFinite(undefined));
     49 
     50 assertFalse(Number.isNaN(0));
     51 assertFalse(Number.isNaN(Number.MIN_VALUE));
     52 assertFalse(Number.isNaN(Number.MAX_VALUE));
     53 assertFalse(Number.isNaN(Number.MIN_SAFE_INTEGER - 13));
     54 assertFalse(Number.isNaN(Number.MAX_SAFE_INTEGER + 23));
     55 assertTrue(Number.isNaN(Number.NaN));
     56 assertFalse(Number.isNaN(Number.POSITIVE_INFINITY));
     57 assertFalse(Number.isNaN(Number.NEGATIVE_INFINITY));
     58 assertFalse(Number.isNaN(Number.EPSILON));
     59 assertFalse(Number.isNaN(new Number(0)));
     60 assertFalse(Number.isNaN(1/0));
     61 assertFalse(Number.isNaN(-1/0));
     62 assertFalse(Number.isNaN({}));
     63 assertFalse(Number.isNaN([]));
     64 assertFalse(Number.isNaN("s"));
     65 assertFalse(Number.isNaN(null));
     66 assertFalse(Number.isNaN(undefined));
     67 
     68 assertFalse(Number.isInteger({}));
     69 assertFalse(Number.isInteger([]));
     70 assertFalse(Number.isInteger("s"));
     71 assertFalse(Number.isInteger(null));
     72 assertFalse(Number.isInteger(undefined));
     73 assertFalse(Number.isInteger(new Number(2)));
     74 assertTrue(Number.isInteger(0));
     75 assertFalse(Number.isInteger(Number.MIN_VALUE));
     76 assertTrue(Number.isInteger(Number.MAX_VALUE));
     77 assertTrue(Number.isInteger(Number.MIN_SAFE_INTEGER));
     78 assertTrue(Number.isInteger(Number.MIN_SAFE_INTEGER - 13));
     79 assertTrue(Number.isInteger(Number.MAX_SAFE_INTEGER));
     80 assertTrue(Number.isInteger(Number.MAX_SAFE_INTEGER + 23));
     81 assertFalse(Number.isInteger(Number.NaN));
     82 assertFalse(Number.isInteger(Number.POSITIVE_INFINITY));
     83 assertFalse(Number.isInteger(Number.NEGATIVE_INFINITY));
     84 assertFalse(Number.isInteger(1/0));
     85 assertFalse(Number.isInteger(-1/0));
     86 assertFalse(Number.isInteger(Number.EPSILON));
     87 
     88 assertFalse(Number.isSafeInteger({}));
     89 assertFalse(Number.isSafeInteger([]));
     90 assertFalse(Number.isSafeInteger("s"));
     91 assertFalse(Number.isSafeInteger(null));
     92 assertFalse(Number.isSafeInteger(undefined));
     93 assertFalse(Number.isSafeInteger(new Number(2)));
     94 assertTrue(Number.isSafeInteger(0));
     95 assertTrue(Number.isSafeInteger(Number.MIN_SAFE_INTEGER));
     96 assertFalse(Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 13));
     97 assertTrue(Number.isSafeInteger(Number.MIN_SAFE_INTEGER + 13));
     98 assertTrue(Number.isSafeInteger(Number.MAX_SAFE_INTEGER));
     99 assertFalse(Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 23));
    100 assertTrue(Number.isSafeInteger(Number.MAX_SAFE_INTEGER - 23));
    101 assertFalse(Number.isSafeInteger(Number.MIN_VALUE));
    102 assertFalse(Number.isSafeInteger(Number.MAX_VALUE));
    103 assertFalse(Number.isSafeInteger(Number.NaN));
    104 assertFalse(Number.isSafeInteger(Number.POSITIVE_INFINITY));
    105 assertFalse(Number.isSafeInteger(Number.NEGATIVE_INFINITY));
    106 assertFalse(Number.isSafeInteger(1/0));
    107 assertFalse(Number.isSafeInteger(-1/0));
    108 assertFalse(Number.isSafeInteger(Number.EPSILON));
    109 
    110 var near_upper = Math.pow(2, 52);
    111 assertTrue(Number.isSafeInteger(near_upper));
    112 assertFalse(Number.isSafeInteger(2 * near_upper));
    113 assertTrue(Number.isSafeInteger(2 * near_upper - 1));
    114 assertTrue(Number.isSafeInteger(2 * near_upper - 2));
    115 assertFalse(Number.isSafeInteger(2 * near_upper + 1));
    116 assertFalse(Number.isSafeInteger(2 * near_upper + 2));
    117 assertFalse(Number.isSafeInteger(2 * near_upper + 7));
    118 
    119 var near_lower = -near_upper;
    120 assertTrue(Number.isSafeInteger(near_lower));
    121 assertFalse(Number.isSafeInteger(2 * near_lower));
    122 assertTrue(Number.isSafeInteger(2 * near_lower + 1));
    123 assertTrue(Number.isSafeInteger(2 * near_lower + 2));
    124 assertFalse(Number.isSafeInteger(2 * near_lower - 1));
    125 assertFalse(Number.isSafeInteger(2 * near_lower - 2));
    126 assertFalse(Number.isSafeInteger(2 * near_lower - 7));
    127