Add tests for TypedArrays forEach

This commit is contained in:
Leonardo Balter 2016-03-22 16:21:32 -04:00 committed by Mike Pennisi
parent 45e5fc8889
commit e01371e12a
12 changed files with 522 additions and 0 deletions

View File

@ -0,0 +1,46 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
[[ArrayLength]] is accessed in place of performing a [[Get]] of "length"
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample1 = new TA(42);
var loop = 0;
Object.defineProperty(sample1, "length", {value: 1});
sample1.forEach(function() {
loop++;
});
assert.sameValue(loop, 42, "data descriptor");
var sample2 = new TA(7);
loop = 0;
Object.defineProperty(sample2, "length", {
get: function() {
throw new Test262Error(
"Does not return abrupt getting length property"
);
}
});
sample2.forEach(function() {
loop++;
});
assert.sameValue(loop, 7, "accessor descriptor");
});

View File

@ -0,0 +1,54 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
thisArg does not affect callbackfn arguments
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
...
c. If kPresent is true, then
i. Let kValue be ? Get(O, Pk).
ii. Perform ? Call(callbackfn, T, « kValue, k, O »).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 44]);
var results = [];
var thisArg = ["test262", 0, "ecma262", 0];
sample.forEach(function() {
results.push(arguments);
}, thisArg);
assert.sameValue(results.length, 3, "results.length");
assert.sameValue(thisArg.length, 4, "thisArg.length");
assert.sameValue(results[0].length, 3, "results[0].length");
assert.sameValue(results[0][0], 42, "results[0][0] - kValue");
assert.sameValue(results[0][1], 0, "results[0][1] - k");
assert.sameValue(results[0][2], sample, "results[0][2] - this");
assert.sameValue(results[1].length, 3, "results[1].length");
assert.sameValue(results[1][0], 43, "results[1][0] - kValue");
assert.sameValue(results[1][1], 1, "results[1][1] - k");
assert.sameValue(results[1][2], sample, "results[1][2] - this");
assert.sameValue(results[2].length, 3, "results[2].length");
assert.sameValue(results[2][0], 44, "results[2][0] - kValue");
assert.sameValue(results[2][1], 2, "results[2][1] - k");
assert.sameValue(results[2][2], sample, "results[2][2] - this");
});

View File

@ -0,0 +1,52 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
callbackfn arguments
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
...
c. If kPresent is true, then
i. Let kValue be ? Get(O, Pk).
ii. Perform ? Call(callbackfn, T, « kValue, k, O »).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 44]);
var results = [];
sample.forEach(function() {
results.push(arguments);
});
assert.sameValue(results.length, 3, "results.length");
assert.sameValue(results[0].length, 3, "results[0].length");
assert.sameValue(results[0][0], 42, "results[0][0] - kValue");
assert.sameValue(results[0][1], 0, "results[0][1] - k");
assert.sameValue(results[0][2], sample, "results[0][2] - this");
assert.sameValue(results[1].length, 3, "results[1].length");
assert.sameValue(results[1][0], 43, "results[1][0] - kValue");
assert.sameValue(results[1][1], 1, "results[1][1] - k");
assert.sameValue(results[1][2], sample, "results[1][2] - this");
assert.sameValue(results[2].length, 3, "results[2].length");
assert.sameValue(results[2][0], 44, "results[2][0] - kValue");
assert.sameValue(results[2][1], 2, "results[2][1] - k");
assert.sameValue(results[2][2], sample, "results[2][2] - this");
});

View File

@ -0,0 +1,42 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
Instance buffer can be detached during loop
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
...
c. If kPresent is true, then
...
ii. Perform ? Call(callbackfn, T, « kValue, k, O »).
...
includes: [detachArrayBuffer.js, testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var loops = 0;
var sample = new TA(2);
assert.throws(TypeError, function() {
sample.forEach(function() {
if (loops === 1) {
throw new Test262Error("callbackfn called twice");
}
$DETACHBUFFER(sample.buffer);
loops++;
});
});
assert.sameValue(loops, 1);
});

View File

@ -0,0 +1,53 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
callbackfn is not callable
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(3);
assert.throws(TypeError, function() {
sample.forEach();
});
assert.throws(TypeError, function() {
sample.forEach(undefined);
});
assert.throws(TypeError, function() {
sample.forEach(null);
});
assert.throws(TypeError, function() {
sample.forEach({});
});
assert.throws(TypeError, function() {
sample.forEach(1);
});
assert.throws(TypeError, function() {
sample.forEach("");
});
assert.throws(TypeError, function() {
sample.forEach(false);
});
});

View File

@ -0,0 +1,41 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
Does not interact over non-integer properties
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
...
c. If kPresent is true, then
...
ii. Perform ? Call(callbackfn, T, « kValue, k, O »).
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([7, 8]);
var results = [];
sample.foo = 42;
sample[Symbol("1")] = 43;
sample.forEach(function(v, i) {
results.push([v, i]);
});
assert.sameValue(results.length, 2, "results.length");
assert.sameValue(results[0][1], 0, "results[0][1] - k");
assert.sameValue(results[1][1], 1, "results[1][1] - k");
assert.sameValue(results[0][0], 7, "results[0][0] - kValue");
assert.sameValue(results[1][0], 8, "results[1][0] - kValue");
});

View File

@ -0,0 +1,35 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
callbackfn is not called on empty instances
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
...
c. If kPresent is true, then
...
ii. Perform ? Call(callbackfn, T, « kValue, k, O »).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var called = 0;
var result1 = new TA().forEach(function() {
calls++;
});
assert.sameValue(called, 0);
});

View File

@ -0,0 +1,29 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
The callbackfn return does not change the instance
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample1 = new TA(3);
sample1[1] = 1;
sample1.forEach(function() {
return 42;
});
assert.sameValue(sample1[0], 0, "[0] == 0");
assert.sameValue(sample1[1], 1, "[1] == 1");
assert.sameValue(sample1[2], 0, "[2] == 0");
});

View File

@ -0,0 +1,35 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
Returns abrupt from callbackfn
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
...
c. If kPresent is true, then
...
ii. Perform ? Call(callbackfn, T, « kValue, k, O »).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(3);
assert.throws(Test262Error, function() {
sample.forEach(function() {
throw new Test262Error();
});
});
});

View File

@ -0,0 +1,47 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
Integer indexed values changed during iteration
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
includes: [testTypedArray.js]
features: [Reflect.set]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 44]);
var newVal = 0;
sample.forEach(function(val, i) {
if (i > 0) {
assert.sameValue(
sample[i - 1], newVal - 1,
"get the changed value during the loop"
);
assert.sameValue(
Reflect.set(sample, 0, 7),
true,
"re-set a value for sample[0]"
);
}
assert.sameValue(
Reflect.set(sample, i, newVal),
true,
"set value during interaction"
);
newVal++;
});
assert.sameValue(sample[0], 7, "changed values after interaction [0] == 7");
assert.sameValue(sample[1], 1, "changed values after interaction [1] == 1");
assert.sameValue(sample[2], 2, "changed values after interaction [2] == 2");
});

View File

@ -0,0 +1,56 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
callbackfn `this` value
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
...
4. If thisArg was supplied, let T be thisArg; else let T be undefined.
...
6. Repeat, while k < len
...
c. If kPresent is true, then
...
ii. Perform ? Call(callbackfn, T, « kValue, k, O »).
...
includes: [testTypedArray.js]
---*/
var expected = (function() { return this; })();
var thisArg = {};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(3);
var results1 = [];
sample.forEach(function() {
results1.push(this);
});
assert.sameValue(results1.length, 3, "results1");
assert.sameValue(results1[0], expected, "without thisArg - [0]");
assert.sameValue(results1[1], expected, "without thisArg - [1]");
assert.sameValue(results1[2], expected, "without thisArg - [2]");
var results2 = [];
sample.forEach(function() {
results2.push(this);
}, thisArg);
assert.sameValue(results2.length, 3, "results2");
assert.sameValue(results2[0], thisArg, "using thisArg - [0]");
assert.sameValue(results2[1], thisArg, "using thisArg - [1]");
assert.sameValue(results2[2], thisArg, "using thisArg - [2]");
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.foreach
description: >
Returns undefined
info: >
22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
%TypedArray%.prototype.forEach is a distinct function that implements the same
algorithm as Array.prototype.forEach as defined in 22.1.3.10 except that the
this object's [[ArrayLength]] internal slot is accessed in place of performing
a [[Get]] of "length"
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample1 = new TA(42);
var result1 = sample1.forEach(function() {
return 42;
});
assert.sameValue(result1, undefined, "result1");
var sample2 = new TA(1);
var result2 = sample2.forEach(function() {
return null;
});
assert.sameValue(result2, undefined, "result2");
});