mirror of https://github.com/tc39/test262.git
Add tests for TypedArrays indexOf
This commit is contained in:
parent
cf68c3be91
commit
d46b0b1860
|
@ -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);
|
||||
});
|
|
@ -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)");
|
||||
});
|
|
@ -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]");
|
||||
});
|
31
test/built-ins/TypedArray/prototype/indexOf/get-length-uses-internal-arraylength.js
vendored
Normal file
31
test/built-ins/TypedArray/prototype/indexOf/get-length-uses-internal-arraylength.js
vendored
Normal file
|
@ -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);
|
||||
});
|
36
test/built-ins/TypedArray/prototype/indexOf/length-zero-returns-minus-one.js
vendored
Normal file
36
test/built-ins/TypedArray/prototype/indexOf/length-zero-returns-minus-one.js
vendored
Normal file
|
@ -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)"
|
||||
);
|
||||
});
|
32
test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex-symbol.js
vendored
Normal file
32
test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex-symbol.js
vendored
Normal file
|
@ -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);
|
||||
})
|
||||
});
|
35
test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex.js
vendored
Normal file
35
test/built-ins/TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex.js
vendored
Normal file
|
@ -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);
|
||||
})
|
||||
});
|
|
@ -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)");
|
||||
});
|
37
test/built-ins/TypedArray/prototype/indexOf/search-not-found-returns-minus-one.js
vendored
Normal file
37
test/built-ins/TypedArray/prototype/indexOf/search-not-found-returns-minus-one.js
vendored
Normal file
|
@ -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)");
|
||||
});
|
|
@ -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");
|
||||
});
|
|
@ -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]");
|
||||
});
|
Loading…
Reference in New Issue