Home | History | Annotate | Download | only in es6
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Flags: --harmony-tolength
      6 
      7 // Test array functions do not cause infinite loops when length is negative,
      8 // max_value, etc.
      9 
     10 // ArrayToString
     11 
     12 var o = { length: Number.MIN_VALUE };
     13 var result = Array.prototype.toString.call(o);
     14 assertEquals("[object Object]", result);
     15 
     16 // ArrayToLocaleString
     17 
     18 var o = { length: Number.MIN_VALUE };
     19 var result = Array.prototype.toLocaleString.call(o);
     20 assertEquals("", result);
     21 
     22 // ArrayJoin
     23 
     24 var o = { length: Number.MIN_VALUE };
     25 var result = Array.prototype.join.call(o);
     26 assertEquals(0, result.length);
     27 
     28 // ArrayPush
     29 
     30 var o = { length: Number.MIN_VALUE };
     31 Array.prototype.push.call(o, 1);
     32 assertEquals(1, o.length);
     33 assertEquals(1, o[0]);
     34 
     35 var o = { length: Number.MAX_VALUE };
     36 assertThrows(() => Array.prototype.push.call(o, 1), TypeError);
     37 
     38 // ArrayPop
     39 
     40 var o = { length: 0 };
     41 Array.prototype.pop.call(o);
     42 assertEquals(0, o.length);
     43 
     44 var o = { length: Number.MIN_VALUE };
     45 Array.prototype.pop.call(o);
     46 assertEquals(0, o.length);
     47 
     48 var o = { length: Number.MAX_VALUE };
     49 Array.prototype.pop.call(o);
     50 assertEquals(o.length, Number.MAX_SAFE_INTEGER - 1);
     51 
     52 // ArrayReverse
     53 
     54 var o = { 0: 'foo', length: Number.MIN_VALUE }
     55 var result = Array.prototype.reverse.call(o);
     56 assertEquals('object', typeof(result));
     57 assertEquals(Number.MIN_VALUE, result.length);
     58 assertEquals(Number.MIN_VALUE, o.length);
     59 
     60 // ArrayShift
     61 
     62 var o = { 0: "foo", length: Number.MIN_VALUE }
     63 var result = Array.prototype.shift.call(o);
     64 assertEquals(undefined, result);
     65 assertEquals(0, o.length);
     66 
     67 // ArrayUnshift
     68 
     69 var o = { length: 0 };
     70 Array.prototype.unshift.call(o);
     71 assertEquals(0, o.length);
     72 
     73 var o = { length: 0 };
     74 Array.prototype.unshift.call(o, 'foo');
     75 assertEquals('foo', o[0]);
     76 assertEquals(1, o.length);
     77 
     78 var o = { length: Number.MIN_VALUE };
     79 Array.prototype.unshift.call(o);
     80 assertEquals(0, o.length);
     81 
     82 var o = { length: Number.MIN_VALUE };
     83 Array.prototype.unshift.call(o, 'foo');
     84 assertEquals('foo', o[0]);
     85 assertEquals(1, o.length);
     86 
     87 // ArraySplice
     88 
     89 var o = { length: Number.MIN_VALUE };
     90 Array.prototype.splice.call(o);
     91 assertEquals(0, o.length);
     92 
     93 var o = { length: Number.MIN_VALUE };
     94 Array.prototype.splice.call(o, 0, 10, ['foo']);
     95 assertEquals(['foo'], o[0]);
     96 assertEquals(1, o.length);
     97 
     98 var o = { length: Number.MIN_VALUE };
     99 Array.prototype.splice.call(o, -1);
    100 assertEquals(0, o.length);
    101 
    102 var o = { length: Number.MAX_SAFE_INTEGER };
    103 Array.prototype.splice.call(o, -1);
    104 assertEquals(Number.MAX_SAFE_INTEGER - 1, o.length);
    105 
    106 // ArraySlice
    107 
    108 var o = { length: Number.MIN_VALUE };
    109 var result = Array.prototype.slice.call(o);
    110 assertEquals(0, result.length);
    111 
    112 var o = { length: Number.MIN_VALUE };
    113 var result = Array.prototype.slice.call(o, Number.MAX_VALUE);
    114 assertEquals(0, result.length);
    115 
    116 var o = { length: Number.MAX_VALUE };
    117 var result = Array.prototype.slice.call(o, Number.MAX_VALUE - 1);
    118 assertEquals(0, result.length);
    119 
    120 // ArrayIndexOf
    121 
    122 var o = { length: Number.MIN_VALUE };
    123 var result = Array.prototype.indexOf.call(o);
    124 assertEquals(-1, result);
    125 
    126 var o = { length: Number.MAX_SAFE_INTEGER }
    127 o[Number.MAX_SAFE_INTEGER - 1] = "foo"
    128 var result = Array.prototype.indexOf.call(o,
    129     "foo", Number.MAX_SAFE_INTEGER - 2);
    130 assertEquals(Number.MAX_SAFE_INTEGER - 1, result);
    131 
    132 var o = { length: Number.MAX_SAFE_INTEGER };
    133 o[Number.MAX_SAFE_INTEGER - 1] = "foo";
    134 var result = Array.prototype.indexOf.call(o, "foo", -1);
    135 assertEquals(Number.MAX_SAFE_INTEGER - 1, result);
    136 
    137 // ArrayLastIndexOf
    138 
    139 var o = { length: Number.MIN_VALUE };
    140 var result = Array.prototype.lastIndexOf.call(o);
    141 assertEquals(-1, result);
    142 
    143 var o = { length: Number.MAX_SAFE_INTEGER }
    144 o[Number.MAX_SAFE_INTEGER - 1] = "foo"
    145 var result = Array.prototype.lastIndexOf.call(o,
    146     "foo", Number.MAX_SAFE_INTEGER);
    147 assertEquals(Number.MAX_SAFE_INTEGER - 1, result);
    148 
    149 var o = { length: Number.MAX_SAFE_INTEGER };
    150 o[Number.MAX_SAFE_INTEGER - 1] = "foo";
    151 var result = Array.prototype.lastIndexOf.call(o, "foo", -1);
    152 assertEquals(Number.MAX_SAFE_INTEGER - 1, result);
    153 
    154 // ArrayFilter
    155 
    156 var func = function(v) { return v; }
    157 
    158 var o = { length: Number.MIN_VALUE };
    159 Array.prototype.filter.call(o, func);
    160 assertEquals(Number.MIN_VALUE, o.length);
    161 
    162 // ArrayForEach
    163 
    164 var o = { length: Number.MIN_VALUE };
    165 Array.prototype.forEach.call(o, func);
    166 assertEquals(Number.MIN_VALUE, o.length);
    167 
    168 // ArraySome
    169 
    170 var o = { length: Number.MIN_VALUE };
    171 Array.prototype.some.call(o, func);
    172 assertEquals(Number.MIN_VALUE, o.length);
    173 
    174 // ArrayEvery
    175 
    176 var o = { length: Number.MIN_VALUE };
    177 Array.prototype.every.call(o, func);
    178 assertEquals(Number.MIN_VALUE, o.length);
    179 
    180 // ArrayMap
    181 
    182 var o = { length: Number.MIN_VALUE };
    183 Array.prototype.map.call(o, func);
    184 assertEquals(Number.MIN_VALUE, o.length);
    185 
    186 // ArrayReduce
    187 
    188 var o = { length: Number.MIN_VALUE };
    189 Array.prototype.reduce.call(o, func, 0);
    190 assertEquals(Number.MIN_VALUE, o.length);
    191 
    192 // ArrayReduceRight
    193 
    194 var o = { length: Number.MIN_VALUE };
    195 Array.prototype.reduceRight.call(o, func, 0);
    196 assertEquals(Number.MIN_VALUE, o.length);
    197 
    198 // ArrayFill
    199 
    200 var o = { length: Number.MIN_VALUE };
    201 Array.prototype.fill(o, 0);
    202 assertEquals(Number.MIN_VALUE, o.length);
    203