From d797bb979e717ef93b58650760e475895ea771c2 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Wed, 4 Mar 2015 10:30:59 -0500 Subject: [PATCH] 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 --- harness/compareArray.js | 22 ++-- test/built-ins/Array/from/Array.from_arity.js | 12 ++ ...ay.from_forwards-length-for-array-likes.js | 19 +++ ...rray.of_assignment-to-new-object-length.js | 20 ++++ test/built-ins/Array/of/Array.of_basics.js | 23 ++++ ...calls-a-length-setter-if-one-is-present.js | 36 ++++++ ...of_can-be-transplanted-to-other-classes.js | 37 ++++++ .../of/Array.of_check-superficial-features.js | 45 +++++++ .../Array/of/Array.of_does-not-leave-holes.js | 20 ++++ ...y.of_does-not-trigger-prototype-setters.js | 32 +++++ .../Array/of/Array.of_makes-real-arrays.js | 28 +++++ ...f-arguments-to-the-constructor-it-calls.js | 26 ++++ .../fill/Array.prototype.fill_basic.js | 31 +++++ ...prototype.fill_cannot-fill-frozen-array.js | 16 +++ .../fill/Array.prototype.fill_exceptions.js | 19 +++ .../Array.prototype.find_array-modified.js | 24 ++++ .../find/Array.prototype.find_basic.js | 23 ++++ ...rray.prototype.find_called-length-times.js | 22 ++++ .../find/Array.prototype.find_exceptions.js | 111 ++++++++++++++++++ .../Array.prototype.find_exotic-object.js | 40 +++++++ ...rototype.find_not-called-on-empty-array.js | 33 ++++++ ...find_predicate-called-correct-arguments.js | 33 ++++++ .../find/Array.prototype.find_skip-empty.js | 21 ---- .../find/Array.prototype.find_this-arg.js | 49 ++++++++ ...d_treats-holes-as-undefined-not-skipped.js | 18 +++ .../find/Array.prototype.find_with-string.js | 48 ++++++++ .../Array.prototype.findIndex_arity.js | 13 ++ .../Array.prototype.findIndex_basic.js | 17 +++ ...prototype.findIndex_called-length-times.js | 24 ++++ .../Array.prototype.findIndex_exceptions.js | 107 +++++++++++++++++ ...Array.prototype.findIndex_exotic-object.js | 42 +++++++ ...Array.prototype.findIndex_modifications.js | 23 ++++ ...ype.findIndex_not-called-on-empty-array.js | 32 +++++ ...prototype.findIndex_predicate-arguments.js | 34 ++++++ ...Index_this-arg-receiver-coercion-strict.js | 13 ++ ...e.findIndex_this-arg-receiver-primitive.js | 16 +++ ...e.findIndex_this-arg-receiver-reference.js | 12 ++ .../Array.prototype.findIndex_this-arg.js | 48 ++++++++ ...x_treats-holes-as-undefined-not-skipped.js | 11 ++ .../Array.prototype.findIndex_with-string.js | 49 ++++++++ .../getOwnPropertyNames/15.2.3.4-4-44.js | 13 +- .../getOwnPropertyNames/15.2.3.4-4-49.js | 12 +- .../getOwnPropertyNames/15.2.3.4-4-b-2.js | 56 ++++----- 43 files changed, 1254 insertions(+), 76 deletions(-) create mode 100644 test/built-ins/Array/from/Array.from_arity.js create mode 100644 test/built-ins/Array/from/Array.from_forwards-length-for-array-likes.js create mode 100644 test/built-ins/Array/of/Array.of_assignment-to-new-object-length.js create mode 100644 test/built-ins/Array/of/Array.of_basics.js create mode 100644 test/built-ins/Array/of/Array.of_calls-a-length-setter-if-one-is-present.js create mode 100644 test/built-ins/Array/of/Array.of_can-be-transplanted-to-other-classes.js create mode 100644 test/built-ins/Array/of/Array.of_check-superficial-features.js create mode 100644 test/built-ins/Array/of/Array.of_does-not-leave-holes.js create mode 100644 test/built-ins/Array/of/Array.of_does-not-trigger-prototype-setters.js create mode 100644 test/built-ins/Array/of/Array.of_makes-real-arrays.js create mode 100644 test/built-ins/Array/of/Array.of_passes-the-number-of-arguments-to-the-constructor-it-calls.js create mode 100644 test/built-ins/Array/prototype/fill/Array.prototype.fill_basic.js create mode 100644 test/built-ins/Array/prototype/fill/Array.prototype.fill_cannot-fill-frozen-array.js create mode 100644 test/built-ins/Array/prototype/fill/Array.prototype.fill_exceptions.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_array-modified.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_basic.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_called-length-times.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_exceptions.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_exotic-object.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_not-called-on-empty-array.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_predicate-called-correct-arguments.js delete mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_skip-empty.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_this-arg.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_treats-holes-as-undefined-not-skipped.js create mode 100644 test/built-ins/Array/prototype/find/Array.prototype.find_with-string.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_arity.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_basic.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_called-length-times.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exceptions.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exotic-object.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_modifications.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_not-called-on-empty-array.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_predicate-arguments.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-coercion-strict.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-primitive.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-reference.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_treats-holes-as-undefined-not-skipped.js create mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_with-string.js diff --git a/harness/compareArray.js b/harness/compareArray.js index 9b842ed5c6..6eb69e6eba 100644 --- a/harness/compareArray.js +++ b/harness/compareArray.js @@ -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; } diff --git a/test/built-ins/Array/from/Array.from_arity.js b/test/built-ins/Array/from/Array.from_arity.js new file mode 100644 index 0000000000..354f281ba4 --- /dev/null +++ b/test/built-ins/Array/from/Array.from_arity.js @@ -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); diff --git a/test/built-ins/Array/from/Array.from_forwards-length-for-array-likes.js b/test/built-ins/Array/from/Array.from_forwards-length-for-array-likes.js new file mode 100644 index 0000000000..df603c871e --- /dev/null +++ b/test/built-ins/Array/from/Array.from_forwards-length-for-array-likes.js @@ -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); diff --git a/test/built-ins/Array/of/Array.of_assignment-to-new-object-length.js b/test/built-ins/Array/of/Array.of_assignment-to-new-object-length.js new file mode 100644 index 0000000000..a74b751346 --- /dev/null +++ b/test/built-ins/Array/of/Array.of_assignment-to-new-object-length.js @@ -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(); }); diff --git a/test/built-ins/Array/of/Array.of_basics.js b/test/built-ins/Array/of/Array.of_basics.js new file mode 100644 index 0000000000..7f1a2fd876 --- /dev/null +++ b/test/built-ins/Array/of/Array.of_basics.js @@ -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)); diff --git a/test/built-ins/Array/of/Array.of_calls-a-length-setter-if-one-is-present.js b/test/built-ins/Array/of/Array.of_calls-a-length-setter-if-one-is-present.js new file mode 100644 index 0000000000..57c19655c5 --- /dev/null +++ b/test/built-ins/Array/of/Array.of_calls-a-length-setter-if-one-is-present.js @@ -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); diff --git a/test/built-ins/Array/of/Array.of_can-be-transplanted-to-other-classes.js b/test/built-ins/Array/of/Array.of_can-be-transplanted-to-other-classes.js new file mode 100644 index 0000000000..234f8e10a5 --- /dev/null +++ b/test/built-ins/Array/of/Array.of_can-be-transplanted-to-other-classes.js @@ -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)); + diff --git a/test/built-ins/Array/of/Array.of_check-superficial-features.js b/test/built-ins/Array/of/Array.of_check-superficial-features.js new file mode 100644 index 0000000000..ea8f809840 --- /dev/null +++ b/test/built-ins/Array/of/Array.of_check-superficial-features.js @@ -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); diff --git a/test/built-ins/Array/of/Array.of_does-not-leave-holes.js b/test/built-ins/Array/of/Array.of_does-not-leave-holes.js new file mode 100644 index 0000000000..1e68f97475 --- /dev/null +++ b/test/built-ins/Array/of/Array.of_does-not-leave-holes.js @@ -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])); + diff --git a/test/built-ins/Array/of/Array.of_does-not-trigger-prototype-setters.js b/test/built-ins/Array/of/Array.of_does-not-trigger-prototype-setters.js new file mode 100644 index 0000000000..add037ff61 --- /dev/null +++ b/test/built-ins/Array/of/Array.of_does-not-trigger-prototype-setters.js @@ -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"); + diff --git a/test/built-ins/Array/of/Array.of_makes-real-arrays.js b/test/built-ins/Array/of/Array.of_makes-real-arrays.js new file mode 100644 index 0000000000..f3bf5e895b --- /dev/null +++ b/test/built-ins/Array/of/Array.of_makes-real-arrays.js @@ -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()); + diff --git a/test/built-ins/Array/of/Array.of_passes-the-number-of-arguments-to-the-constructor-it-calls.js b/test/built-ins/Array/of/Array.of_passes-the-number-of-arguments-to-the-constructor-it-calls.js new file mode 100644 index 0000000000..61619a0d5d --- /dev/null +++ b/test/built-ins/Array/of/Array.of_passes-the-number-of-arguments-to-the-constructor-it-calls.js @@ -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); diff --git a/test/built-ins/Array/prototype/fill/Array.prototype.fill_basic.js b/test/built-ins/Array/prototype/fill/Array.prototype.fill_basic.js new file mode 100644 index 0000000000..e19d3ab36e --- /dev/null +++ b/test/built-ins/Array/prototype/fill/Array.prototype.fill_basic.js @@ -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])); diff --git a/test/built-ins/Array/prototype/fill/Array.prototype.fill_cannot-fill-frozen-array.js b/test/built-ins/Array/prototype/fill/Array.prototype.fill_cannot-fill-frozen-array.js new file mode 100644 index 0000000000..e091c6c5d7 --- /dev/null +++ b/test/built-ins/Array/prototype/fill/Array.prototype.fill_cannot-fill-frozen-array.js @@ -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(); +}); diff --git a/test/built-ins/Array/prototype/fill/Array.prototype.fill_exceptions.js b/test/built-ins/Array/prototype/fill/Array.prototype.fill_exceptions.js new file mode 100644 index 0000000000..075c220c41 --- /dev/null +++ b/test/built-ins/Array/prototype/fill/Array.prototype.fill_exceptions.js @@ -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); +}); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_array-modified.js b/test/built-ins/Array/prototype/find/Array.prototype.find_array-modified.js new file mode 100644 index 0000000000..0aa20aa5ed --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_array-modified.js @@ -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); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_basic.js b/test/built-ins/Array/prototype/find/Array.prototype.find_basic.js new file mode 100644 index 0000000000..24ae800e60 --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_basic.js @@ -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); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_called-length-times.js b/test/built-ins/Array/prototype/find/Array.prototype.find_called-length-times.js new file mode 100644 index 0000000000..9e9e1a1767 --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_called-length-times.js @@ -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); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_exceptions.js b/test/built-ins/Array/prototype/find/Array.prototype.find_exceptions.js new file mode 100644 index 0000000000..0dba5e7fa2 --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_exceptions.js @@ -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+/, []); +}); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_exotic-object.js b/test/built-ins/Array/prototype/find/Array.prototype.find_exotic-object.js new file mode 100644 index 0000000000..4b210581c6 --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_exotic-object.js @@ -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); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_not-called-on-empty-array.js b/test/built-ins/Array/prototype/find/Array.prototype.find_not-called-on-empty-array.js new file mode 100644 index 0000000000..11c7df7175 --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_not-called-on-empty-array.js @@ -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); + diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_predicate-called-correct-arguments.js b/test/built-ins/Array/prototype/find/Array.prototype.find_predicate-called-correct-arguments.js new file mode 100644 index 0000000000..9368853375 --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_predicate-called-correct-arguments.js @@ -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); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_skip-empty.js b/test/built-ins/Array/prototype/find/Array.prototype.find_skip-empty.js deleted file mode 100644 index 7607e74ad7..0000000000 --- a/test/built-ins/Array/prototype/find/Array.prototype.find_skip-empty.js +++ /dev/null @@ -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); -} diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_this-arg.js b/test/built-ins/Array/prototype/find/Array.prototype.find_this-arg.js new file mode 100644 index 0000000000..f422134417 --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_this-arg.js @@ -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]); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_treats-holes-as-undefined-not-skipped.js b/test/built-ins/Array/prototype/find/Array.prototype.find_treats-holes-as-undefined-not-skipped.js new file mode 100644 index 0000000000..b47a8c8612 --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_treats-holes-as-undefined-not-skipped.js @@ -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); diff --git a/test/built-ins/Array/prototype/find/Array.prototype.find_with-string.js b/test/built-ins/Array/prototype/find/Array.prototype.find_with-string.js new file mode 100644 index 0000000000..40a6163cbd --- /dev/null +++ b/test/built-ins/Array/prototype/find/Array.prototype.find_with-string.js @@ -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"); + diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_arity.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_arity.js new file mode 100644 index 0000000000..622ceb5623 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_arity.js @@ -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); + diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_basic.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_basic.js new file mode 100644 index 0000000000..2b9db5c681 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_basic.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_called-length-times.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_called-length-times.js new file mode 100644 index 0000000000..dc3cbe85e7 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_called-length-times.js @@ -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); +})(); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exceptions.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exceptions.js new file mode 100644 index 0000000000..11abc79b1c --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exceptions.js @@ -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+/, []); +}); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exotic-object.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exotic-object.js new file mode 100644 index 0000000000..888b17e179 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exotic-object.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_modifications.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_modifications.js new file mode 100644 index 0000000000..a1c8d28b41 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_modifications.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_not-called-on-empty-array.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_not-called-on-empty-array.js new file mode 100644 index 0000000000..9f9db8b324 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_not-called-on-empty-array.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_predicate-arguments.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_predicate-arguments.js new file mode 100644 index 0000000000..08c4a7dee1 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_predicate-arguments.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-coercion-strict.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-coercion-strict.js new file mode 100644 index 0000000000..efa90c32bf --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-coercion-strict.js @@ -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]); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-primitive.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-primitive.js new file mode 100644 index 0000000000..a6ca7ec54c --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-primitive.js @@ -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]); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-reference.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-reference.js new file mode 100644 index 0000000000..4c89bd813b --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-reference.js @@ -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]); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg.js new file mode 100644 index 0000000000..9798ca79a6 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg.js @@ -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]); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_treats-holes-as-undefined-not-skipped.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_treats-holes-as-undefined-not-skipped.js new file mode 100644 index 0000000000..489d41c02f --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_treats-holes-as-undefined-not-skipped.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_with-string.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_with-string.js new file mode 100644 index 0000000000..a48fc0504e --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_with-string.js @@ -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); + diff --git a/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-44.js b/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-44.js index 232015fb12..683c888792 100644 --- a/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-44.js +++ b/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-44.js @@ -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); diff --git a/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-49.js b/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-49.js index 5e5a6fc546..9246232b21 100644 --- a/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-49.js +++ b/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-49.js @@ -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); diff --git a/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-b-2.js b/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-b-2.js index e8257e71e0..5adc29a5ac 100644 --- a/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-b-2.js +++ b/test/built-ins/Object/getOwnPropertyNames/15.2.3.4-4-b-2.js @@ -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);