typedarray-findlastindex.js 5.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax --harmony-array-find-last

var typedArrayConstructors = [
  Uint8Array,
  Int8Array,
  Uint16Array,
  Int16Array,
  Uint32Array,
  Int32Array,
  Uint8ClampedArray,
  Float32Array,
  Float64Array];

for (var constructor of typedArrayConstructors) {

assertEquals(1, constructor.prototype.findLastIndex.length);

var a = new constructor([21, 22, 23, 24]);
assertEquals(-1, a.findLastIndex(function() { return false; }));
assertEquals(-1, a.findLastIndex(function(val) { return 121 === val; }));
assertEquals(3, a.findLastIndex(function() { return true; }));
assertEquals(1, a.findLastIndex(function(val) { return 22 === val; }), undefined);
assertEquals(2, a.findLastIndex(function(val) { return 23 === val; }), null);
assertEquals(3, a.findLastIndex(function(val) { return 24 === val; }));


//
// Test predicate is not called when array is empty
//
(function() {
  var a = new constructor([]);
  var l = -1;
  var o = -1;
  var v = -1;
  var k = -1;

  a.findLastIndex(function(val, key, obj) {
    o = obj;
    l = obj.length;
    v = val;
    k = key;

    return false;
  });

  assertEquals(-1, l);
  assertEquals(-1, o);
  assertEquals(-1, v);
  assertEquals(-1, k);
})();


//
// Test predicate is called with correct arguments
//
(function() {
  var a = new constructor([5]);
  var l = -1;
  var o = -1;
  var v = -1;
  var k = -1;

  var index = a.findLastIndex(function(val, key, obj) {
    o = obj;
    l = obj.length;
    v = val;
    k = key;

    return false;
  });

  assertArrayEquals(a, o);
  assertEquals(a.length, l);
  assertEquals(5, v);
  assertEquals(0, k);
  assertEquals(-1, index);
})();


//
// Test predicate is called array.length times
//
(function() {
  var a = new constructor([1, 2, 3, 4, 5]);
  var l = 0;

  a.findLastIndex(function() {
    l++;
    return false;
  });

  assertEquals(a.length, l);
})();


//
// Test array modifications
//
(function() {
  a = new constructor([1, 2, 3]);
  a.findLastIndex(function(val, key) { a[key] = ++val; return false; });
  assertArrayEquals([2, 3, 4], a);
  assertEquals(3, a.length);
})();


//
// Test thisArg
//
(function() {
  // Test String as a thisArg
  var index = new constructor([1, 2, 3]).findLastIndex(function(val, key) {
    return this.charAt(Number(key)) === String(val);
  }, "321");
  assertEquals(1, index);

  // Test object as a thisArg
  var thisArg = {
    elementAt: function(key) {
      return this[key];
    }
  };
  Array.prototype.push.apply(thisArg, [3, 2, 1]);

  index = new constructor([1, 2, 3]).findLastIndex(function(val, key) {
    return this.elementAt(key) === val;
  }, thisArg);
  assertEquals(1, index);

  // Create a new object in each function call when receiver is a
  // primitive value. See ECMA-262, Annex C.
  a = [];
  new constructor([1, 2]).findLastIndex(function() { a.push(this) }, "");
  assertTrue(a[0] !== a[1]);

  // Do not create a new object otherwise.
  a = [];
  new constructor([1, 2]).findLastIndex(function() { a.push(this) }, {});
  assertEquals(a[0], a[1]);

  // In strict mode primitive values should not be coerced to an object.
  a = [];
  new constructor([1, 2]).findLastIndex(function() { 'use strict'; a.push(this); }, "");
  assertEquals("", a[0]);
  assertEquals(a[0], a[1]);

})();

// Test exceptions
assertThrows('constructor.prototype.findLastIndex.call(null, function() { })',
  TypeError);
assertThrows('constructor.prototype.findLastIndex.call(undefined, function() { })',
  TypeError);
assertThrows('constructor.prototype.findLastIndex.apply(null, function() { }, [])',
  TypeError);
assertThrows('constructor.prototype.findLastIndex.apply(undefined, function() { }, [])',
  TypeError);
assertThrows('constructor.prototype.findLastIndex.apply([], function() { }, [])',
  TypeError);
assertThrows('constructor.prototype.findLastIndex.apply({}, function() { }, [])',
  TypeError);
assertThrows('constructor.prototype.findLastIndex.apply("", function() { }, [])',
  TypeError);

assertThrows('new constructor([]).findLastIndex(null)', TypeError);
assertThrows('new constructor([]).findLastIndex(undefined)', TypeError);
assertThrows('new constructor([]).findLastIndex(0)', TypeError);
assertThrows('new constructor([]).findLastIndex(true)', TypeError);
assertThrows('new constructor([]).findLastIndex(false)', TypeError);
assertThrows('new constructor([]).findLastIndex("")', TypeError);
assertThrows('new constructor([]).findLastIndex({})', TypeError);
assertThrows('new constructor([]).findLastIndex([])', TypeError);
assertThrows('new constructor([]).findLastIndex(/\d+/)', TypeError);

// Shadowing length doesn't affect findLastIndex, unlike Array.prototype.findLastIndex
a = new constructor([1, 2]);
Object.defineProperty(a, 'length', {value: 1});
var x = 0;
assertEquals(a.findLastIndex(function(elt) { x += elt; return false; }), -1);
assertEquals(x, 3);
assertEquals(Array.prototype.findLastIndex.call(a,
    function(elt) { x += elt; return false; }), -1);
assertEquals(x, 4);

// Detached Operation
var tmp = {
  [Symbol.toPrimitive]() {
    assertUnreachable("Parameter should not be processed when " +
                      "array.[[ViewedArrayBuffer]] is detached.");
    return 0;
  }
};
var array = new constructor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
%ArrayBufferDetach(array.buffer);
assertThrows(() => array.findLastIndex(tmp), TypeError);

//
// Test detaching in predicate.
//
(function() {

var array = new constructor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
var values = [];
assertEquals(array.findLastIndex((value, idx) => {
  values.push(value);
  if (value === 5) {
    %ArrayBufferDetach(array.buffer);
  }
}), -1);
assertEquals(values, [10, 9, 8, 7, 6, 5, undefined, undefined, undefined, undefined]);

var array = new constructor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
assertEquals(array.findLastIndex((value, idx) => {
  if (value !== undefined) {
    %ArrayBufferDetach(array.buffer);
  }
  return idx === 0;
}), 0);
})();
}