From d46b0b186012556a2e1e3abd81d3181c7c5d54c2 Mon Sep 17 00:00:00 2001 From: Leonardo Balter Date: Tue, 19 Apr 2016 17:24:47 -0400 Subject: [PATCH 1/2] Add tests for TypedArrays indexOf --- ...ual-or-greater-length-returns-minus-one.js | 29 ++++++++++ .../prototype/indexOf/fromIndex-infinity.js | 38 +++++++++++++ .../prototype/indexOf/fromIndex-minus-zero.js | 29 ++++++++++ .../get-length-uses-internal-arraylength.js | 31 ++++++++++ .../indexOf/length-zero-returns-minus-one.js | 36 ++++++++++++ ...eturn-abrupt-tointeger-fromindex-symbol.js | 32 +++++++++++ .../return-abrupt-tointeger-fromindex.js | 35 ++++++++++++ .../indexOf/search-found-returns-index.js | 45 +++++++++++++++ .../search-not-found-returns-minus-one.js | 37 ++++++++++++ .../prototype/indexOf/strict-comparison.js | 40 +++++++++++++ .../prototype/indexOf/tointeger-fromindex.js | 56 +++++++++++++++++++ 11 files changed, 408 insertions(+) create mode 100644 test/built-ins/TypedArray/prototype/indexOf/fromIndex-equal-or-greater-length-returns-minus-one.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/fromIndex-infinity.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/fromIndex-minus-zero.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/get-length-uses-internal-arraylength.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/length-zero-returns-minus-one.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex-symbol.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/search-found-returns-index.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/search-not-found-returns-minus-one.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/strict-comparison.js create mode 100644 test/built-ins/TypedArray/prototype/indexOf/tointeger-fromindex.js diff --git a/test/built-ins/TypedArray/prototype/indexOf/fromIndex-equal-or-greater-length-returns-minus-one.js b/test/built-ins/TypedArray/prototype/indexOf/fromIndex-equal-or-greater-length-returns-minus-one.js new file mode 100644 index 0000000000..116c8b34a4 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/fromIndex-equal-or-greater-length-returns-minus-one.js @@ -0,0 +1,29 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: Return -1 if fromIndex >= ArrayLength +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step + produces the value 0.) + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample; + + sample = new TA(42); + assert.sameValue(sample.indexOf(0, 42), -1); + assert.sameValue(sample.indexOf(0, 43), -1); +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/fromIndex-infinity.js b/test/built-ins/TypedArray/prototype/indexOf/fromIndex-infinity.js new file mode 100644 index 0000000000..2cd2f29913 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/fromIndex-infinity.js @@ -0,0 +1,38 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: handle Infinity values for fromIndex +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 6. If n ≥ 0, then + a. If n is -0, let k be +0; else let k be n. + 7. Else n < 0, + a. Let k be len + n. + b. If k < 0, let k be 0. + 8. Repeat, while k < len + a. Let kPresent be ? HasProperty(O, ! ToString(k)). + b. If kPresent is true, then + i. Let elementK be ? Get(O, ! ToString(k)). + ii. Let same be the result of performing Strict Equality Comparison + searchElement === elementK. + iii. If same is true, return k. + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([42, 43, 43, 41]); + + assert.sameValue(sample.indexOf(43, Infinity), -1, "indexOf(43, Infinity)"); + assert.sameValue(sample.indexOf(43, -Infinity), 1, "indexOf(43, -Infinity)"); +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/fromIndex-minus-zero.js b/test/built-ins/TypedArray/prototype/indexOf/fromIndex-minus-zero.js new file mode 100644 index 0000000000..aeaea5c6e0 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/fromIndex-minus-zero.js @@ -0,0 +1,29 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: -0 fromIndex becomes 0 +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 6. If n ≥ 0, then + a. If n is -0, let k be +0; else let k be n. + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample; + + sample = new TA([42, 43]); + assert.sameValue(sample.indexOf(42, -0), 0, "-0 [0]"); + assert.sameValue(sample.indexOf(43, -0), 1, "-0 [1]"); +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/get-length-uses-internal-arraylength.js b/test/built-ins/TypedArray/prototype/indexOf/get-length-uses-internal-arraylength.js new file mode 100644 index 0000000000..e3095ab343 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/get-length-uses-internal-arraylength.js @@ -0,0 +1,31 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: Get "length" uses internal ArrayLength +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 2. Let len be ? ToLength(? Get(O, "length")). + ... +includes: [testTypedArray.js] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", {value: 0}); + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([7]); + + Object.defineProperty(TA.prototype, "length", {value: 0}); + Object.defineProperty(sample, "length", {value: 0}); + + assert.sameValue(sample.indexOf(7), 0); +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/length-zero-returns-minus-one.js b/test/built-ins/TypedArray/prototype/indexOf/length-zero-returns-minus-one.js new file mode 100644 index 0000000000..98c93d5675 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/length-zero-returns-minus-one.js @@ -0,0 +1,36 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: Returns -1 if length is 0 +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 2. Let len be ? ToLength(? Get(O, "length")). + 3. If len is 0, return -1. + ... +includes: [testTypedArray.js] +---*/ + +var fromIndex = { + valueOf: function() { + throw new Test262Error(); + } +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + assert.sameValue(sample.indexOf(0), -1, "returns -1"); + assert.sameValue( + sample.indexOf(0, fromIndex), -1, + "length is checked before ToInteger(fromIndex)" + ); +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex-symbol.js b/test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex-symbol.js new file mode 100644 index 0000000000..a12260b218 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex-symbol.js @@ -0,0 +1,32 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: Return abrupt from ToInteger(fromIndex) - using symbol +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step + produces the value 0.) + ... +includes: [testTypedArray.js] +features: [Symbol] +---*/ + +var fromIndex = Symbol("1"); + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + assert.throws(TypeError, function() { + sample.indexOf(7, fromIndex); + }) +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex.js b/test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex.js new file mode 100644 index 0000000000..d264901533 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex.js @@ -0,0 +1,35 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: Return abrupt from ToInteger(fromIndex) +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step + produces the value 0.) + ... +includes: [testTypedArray.js] +---*/ + +var fromIndex = { + valueOf: function() { + throw new Test262Error(); + } +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + assert.throws(Test262Error, function() { + sample.indexOf(7, fromIndex); + }) +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/search-found-returns-index.js b/test/built-ins/TypedArray/prototype/indexOf/search-found-returns-index.js new file mode 100644 index 0000000000..416af33f39 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/search-found-returns-index.js @@ -0,0 +1,45 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: returns index for the first found element +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 6. If n ≥ 0, then + a. If n is -0, let k be +0; else let k be n. + 7. Else n < 0, + a. Let k be len + n. + b. If k < 0, let k be 0. + 8. Repeat, while k < len + a. Let kPresent be ? HasProperty(O, ! ToString(k)). + b. If kPresent is true, then + i. Let elementK be ? Get(O, ! ToString(k)). + ii. Let same be the result of performing Strict Equality Comparison + searchElement === elementK. + iii. If same is true, return k. + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([42, 43, 42, 41]); + assert.sameValue(sample.indexOf(42), 0, "indexOf(42)"); + assert.sameValue(sample.indexOf(43), 1, "indexOf(43)"); + assert.sameValue(sample.indexOf(43, 1), 1, "indexOf(43, 1)"); + assert.sameValue(sample.indexOf(42, 1), 2, "indexOf(42, 1)"); + assert.sameValue(sample.indexOf(42, 2), 2, "indexOf(42, 2)"); + + assert.sameValue(sample.indexOf(42, -4), 0, "indexOf(42, -4)"); + assert.sameValue(sample.indexOf(42, -3), 2, "indexOf(42, -3)"); + assert.sameValue(sample.indexOf(42, -2), 2, "indexOf(42, -2)"); + assert.sameValue(sample.indexOf(42, -5), 0, "indexOf(42, -5)"); +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/search-not-found-returns-minus-one.js b/test/built-ins/TypedArray/prototype/indexOf/search-not-found-returns-minus-one.js new file mode 100644 index 0000000000..e1f2c8d1a2 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/search-not-found-returns-minus-one.js @@ -0,0 +1,37 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: returns -1 if the element if not found +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 6. If n ≥ 0, then + a. If n is -0, let k be +0; else let k be n. + 7. Else n < 0, + a. Let k be len + n. + b. If k < 0, let k be 0. + ... + 9. Return -1. +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample; + + sample = new TA([42, 43, 42, 41]); + assert.sameValue(sample.indexOf(44), -1, "indexOf(44)"); + assert.sameValue(sample.indexOf(43, 2), -1, "indexOf(43, 2)"); + assert.sameValue(sample.indexOf(42, 3), -1, "indexOf(42, 3)"); + assert.sameValue(sample.indexOf(44, -4), -1, "indexOf(44, -4)"); + assert.sameValue(sample.indexOf(44, -5), -1, "indexOf(44, -5)"); + assert.sameValue(sample.indexOf(42, -1), -1, "indexOf(42, -1)"); +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/strict-comparison.js b/test/built-ins/TypedArray/prototype/indexOf/strict-comparison.js new file mode 100644 index 0000000000..76e7467063 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/strict-comparison.js @@ -0,0 +1,40 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: search element is compared using strict comparing (===) +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 8. Repeat, while k < len + ... + b. If kPresent is true, then + i. Let elementK be ? Get(O, ! ToString(k)). + ii. Let same be the result of performing Strict Equality Comparison + searchElement === elementK. + iii. If same is true, return k. + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([42, 0, 1, undefined, NaN]); + assert.sameValue(sample.indexOf("42"), -1, "'42'"); + assert.sameValue(sample.indexOf([42]), -1, "[42]"); + assert.sameValue(sample.indexOf(42.0), 0, "42.0"); + assert.sameValue(sample.indexOf(-0), 1, "-0"); + assert.sameValue(sample.indexOf(true), -1, "true"); + assert.sameValue(sample.indexOf(false), -1, "false"); + assert.sameValue(sample.indexOf(NaN), -1, "NaN === NaN is false"); + assert.sameValue(sample.indexOf(null), -1, "null"); + assert.sameValue(sample.indexOf(undefined), -1, "undefined"); + assert.sameValue(sample.indexOf(""), -1, "empty string"); +}); diff --git a/test/built-ins/TypedArray/prototype/indexOf/tointeger-fromindex.js b/test/built-ins/TypedArray/prototype/indexOf/tointeger-fromindex.js new file mode 100644 index 0000000000..e80ddd1d16 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/indexOf/tointeger-fromindex.js @@ -0,0 +1,56 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.indexof +description: Return -1 if fromIndex >= ArrayLength - converted values +info: > + 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.indexOf is a distinct function that implements the same + algorithm as Array.prototype.indexOf as defined in 22.1.3.12 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.12 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) + + ... + 4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step + produces the value 0.) + ... +includes: [testTypedArray.js] +---*/ + +var obj = { + valueOf: function() { + return 1; + } +}; + +testWithTypedArrayConstructors(function(TA) { + var sample; + + sample = new TA([42, 43]); + assert.sameValue(sample.indexOf(42, "1"), -1, "string [0]"); + assert.sameValue(sample.indexOf(43, "1"), 1, "string [1]"); + + assert.sameValue(sample.indexOf(42, true), -1, "true [0]"); + assert.sameValue(sample.indexOf(43, true), 1, "true [1]"); + + assert.sameValue(sample.indexOf(42, false), 0, "false [0]"); + assert.sameValue(sample.indexOf(43, false), 1, "false [1]"); + + assert.sameValue(sample.indexOf(42, NaN), 0, "NaN [0]"); + assert.sameValue(sample.indexOf(43, NaN), 1, "NaN [1]"); + + assert.sameValue(sample.indexOf(42, null), 0, "null [0]"); + assert.sameValue(sample.indexOf(43, null), 1, "null [1]"); + + assert.sameValue(sample.indexOf(42, undefined), 0, "undefined [0]"); + assert.sameValue(sample.indexOf(43, undefined), 1, "undefined [1]"); + + assert.sameValue(sample.indexOf(42, null), 0, "null [0]"); + assert.sameValue(sample.indexOf(43, null), 1, "null [1]"); + + assert.sameValue(sample.indexOf(42, obj), -1, "object [0]"); + assert.sameValue(sample.indexOf(43, obj), 1, "object [1]"); +}); From d1f02d0fb84fd2a4d5f2bde6d6ed81f1aa4d16fa Mon Sep 17 00:00:00 2001 From: Leonardo Balter Date: Tue, 19 Apr 2016 19:13:52 -0400 Subject: [PATCH 2/2] Add tests for TypedArrays lastIndexOf --- .../lastIndexOf/fromIndex-infinity.js | 31 ++++++++++ .../lastIndexOf/fromIndex-minus-zero.js | 29 ++++++++++ .../get-length-uses-internal-arraylength.js | 31 ++++++++++ .../length-zero-returns-minus-one.js | 36 ++++++++++++ ...eturn-abrupt-tointeger-fromindex-symbol.js | 32 +++++++++++ .../return-abrupt-tointeger-fromindex.js | 35 ++++++++++++ .../lastIndexOf/search-found-returns-index.js | 56 +++++++++++++++++++ .../search-not-found-returns-minus-one.js | 41 ++++++++++++++ .../lastIndexOf/strict-comparison.js | 40 +++++++++++++ .../lastIndexOf/tointeger-fromindex.js | 56 +++++++++++++++++++ 10 files changed, 387 insertions(+) create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-infinity.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-minus-zero.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/get-length-uses-internal-arraylength.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/length-zero-returns-minus-one.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex-symbol.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/search-found-returns-index.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/search-not-found-returns-minus-one.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/strict-comparison.js create mode 100644 test/built-ins/TypedArray/prototype/lastIndexOf/tointeger-fromindex.js diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-infinity.js b/test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-infinity.js new file mode 100644 index 0000000000..75927b2d5b --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-infinity.js @@ -0,0 +1,31 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: handle Infinity values for fromIndex +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 5. If n ≥ 0, then + a. If n is -0, let k be +0; else let k be min(n, len - 1). + 6. Else n < 0, + a. Let k be len + n. + 7. Repeat, while k ≥ 0 + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([42, 43, 43, 41]); + + assert.sameValue(sample.lastIndexOf(43, Infinity), 2, "lastIndexOf(43, Infinity)"); + assert.sameValue(sample.lastIndexOf(43, -Infinity), -1, "lastIndexOf(43, -Infinity)"); +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-minus-zero.js b/test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-minus-zero.js new file mode 100644 index 0000000000..a5d1b20731 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-minus-zero.js @@ -0,0 +1,29 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: -0 fromIndex becomes 0 +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 5. If n ≥ 0, then + a. If n is -0, let k be +0; else let k be min(n, len - 1). + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample; + + sample = new TA([42, 43]); + assert.sameValue(sample.lastIndexOf(42, -0), 0, "-0 [0]"); + assert.sameValue(sample.lastIndexOf(43, -0), -1, "-0 [1]"); +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/get-length-uses-internal-arraylength.js b/test/built-ins/TypedArray/prototype/lastIndexOf/get-length-uses-internal-arraylength.js new file mode 100644 index 0000000000..f76932ab15 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/get-length-uses-internal-arraylength.js @@ -0,0 +1,31 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: Get "length" uses internal ArrayLength +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 2. Let len be ? ToLength(? Get(O, "length")). + ... +includes: [testTypedArray.js] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", {value: 0}); + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([7]); + + Object.defineProperty(TA.prototype, "length", {value: 0}); + Object.defineProperty(sample, "length", {value: 0}); + + assert.sameValue(sample.lastIndexOf(7), 0); +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/length-zero-returns-minus-one.js b/test/built-ins/TypedArray/prototype/lastIndexOf/length-zero-returns-minus-one.js new file mode 100644 index 0000000000..e2467aba75 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/length-zero-returns-minus-one.js @@ -0,0 +1,36 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: Returns -1 if length is 0 +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 2. Let len be ? ToLength(? Get(O, "length")). + 3. If len is 0, return -1. + ... +includes: [testTypedArray.js] +---*/ + +var fromIndex = { + valueOf: function() { + throw new Test262Error(); + } +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + assert.sameValue(sample.lastIndexOf(0), -1, "returns -1"); + assert.sameValue( + sample.lastIndexOf(0, fromIndex), -1, + "length is checked before ToInteger(fromIndex)" + ); +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex-symbol.js b/test/built-ins/TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex-symbol.js new file mode 100644 index 0000000000..96c5979d25 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex-symbol.js @@ -0,0 +1,32 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: Return abrupt from ToInteger(fromIndex) - using symbol +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 4. If argument fromIndex was passed, let n be ? ToInteger(fromIndex); else let + n be len-1. + ... +includes: [testTypedArray.js] +features: [Symbol] +---*/ + +var fromIndex = Symbol("1"); + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + assert.throws(TypeError, function() { + sample.lastIndexOf(7, fromIndex); + }) +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex.js b/test/built-ins/TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex.js new file mode 100644 index 0000000000..85ebef5d70 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex.js @@ -0,0 +1,35 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: Return abrupt from ToInteger(fromIndex) +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 4. If argument fromIndex was passed, let n be ? ToInteger(fromIndex); else let + n be len-1. + ... +includes: [testTypedArray.js] +---*/ + +var fromIndex = { + valueOf: function() { + throw new Test262Error(); + } +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + assert.throws(Test262Error, function() { + sample.lastIndexOf(7, fromIndex); + }) +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/search-found-returns-index.js b/test/built-ins/TypedArray/prototype/lastIndexOf/search-found-returns-index.js new file mode 100644 index 0000000000..aeb6980545 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/search-found-returns-index.js @@ -0,0 +1,56 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: returns index for the first found element +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 5. If n ≥ 0, then + a. If n is -0, let k be +0; else let k be min(n, len - 1). + 6. Else n < 0, + a. Let k be len + n. + 7. Repeat, while k ≥ 0 + a. Let kPresent be ? HasProperty(O, ! ToString(k)). + b. If kPresent is true, then + i. Let elementK be ? Get(O, ! ToString(k)). + ii. Let same be the result of performing Strict Equality Comparison + searchElement === elementK. + iii. If same is true, return k. + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([42, 43, 42, 41]); + assert.sameValue(sample.lastIndexOf(42), 2, "lastIndexOf(42)"); + assert.sameValue(sample.lastIndexOf(43), 1, "lastIndexOf(43)"); + assert.sameValue(sample.lastIndexOf(41), 3, "lastIndexOf(41)"); + assert.sameValue(sample.lastIndexOf(41, 3), 3, "lastIndexOf(41, 3)"); + assert.sameValue(sample.lastIndexOf(41, 4), 3, "lastIndexOf(41, 4)"); + assert.sameValue(sample.lastIndexOf(43, 1), 1, "lastIndexOf(43, 1)"); + assert.sameValue(sample.lastIndexOf(43, 2), 1, "lastIndexOf(43, 2)"); + assert.sameValue(sample.lastIndexOf(43, 3), 1, "lastIndexOf(43, 3)"); + assert.sameValue(sample.lastIndexOf(43, 4), 1, "lastIndexOf(43, 4)"); + assert.sameValue(sample.lastIndexOf(42, 0), 0, "lastIndexOf(42, 0)"); + assert.sameValue(sample.lastIndexOf(42, 1), 0, "lastIndexOf(42, 1)"); + assert.sameValue(sample.lastIndexOf(42, 2), 2, "lastIndexOf(42, 2)"); + assert.sameValue(sample.lastIndexOf(42, 3), 2, "lastIndexOf(42, 3)"); + assert.sameValue(sample.lastIndexOf(42, 4), 2, "lastIndexOf(42, 4)"); + assert.sameValue(sample.lastIndexOf(42, -4), 0, "lastIndexOf(42, -4)"); + assert.sameValue(sample.lastIndexOf(42, -3), 0, "lastIndexOf(42, -3)"); + assert.sameValue(sample.lastIndexOf(42, -2), 2, "lastIndexOf(42, -2)"); + assert.sameValue(sample.lastIndexOf(42, -1), 2, "lastIndexOf(42, -1)"); + assert.sameValue(sample.lastIndexOf(43, -3), 1, "lastIndexOf(43, -3)"); + assert.sameValue(sample.lastIndexOf(43, -2), 1, "lastIndexOf(43, -2)"); + assert.sameValue(sample.lastIndexOf(43, -1), 1, "lastIndexOf(43, -1)"); + assert.sameValue(sample.lastIndexOf(41, -1), 3, "lastIndexOf(41, -1)"); +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/search-not-found-returns-minus-one.js b/test/built-ins/TypedArray/prototype/lastIndexOf/search-not-found-returns-minus-one.js new file mode 100644 index 0000000000..537496b13c --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/search-not-found-returns-minus-one.js @@ -0,0 +1,41 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: returns -1 if the element if not found +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 5. If n ≥ 0, then + a. If n is -0, let k be +0; else let k be min(n, len - 1). + 6. Else n < 0, + a. Let k be len + n. + 7. Repeat, while k ≥ 0 + ... + 8. Return -1. +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample; + + sample = new TA([42, 43, 42, 41]); + assert.sameValue(sample.lastIndexOf(44), -1, "lastIndexOf(44)"); + assert.sameValue(sample.lastIndexOf(44, -4), -1, "lastIndexOf(44, -4)"); + assert.sameValue(sample.lastIndexOf(44, -5), -1, "lastIndexOf(44, -5)"); + assert.sameValue(sample.lastIndexOf(42, -5), -1, "lastIndexOf(42, -5)"); + assert.sameValue(sample.lastIndexOf(43, -4), -1, "lastIndexOf(43, -4)"); + assert.sameValue(sample.lastIndexOf(43, -5), -1, "lastIndexOf(43, -5)"); + assert.sameValue(sample.lastIndexOf(41, 0), -1, "lastIndexOf(41, 0)"); + assert.sameValue(sample.lastIndexOf(41, 1), -1, "lastIndexOf(41, 1)"); + assert.sameValue(sample.lastIndexOf(41, 2), -1, "lastIndexOf(41, 2)"); + assert.sameValue(sample.lastIndexOf(43, 0), -1, "lastIndexOf(43, 0)"); +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/strict-comparison.js b/test/built-ins/TypedArray/prototype/lastIndexOf/strict-comparison.js new file mode 100644 index 0000000000..290fdbe0b8 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/strict-comparison.js @@ -0,0 +1,40 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: search element is compared using strict comparing (===) +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 7. Repeat, while k ≥ 0 + ... + b. If kPresent is true, then + i. Let elementK be ? Get(O, ! ToString(k)). + ii. Let same be the result of performing Strict Equality Comparison + searchElement === elementK. + iii. If same is true, return k. + ... +includes: [testTypedArray.js] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([42, undefined, NaN, 0, 1]); + assert.sameValue(sample.lastIndexOf("42"), -1, "'42'"); + assert.sameValue(sample.lastIndexOf([42]), -1, "[42]"); + assert.sameValue(sample.lastIndexOf(42.0), 0, "42.0"); + assert.sameValue(sample.lastIndexOf(-0), 3, "-0"); + assert.sameValue(sample.lastIndexOf(true), -1, "true"); + assert.sameValue(sample.lastIndexOf(false), -1, "false"); + assert.sameValue(sample.lastIndexOf(NaN), -1, "NaN === NaN is false"); + assert.sameValue(sample.lastIndexOf(null), -1, "null"); + assert.sameValue(sample.lastIndexOf(undefined), -1, "undefined"); + assert.sameValue(sample.lastIndexOf(""), -1, "empty string"); +}); diff --git a/test/built-ins/TypedArray/prototype/lastIndexOf/tointeger-fromindex.js b/test/built-ins/TypedArray/prototype/lastIndexOf/tointeger-fromindex.js new file mode 100644 index 0000000000..aa5a99a72c --- /dev/null +++ b/test/built-ins/TypedArray/prototype/lastIndexOf/tointeger-fromindex.js @@ -0,0 +1,56 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.lastindexof +description: Return -1 if fromIndex >= ArrayLength - converted values +info: > + 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + %TypedArray%.prototype.lastIndexOf is a distinct function that implements the + same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.15 except + that the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". + + 22.1.3.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) + + ... + 4. If argument fromIndex was passed, let n be ? ToInteger(fromIndex); else let + n be len-1. + ... +includes: [testTypedArray.js] +---*/ + +var obj = { + valueOf: function() { + return 1; + } +}; + +testWithTypedArrayConstructors(function(TA) { + var sample; + + sample = new TA([42, 43]); + assert.sameValue(sample.lastIndexOf(42, "1"), 0, "string [0]"); + assert.sameValue(sample.lastIndexOf(43, "1"), 1, "string [1]"); + + assert.sameValue(sample.lastIndexOf(42, true), 0, "true [0]"); + assert.sameValue(sample.lastIndexOf(43, true), 1, "true [1]"); + + assert.sameValue(sample.lastIndexOf(42, false), 0, "false [0]"); + assert.sameValue(sample.lastIndexOf(43, false), -1, "false [1]"); + + assert.sameValue(sample.lastIndexOf(42, NaN), 0, "NaN [0]"); + assert.sameValue(sample.lastIndexOf(43, NaN), -1, "NaN [1]"); + + assert.sameValue(sample.lastIndexOf(42, null), 0, "null [0]"); + assert.sameValue(sample.lastIndexOf(43, null), -1, "null [1]"); + + assert.sameValue(sample.lastIndexOf(42, undefined), 0, "undefined [0]"); + assert.sameValue(sample.lastIndexOf(43, undefined), -1, "undefined [1]"); + + assert.sameValue(sample.lastIndexOf(42, null), 0, "null [0]"); + assert.sameValue(sample.lastIndexOf(43, null), -1, "null [1]"); + + assert.sameValue(sample.lastIndexOf(42, obj), 0, "object [0]"); + assert.sameValue(sample.lastIndexOf(43, obj), 1, "object [1]"); +});