From d46b0b186012556a2e1e3abd81d3181c7c5d54c2 Mon Sep 17 00:00:00 2001 From: Leonardo Balter Date: Tue, 19 Apr 2016 17:24:47 -0400 Subject: Add tests for TypedArrays indexOf --- ...ex-equal-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 ++++++++++++++ .../return-abrupt-tointeger-fromindex-symbol.js | 32 +++++++++++++ .../indexOf/return-abrupt-tointeger-fromindex.js | 35 ++++++++++++++ .../indexOf/search-found-returns-index.js | 45 +++++++++++++++++ .../indexOf/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 (limited to 'test/built-ins') 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 000000000..116c8b34a --- /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 000000000..2cd2f2991 --- /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 000000000..aeaea5c6e --- /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 000000000..e3095ab34 --- /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 000000000..98c93d567 --- /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 000000000..a12260b21 --- /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 000000000..d26490153 --- /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 000000000..416af33f3 --- /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 000000000..e1f2c8d1a --- /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 000000000..76e746706 --- /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 000000000..e80ddd1d1 --- /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]"); +}); -- cgit v1.2.1