Import tests from Google V8 (Array.prototype.{fill, find, findIndex}, Array.{from, of})

These tests are derived from the following files within the Google V8 project:

    	test/mjsunit/harmony/array-fill.js
    	test/mjsunit/harmony/array-find.js
    	test/mjsunit/harmony/array-findindex.js
    	test/mjsunit/harmony/array-from.js
    	test/mjsunit/harmony/array-of.js
This commit is contained in:
Rick Waldron 2015-03-04 10:30:59 -05:00
parent 47674610ae
commit d797bb979e
43 changed files with 1254 additions and 76 deletions

View File

@ -1,19 +1,15 @@
//-----------------------------------------------------------------------------
function compareArray(aExpected, aActual) {
if (aActual.length != aExpected.length) {
return false;
}
function compareArray(a, b) {
if (b.length !== a.length) {
return false;
}
aExpected.sort();
aActual.sort();
var s;
for (var i = 0; i < aExpected.length; i++) {
if (aActual[i] !== aExpected[i]) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (b[i] !== a[i]) {
return false;
}
return true;
}
return true;
}

View File

@ -0,0 +1,12 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.1
description: >
The Array.from() method creates a new Array instance
from an array-like or iterable object.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
---*/
assert.sameValue(Array.from.length, 1);

View File

@ -0,0 +1,19 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.1
description: >
The Array.from() method creates a new Array instance
from an array-like or iterable object.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
---*/
var myCollectionCalled = false;
function MyCollection(length) {
myCollectionCalled = true;
assert.sameValue(1, arguments.length);
assert.sameValue(5, length);
}
Array.from.call(MyCollection, {length: 5});
assert(myCollectionCalled);

View File

@ -0,0 +1,20 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
function Empty() {}
Empty.of = Array.of;
Object.defineProperty(Empty.prototype, "length", {get: function() { return 0; }});
var nothing = new Empty;
nothing.length = 2; // no exception; this is not a strict mode assignment
assert.throws(TypeError, function() { Empty.of(); });

View File

@ -0,0 +1,23 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
var a = Array.of();
var elem = [];
assert.sameValue(a.length, 0);
a = Array.of(undefined, null, 3.14, elem);
assert(compareArray(a, [undefined, null, 3.14, elem]));
a = [];
for (var i = 0; i < 1000; i++) {
a[i] = i;
}
assert(compareArray(Array.of.apply(null, a), a));

View File

@ -0,0 +1,36 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
var hits = 0;
var lastObj = null, lastVal = undefined;
function setter(v) {
hits++;
lastObj = this;
lastVal = v;
}
// when the setter is on the new object
function Pack() {
Object.defineProperty(this, "length", {set: setter});
}
Pack.of = Array.of;
var pack = Pack.of("wolves", "cards", "cigarettes", "lies");
assert(compareArray(lastObj, pack));
assert.sameValue(lastVal, 4);
// when the setter is on the new object's prototype
function Bevy() {}
Object.defineProperty(Bevy.prototype, "length", {set: setter});
Bevy.of = Array.of;
var bevy = Bevy.of("quail");
assert(compareArray(lastObj, bevy));
assert.sameValue(lastVal, 1);

View File

@ -0,0 +1,37 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of can be transplanted to other classes.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
var hits = 0;
function Bag() {
hits++;
}
Bag.of = Array.of;
hits = 0;
var actual = Bag.of("zero", "one");
assert.sameValue(hits, 1);
hits = 0;
var expected = new Bag;
expected[0] = "zero";
expected[1] = "one";
expected.length = 2;
assert(compareArray(actual, expected));
hits = 0;
actual = Array.of.call(Bag, "zero", "one");
assert.sameValue(hits, 1);
assert(compareArray(actual, expected));

View File

@ -0,0 +1,45 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
// Array.of Check superficial features.
var desc = Object.getOwnPropertyDescriptor(Array, "of");
assert.sameValue(desc.configurable, true);
assert.sameValue(desc.enumerable, false);
assert.sameValue(desc.writable, true);
assert.sameValue(Array.of.length, 0);
assert.throws(TypeError, function() { new Array.of() }); // not a constructor
// When the this-value passed in is not a constructor, the result is an array.
[
undefined,
null,
false,
"cow",
NaN,
67,
Infinity,
-Infinity,
Math.cos, // builtin functions with no [[Construct]] slot
Math.cos.bind(Math) // bound builtin functions with no [[Construct]] slot
].forEach(function(val) {
assert.sameValue(Array.isArray(Array.of.call(val, val)), true);
});
var boundFn = (function() {}).bind(null);
var instance = Array.of.call(boundFn, 1, 2, 3);
assert.sameValue(instance.length, 3);
assert.sameValue(instance instanceof boundFn, true);
assert.sameValue(Array.isArray(instance), false);

View File

@ -0,0 +1,20 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of does not leave holes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
assert(compareArray(Array.of(undefined), [undefined]));
assert(compareArray(Array.of(undefined, undefined), [undefined, undefined]));
assert(compareArray(Array.of.apply(null, [,,undefined]), [undefined, undefined, undefined]));
assert(compareArray(Array.of.apply(null, Array(4)), [undefined, undefined, undefined, undefined]));

View File

@ -0,0 +1,32 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of does not trigger prototype setters.
(It defines elements rather than assigning to them.)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
function Bag() {}
Bag.of = Array.of;
var status = "pass";
Object.defineProperty(Array.prototype, "0", {set: function(v) {status = "FAIL 1"}});
assert.sameValue(Array.of(1)[0], 1);
assert.sameValue(status, "pass");
Object.defineProperty(Bag.prototype, "0", {set: function(v) {status = "FAIL 2"}});
assert.sameValue(Bag.of(1)[0], 1);
assert.sameValue(status, "pass");

View File

@ -0,0 +1,28 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of makes real arrays.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
---*/
function check(a) {
assert.sameValue(Object.getPrototypeOf(a), Array.prototype);
assert.sameValue(Array.isArray(a), true);
a[9] = 9;
assert.sameValue(a.length, 10);
}
check(Array.of());
check(Array.of(0));
check(Array.of(0, 1, 2));
var f = Array.of;
check(f());

View File

@ -0,0 +1,26 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of passes the number of arguments to the constructor it calls.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
var hits = 0;
function Herd(n) {
assert.sameValue(arguments.length, 1);
assert.sameValue(n, 5);
hits++;
}
Herd.of = Array.of;
Herd.of("sheep", "cattle", "elephants", "whales", "seals");
assert.sameValue(hits, 1);

View File

@ -0,0 +1,31 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.3.6
description: >
The fill() method fills all the elements of an array from a start
index to an end index with a static value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
includes: [compareArray.js]
---*/
assert.sameValue(1, Array.prototype.fill.length);
assert(compareArray([].fill(8), []));
assert(compareArray(
[0, 0, 0, 0, 0].fill(),
[undefined, undefined, undefined, undefined, undefined]
));
assert(compareArray([0, 0, 0, 0, 0].fill(8), [8, 8, 8, 8, 8]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, 1), [0, 8, 8, 8, 8]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, 10), [0, 0, 0, 0, 0]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, -5), [8, 8, 8, 8, 8]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, 1, 4), [0, 8, 8, 8, 0]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, 1, -1), [0, 8, 8, 8, 0]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, 1, 42), [0, 8, 8, 8, 8]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, -3, 42), [0, 0, 8, 8, 8]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, -3, 4), [0, 0, 8, 8, 0]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, -2, -1), [0, 0, 0, 8, 0]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, -1, -3), [0, 0, 0, 0, 0]));
assert(compareArray([0, 0, 0, 0, 0].fill(8, undefined, 4), [8, 8, 8, 8, 0]));
assert(compareArray([ , , , , 0].fill(8, 1, 3), [, 8, 8, , 0]));

View File

@ -0,0 +1,16 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.3.6
description: >
The fill() method fills all the elements of an array from a start
index to an end index with a static value.
Cannot fill a frozen array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
---*/
assert.throws(TypeError, function() {
Object.freeze([0]).fill();
});

View File

@ -0,0 +1,19 @@
// Copyright (c) 2014 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.
/*---
es6id: 22.1.3.6
description: >
The fill() method fills all the elements of an array from a start
index to an end index with a static value.
this value cannot be null or undefined
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
---*/
assert.throws(TypeError, function() {
Array.prototype.fill.call(null);
});
assert.throws(TypeError, function() {
Array.prototype.fill.call(undefined);
});

View File

@ -0,0 +1,24 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var a = [1, 2, 3];
var found = a.find(function(val) { a.push(val); return false; });
assert(compareArray(a, [1, 2, 3, 1, 2, 3]));
assert.sameValue(a.length, 6);
assert.sameValue(found, undefined);
a = [1, 2, 3];
found = a.find(function(val, key) { a[key] = ++val; return false; });
assert(compareArray(a, [2, 3, 4]));
assert.sameValue(a.length, 3);
assert.sameValue(found, undefined);

View File

@ -0,0 +1,23 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
---*/
assert.sameValue(1, Array.prototype.find.length);
var a = [21, 22, 23, 24];
assert.sameValue(a.find(function() { return false; }), undefined);
assert.sameValue(a.find(function() { return true; }), 21);
assert.sameValue(a.find(function(val) { return 121 === val; }), undefined);
assert.sameValue(a.find(function(val) { return 24 === val; }), 24);
assert.sameValue(a.find(function(val) { return 23 === val; }), 23);
assert.sameValue(a.find(function(val) { return 22 === val; }), 22);

View File

@ -0,0 +1,22 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
---*/
var a = [1, 2, 3, 4, 5];
var l = 0;
var found = a.find(function() {
l++;
return false;
});
assert.sameValue(l, a.length);
assert.sameValue(found, undefined);

View File

@ -0,0 +1,111 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
// Test exceptions
assert.throws(TypeError, function() {
Array.prototype.find.call(null, function() { });
});
assert.throws(TypeError, function() {
Array.prototype.find.call(undefined, function() { });
});
assert.throws(TypeError, function() {
Array.prototype.find.apply(null, function() { }, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply(undefined, function() { }, []);
});
assert.throws(TypeError, function() {
[].find(null);
});
assert.throws(TypeError, function() {
[].find(undefined);
});
assert.throws(TypeError, function() {
[].find(0);
});
assert.throws(TypeError, function() {
[].find(true);
});
assert.throws(TypeError, function() {
[].find(false);
});
assert.throws(TypeError, function() {
[].find("");
});
assert.throws(TypeError, function() {
[].find({});
});
assert.throws(TypeError, function() {
[].find([]);
});
assert.throws(TypeError, function() {
[].find(/\d+/);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, null);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, undefined);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, 0);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, true);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, false);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, "");
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, {});
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, /\d+/);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, null, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, undefined, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, 0, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, true, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, false, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, "", []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, {}, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, [], []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, /\d+/, []);
});

View File

@ -0,0 +1,40 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var a = {
prop1: "val1",
prop2: "val2",
isValid: function() {
return this.prop1 === "val1" && this.prop2 === "val2";
}
};
Array.prototype.push.apply(a, [30, 31, 32]);
var found = Array.prototype.find.call(a, function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return !obj.isValid();
});
assert(compareArray(o, a));
assert.sameValue(l, 3);
assert.sameValue(v, 32);
assert.sameValue(k, 2);
assert.sameValue(found, undefined);

View File

@ -0,0 +1,33 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var a = [];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
a.find(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assert.sameValue(-1, l);
assert.sameValue(-1, o);
assert.sameValue(-1, v);
assert.sameValue(-1, k);

View File

@ -0,0 +1,33 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var a = ["b"];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var found = a.find(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assert(compareArray(o, a));
assert.sameValue(a.length, l);
assert.sameValue(v, "b");
assert.sameValue(k, 0);
assert.sameValue(found, undefined);

View File

@ -1,21 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
predicate is called only for elements of the array which actually
exist; it is not called for missing elements of the array
---*/
var a = [];
a[10] = 1;
a[11] = 2;
var b = a.find(function (v) {
return v !== 1;
});
if (b !== undefined) {
$ERROR('#1: b !== undefined. Actual: ' + b);
}

View File

@ -0,0 +1,49 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
// Test String as a thisArg
var found = [1, 2, 3].find(function(val, key) {
return this.charAt(Number(key)) === String(val);
}, "321");
assert.sameValue(2, found);
// Test object as a thisArg
var thisArg = {
elementAt: function(key) {
return this[key];
}
};
Array.prototype.push.apply(thisArg, ["c", "b", "a"]);
found = ["a", "b", "c"].find(function(val, key) {
return this.elementAt(key) === val;
}, thisArg);
assert.sameValue("b", found);
// Create a new object in each function call when receiver is a
// primitive value. See ECMA-262, Annex C.
a = [];
[1, 2].find(function() { a.push(this) }, "");
assert(a[0] !== a[1]);
// Do not create a new object otherwise.
a = [];
[1, 2].find(function() { a.push(this) }, {});
assert.sameValue(a[0], a[1]);
// In strict mode primitive values should not be coerced to an object.
a = [];
[1, 2].find(function() { "use strict"; a.push(this); }, "");
assert.sameValue("", a[0]);
assert.sameValue(a[0], a[1]);

View File

@ -0,0 +1,18 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
Does not skip holes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
---*/
var count = 0;
[,,,,,].find(function() { count++; return false; });
assert.sameValue(count, 5);

View File

@ -0,0 +1,48 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var a = "abcd";
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var found = Array.prototype.find.call(a, function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return false;
});
assert.sameValue(o, a);
assert.sameValue(l, a.length);
assert.sameValue(v, "d");
assert.sameValue(k, 3);
assert.sameValue(found, undefined);
found = Array.prototype.find.apply(a, [function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return true;
}]);
assert.sameValue(o, a);
assert.sameValue(l, a.length);
assert.sameValue(v, "a");
assert.sameValue(k, 0);
assert.sameValue(found, "a");

View File

@ -0,0 +1,13 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
assert.sameValue(1, Array.prototype.findIndex.length);

View File

@ -0,0 +1,17 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
var a = [21, 22, 23, 24];
assert.sameValue(a.findIndex(function() { return false; }), -1);
assert.sameValue(a.findIndex(function(val) { return 121 === val; }), -1);
assert.sameValue(a.findIndex(function() { return true; }), 0);
assert.sameValue(a.findIndex(function(val) { return 22 === val; }), 1);
assert.sameValue(a.findIndex(function(val) { return 23 === val; }), 2);
assert.sameValue(a.findIndex(function(val) { return 24 === val; }), 3);

View File

@ -0,0 +1,24 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
//
// Test predicate is called array.length times
//
(function() {
var a = [1, 2, 3, 4, 5];
var l = 0;
a.findIndex(function() {
l++;
return false;
});
assert.sameValue(a.length, l);
})();

View File

@ -0,0 +1,107 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
// Test exceptions
assert.throws(TypeError, function() {
Array.prototype.findIndex.call(null, function() { });
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call(undefined, function() { });
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply(null, function() { }, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply(undefined, function() { }, []);
});
assert.throws(TypeError, function() {
[].findIndex(null);
});
assert.throws(TypeError, function() {
[].findIndex(undefined);
});
assert.throws(TypeError, function() {
[].findIndex(0);
});
assert.throws(TypeError, function() {
[].findIndex(true);
});
assert.throws(TypeError, function() {
[].findIndex(false);
});
assert.throws(TypeError, function() {
[].findIndex("");
});
assert.throws(TypeError, function() {
[].findIndex({});
});
assert.throws(TypeError, function() {
[].findIndex([]);
});
assert.throws(TypeError, function() {
[].findIndex(/\d+/);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, null);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, undefined);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, 0);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, true);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, false);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, "");
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, {});
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, /\d+/);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, null, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, undefined, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, 0, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, true, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, false, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, "", []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, {}, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, [], []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, /\d+/, []);
});

View File

@ -0,0 +1,42 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
includes: [compareArray.js]
---*/
//
// Test Array.prototype.findIndex works with exotic object
//
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var a = {
prop1: "val1",
prop2: "val2",
isValid: function() {
return this.prop1 === "val1" && this.prop2 === "val2";
}
};
Array.prototype.push.apply(a, [30, 31, 32]);
var index = Array.prototype.findIndex.call(a, function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return !obj.isValid();
});
assert(compareArray(o, a));
assert.sameValue(l, 3);
assert.sameValue(v, 32);
assert.sameValue(k, 2);
assert.sameValue(index, -1);

View File

@ -0,0 +1,23 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
includes: [compareArray.js]
---*/
//
// Test array modifications
//
var a = [1, 2, 3];
a.findIndex(function(val) { a.push(val); return false; });
assert(compareArray(a, [1, 2, 3, 1, 2, 3]));
assert.sameValue(a.length, 6);
a = [1, 2, 3];
a.findIndex(function(val, key) { a[key] = ++val; return false; });
assert(compareArray(a, [2, 3, 4]));
assert.sameValue(a.length, 3);

View File

@ -0,0 +1,32 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
//
// Test predicate is not called when array is empty
//
var a = [];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
a.findIndex(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assert.sameValue(-1, l);
assert.sameValue(-1, o);
assert.sameValue(-1, v);
assert.sameValue(-1, k);

View File

@ -0,0 +1,34 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
Test predicate is called with correct arguments
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
includes: [compareArray.js]
---*/
var a = ["b"];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var index = a.findIndex(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assert(compareArray(o, a));
assert.sameValue(l, a.length);
assert.sameValue(v, "b");
assert.sameValue(k, 0);
assert.sameValue(index, -1);

View File

@ -0,0 +1,13 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
In strict mode primitive value thisArg should not be coerced to an object.
flags: [onlyStrict]
---*/
var a = [];
[1, 2].findIndex(function() { "use strict"; a.push(this); }, "");
assert.sameValue(a[0], "");
assert.sameValue(a[1], a[0]);

View File

@ -0,0 +1,16 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
Create a new object in each function call when receiver is a
primitive value. See ECMA-262, Annex C.
---*/
var a = [];
[1, 2].findIndex(function() { a.push(this) }, "");
assert(a[0] !== a[1]);
var b = [];
[1, 2].findIndex(function() { b.push(this) }, 1);
assert(b[0] !== b[1]);

View File

@ -0,0 +1,12 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
Do not create a new object in each function call when receiver is a
non-primitive value. See ECMA-262, Annex C.
---*/
var a = [];
[1, 2].findIndex(function() { a.push(this) }, {});
assert.sameValue(a[1], a[0]);

View File

@ -0,0 +1,48 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
//
// Test thisArg
//
// Test String as a thisArg
var index = [1, 2, 3].findIndex(function(val, key) {
return this.charAt(Number(key)) === String(val);
}, "321");
assert.sameValue(index, 1);
// Test object as a thisArg
var thisArg = {
elementAt: function(key) {
return this[key];
}
};
Array.prototype.push.apply(thisArg, ["c", "b", "a"]);
index = ["a", "b", "c"].findIndex(function(val, key) {
return this.elementAt(key) === val;
}, thisArg);
assert.sameValue(index, 1);
// Create a new object in each function call when receiver is a
// primitive value. See ECMA-262, Annex C.
a = [];
[1, 2].findIndex(function() { a.push(this) }, "");
assert(a[0] !== a[1]);
// Do not create a new object otherwise.
a = [];
[1, 2].findIndex(function() { a.push(this) }, {});
assert.sameValue(a[1], a[0]);
// In strict mode primitive values should not be coerced to an object.
a = [];
[1, 2].findIndex(function() { 'use strict'; a.push(this); }, "");
assert.sameValue(a[0], "");
assert.sameValue(a[1], a[0]);

View File

@ -0,0 +1,11 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
Holes are not skipped
---*/
var count = 0;
[,,,,,].find(function() { count++; return false; });
assert.sameValue(count, 5);

View File

@ -0,0 +1,49 @@
// Copyright (c) 2013 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.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
Test Array.prototype.findIndex works with String
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
var a = "abcd";
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var index = Array.prototype.findIndex.call(a, function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return false;
});
assert.sameValue(o, a);
assert.sameValue(l, a.length);
assert.sameValue(v, "d");
assert.sameValue(k, 3);
assert.sameValue(index, -1);
index = Array.prototype.findIndex.apply(a, [function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return true;
}]);
assert.sameValue(o, a);
assert.sameValue(l, a.length);
assert.sameValue(v, "a");
assert.sameValue(k, 0);
assert.sameValue(index, 0);

View File

@ -16,13 +16,12 @@ includes:
function testcase() {
var str = new String("abc");
str[5] = "de";
var str = new String("abc");
str[5] = "de";
var expResult = ["0", "1", "2", "length", "5"];
var expected = ["0", "1", "2", "length", "5"];
var actual = Object.getOwnPropertyNames(str);
var result = Object.getOwnPropertyNames(str);
return compareArray(expResult, result);
}
return compareArray(actual.sort(), expected.sort());
}
runTestCase(testcase);

View File

@ -15,12 +15,10 @@ includes:
---*/
function testcase() {
var arr = [0, 1, 2];
var arr = [0, 1, 2];
var expected = ["0", "1", "2", "length"];
var actual = Object.getOwnPropertyNames(arr);
var expResult = ["0", "1", "2", "length"];
var result = Object.getOwnPropertyNames(arr);
return compareArray(expResult, result);
}
return compareArray(actual.sort(), expected.sort());
}
runTestCase(testcase);

View File

@ -7,41 +7,41 @@
/*---
es5id: 15.2.3.4-4-b-2
description: >
Object.getOwnPropertyNames - all own properties are pushed into
the returned array
Object.getOwnPropertyNames - all own properties are pushed into
the returned array
includes:
- runTestCase.js
- compareArray.js
- runTestCase.js
- compareArray.js
---*/
function testcase() {
var obj = { "a": "a" };
var obj = { "a": "a" };
Object.defineProperty(obj, "b", {
get: function () {
return "b";
},
enumerable: false,
configurable: true
});
Object.defineProperty(obj, "b", {
get: function () {
return "b";
},
enumerable: false,
configurable: true
});
Object.defineProperty(obj, "c", {
get: function () {
return "c";
},
enumerable: true,
configurable: true
});
Object.defineProperty(obj, "c", {
get: function () {
return "c";
},
enumerable: true,
configurable: true
});
Object.defineProperty(obj, "d", {
value: "d",
enumerable: false,
configurable: true
});
Object.defineProperty(obj, "d", {
value: "d",
enumerable: false,
configurable: true
});
var result = Object.getOwnPropertyNames(obj);
var expResult = ["a", "b", "c", "d"];
var actual = Object.getOwnPropertyNames(obj);
var expected = ["a", "b", "c", "d"];
return compareArray(expResult, result);
}
return compareArray(actual.sort(), expected.sort());
}
runTestCase(testcase);