RAB: Integrate staging tests for the .reduce method (#4156)

* Import relevant files from #3888

* Removing parts in resizableArrayBufferUtils.js and adding it in includes,
while adjusting usage of CollectValuesAndResize and applying review changes
from PRs for previously tested methods.

* Added missing 'shrink' test for Array.prototype.reduce

* Deleted accidental files

* Fix accidental call of .map instead of .reduce

Addresses review comments

* fix copyright in new file not in origin PR
This commit is contained in:
Ioanna M Dimitriou H 2024-07-30 02:33:50 +02:00 committed by GitHub
parent 6add1a22cd
commit 1842afcbce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 603 additions and 0 deletions

View File

@ -0,0 +1,86 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.reduce
description: >
Array.p.reduce behaves correctly when the resizable buffer is grown
mid-iteration.
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/
let values;
let rab;
let resizeAfter;
let resizeTo;
// Collects the view of the resizable array buffer rab into values, with an
// iteration during which, after resizeAfter steps, rab is resized to length
// resizeTo. To be called by a method of the view being collected.
// Note that rab, values, resizeAfter, and resizeTo may need to be reset
// before calling this.
function ResizeMidIteration(acc, n) {
// Returns true by default.
return CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo);
}
// Orig. array: [0, 2, 4, 6]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, ...] << lengthTracking
// [4, 6, ...] << lengthTrackingWithOffset
// Test for reduce.
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
Array.prototype.reduce.call(fixedLength, ResizeMidIteration, 'initial value');
assert.compareArray(values, [
0,
2,
4,
6
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
Array.prototype.reduce.call(fixedLengthWithOffset, ResizeMidIteration, 'initial value');
assert.compareArray(values, [
4,
6
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
Array.prototype.reduce.call(lengthTracking, ResizeMidIteration, 'initial value');
assert.compareArray(values, [
0,
2,
4,
6
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
Array.prototype.reduce.call(lengthTrackingWithOffset, ResizeMidIteration, 'initial value');
assert.compareArray(values, [
4,
6
]);
}

View File

@ -0,0 +1,79 @@
// Copyright 2024 Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%array%.prototype.reduce
description: >
Array.p.reduce behaves correctly when the backing resizable buffer is shrunk
mid-iteration.
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/
let values;
let rab;
let resizeAfter;
let resizeTo;
// Collects the view of the resizable array buffer rab into values, with an
// iteration during which, after resizeAfter steps, rab is resized to length
// resizeTo. To be called by a method of the view being collected.
// Note that rab, values, resizeAfter, and resizeTo may need to be reset
// before calling this.
function ResizeMidIteration(acc, n) {
// Returns true by default.
return CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo);
}
// Orig. array: [0, 2, 4, 6]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, ...] << lengthTracking
// [4, 6, ...] << lengthTrackingWithOffset
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
resizeAfter = 2;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
Array.prototype.reduce.call(fixedLength, ResizeMidIteration, 'initial value');
assert.compareArray(values, [
0,
2
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
resizeAfter = 1;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
Array.prototype.reduce.call(fixedLengthWithOffset, ResizeMidIteration, 'initial value');
assert.compareArray(values, [
4
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
resizeAfter = 2;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
Array.prototype.reduce.call(lengthTracking, ResizeMidIteration, 'initial value');
assert.compareArray(values, [
0,
2,
4
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
resizeAfter = 1;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
Array.prototype.reduce.call(lengthTrackingWithOffset, ResizeMidIteration, 'initial value');
assert.compareArray(values, [
4
]);
}

View File

@ -0,0 +1,126 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.reduce
description: >
Array.p.reduce behaves correctly on TypedArrays backed by resizable buffers.
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/
for (let ctor of ctors) {
const rab = CreateResizableArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, 8 * ctor.BYTES_PER_ELEMENT);
const fixedLength = new ctor(rab, 0, 4);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(rab, 0);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
// Write some data into the array.
const taWrite = new ctor(rab);
for (let i = 0; i < 4; ++i) {
WriteToTypedArray(taWrite, i, 2 * i);
}
// Orig. array: [0, 2, 4, 6]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, ...] << lengthTracking
// [4, 6, ...] << lengthTrackingWithOffset
function ReduceCollecting(array) {
const reduceValues = [];
Array.prototype.reduce.call(array, (acc, n) => {
reduceValues.push(n);
}, 'initial value');
return ToNumbers(reduceValues);
}
assert.compareArray(ReduceCollecting(fixedLength), [
0,
2,
4,
6
]);
assert.compareArray(ReduceCollecting(fixedLengthWithOffset), [
4,
6
]);
assert.compareArray(ReduceCollecting(lengthTracking), [
0,
2,
4,
6
]);
assert.compareArray(ReduceCollecting(lengthTrackingWithOffset), [
4,
6
]);
// Shrink so that fixed length TAs go out of bounds.
rab.resize(3 * ctor.BYTES_PER_ELEMENT);
// Orig. array: [0, 2, 4]
// [0, 2, 4, ...] << lengthTracking
// [4, ...] << lengthTrackingWithOffset
assert.compareArray(ReduceCollecting(fixedLength), []);
assert.compareArray(ReduceCollecting(fixedLengthWithOffset), []);
assert.compareArray(ReduceCollecting(lengthTracking), [
0,
2,
4
]);
assert.compareArray(ReduceCollecting(lengthTrackingWithOffset), [4]);
// Shrink so that the TAs with offset go out of bounds.
rab.resize(1 * ctor.BYTES_PER_ELEMENT);
assert.compareArray(ReduceCollecting(fixedLength), []);
assert.compareArray(ReduceCollecting(fixedLengthWithOffset), []);
assert.compareArray(ReduceCollecting(lengthTracking), [0]);
// Shrink to zero.
rab.resize(0);
assert.compareArray(ReduceCollecting(fixedLength), []);
assert.compareArray(ReduceCollecting(fixedLengthWithOffset), []);
assert.compareArray(ReduceCollecting(lengthTrackingWithOffset), []);
assert.compareArray(ReduceCollecting(lengthTracking), []);
// Grow so that all TAs are back in-bounds.
rab.resize(6 * ctor.BYTES_PER_ELEMENT);
for (let i = 0; i < 6; ++i) {
WriteToTypedArray(taWrite, i, 2 * i);
}
// Orig. array: [0, 2, 4, 6, 8, 10]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, 8, 10, ...] << lengthTracking
// [4, 6, 8, 10, ...] << lengthTrackingWithOffset
assert.compareArray(ReduceCollecting(fixedLength), [
0,
2,
4,
6
]);
assert.compareArray(ReduceCollecting(fixedLengthWithOffset), [
4,
6
]);
assert.compareArray(ReduceCollecting(lengthTracking), [
0,
2,
4,
6,
8,
10
]);
assert.compareArray(ReduceCollecting(lengthTrackingWithOffset), [
4,
6,
8,
10
]);
}

View File

@ -0,0 +1,84 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.reduce
description: >
TypedArray.p.reduce behaves correctly on TypedArrays backed by resizable
buffers that are grown mid-iteration.
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/
let values;
let rab;
let resizeAfter;
let resizeTo;
// Collects the view of the resizable array buffer rab into values, with an
// iteration during which, after resizeAfter steps, rab is resized to length
// resizeTo. To be called by a method of the view being collected.
// Note that rab, values, resizeAfter, and resizeTo may need to be reset
// before calling this.
function ResizeMidIteration(n) {
// Returns true by default.
return CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo);
}
// Orig. array: [0, 2, 4, 6]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, ...] << lengthTracking
// [4, 6, ...] << lengthTrackingWithOffset
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
fixedLength.reduce(ResizeMidIteration, 'initial value');
assert.compareArray(values, [
0,
2,
4,
6
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
fixedLengthWithOffset.reduce(ResizeMidIteration, 'initial value');
assert.compareArray(values, [
4,
6
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
lengthTracking.reduce(ResizeMidIteration, 'initial value');
assert.compareArray(values, [
0,
2,
4,
6
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
lengthTrackingWithOffset.reduce(ResizeMidIteration, 'initial value');
assert.compareArray(values, [
4,
6
]);
}

View File

@ -0,0 +1,83 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.reduce
description: >
TypedArray.p.reduce behaves correctly on TypedArrays backed by resizable
buffers that are shrunk mid-iteration.
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/
let values;
let rab;
let resizeAfter;
let resizeTo;
// Collects the view of the resizable array buffer rab into values, with an
// iteration during which, after resizeAfter steps, rab is resized to length
// resizeTo. To be called by a method of the view being collected.
// Note that rab, values, resizeAfter, and resizeTo may need to be reset
// before calling this.
function ResizeMidIteration(n) {
return CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo);
}
// Orig. array: [0, 2, 4, 6]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, ...] << lengthTracking
// [4, 6, ...] << lengthTrackingWithOffset
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
resizeAfter = 2;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
fixedLength.reduce(ResizeMidIteration, 'initial value');
assert.compareArray(values, [
0,
2,
undefined,
undefined
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
resizeAfter = 1;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
fixedLengthWithOffset.reduce(ResizeMidIteration, 'initial value');
assert.compareArray(values, [
4,
undefined
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
resizeAfter = 2;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
lengthTracking.reduce(ResizeMidIteration, 'initial value');
assert.compareArray(values, [
0,
2,
4,
undefined
]);
}
for (let ctor of ctors) {
values = [];
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
resizeAfter = 1;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
lengthTrackingWithOffset.reduce(ResizeMidIteration, 'initial value');
assert.compareArray(values, [
4,
undefined
]);
}

View File

@ -0,0 +1,145 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.reduce
description: >
TypedArray.p.reduce behaves correctly on TypedArrays backed by resizable
buffers.
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/
for (let ctor of ctors) {
const rab = CreateResizableArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, 8 * ctor.BYTES_PER_ELEMENT);
const fixedLength = new ctor(rab, 0, 4);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(rab, 0);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
// Write some data into the array.
const taWrite = new ctor(rab);
for (let i = 0; i < 4; ++i) {
WriteToTypedArray(taWrite, i, 2 * i);
}
// Orig. array: [0, 2, 4, 6]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, ...] << lengthTracking
// [4, 6, ...] << lengthTrackingWithOffset
function ReduceCollecting(array) {
const reduceValues = [];
array.reduce((acc, n) => {
reduceValues.push(n);
}, 'initial value');
return ToNumbers(reduceValues);
}
assert.compareArray(ReduceCollecting(fixedLength), [
0,
2,
4,
6
]);
assert.compareArray(ReduceCollecting(fixedLengthWithOffset), [
4,
6
]);
assert.compareArray(ReduceCollecting(lengthTracking), [
0,
2,
4,
6
]);
assert.compareArray(ReduceCollecting(lengthTrackingWithOffset), [
4,
6
]);
// Shrink so that fixed length TAs go out of bounds.
rab.resize(3 * ctor.BYTES_PER_ELEMENT);
// Orig. array: [0, 2, 4]
// [0, 2, 4, ...] << lengthTracking
// [4, ...] << lengthTrackingWithOffset
assert.throws(TypeError, () => {
ReduceCollecting(fixedLength);
});
assert.throws(TypeError, () => {
ReduceCollecting(fixedLengthWithOffset);
});
assert.compareArray(ReduceCollecting(lengthTracking), [
0,
2,
4
]);
assert.compareArray(ReduceCollecting(lengthTrackingWithOffset), [4]);
// Shrink so that the TAs with offset go out of bounds.
rab.resize(1 * ctor.BYTES_PER_ELEMENT);
assert.throws(TypeError, () => {
ReduceCollecting(fixedLength);
});
assert.throws(TypeError, () => {
ReduceCollecting(fixedLengthWithOffset);
});
assert.throws(TypeError, () => {
ReduceCollecting(lengthTrackingWithOffset);
});
assert.compareArray(ReduceCollecting(lengthTracking), [0]);
// Shrink to zero.
rab.resize(0);
assert.throws(TypeError, () => {
ReduceCollecting(fixedLength);
});
assert.throws(TypeError, () => {
ReduceCollecting(fixedLengthWithOffset);
});
assert.throws(TypeError, () => {
ReduceCollecting(lengthTrackingWithOffset);
});
assert.compareArray(ReduceCollecting(lengthTracking), []);
// Grow so that all TAs are back in-bounds.
rab.resize(6 * ctor.BYTES_PER_ELEMENT);
for (let i = 0; i < 6; ++i) {
WriteToTypedArray(taWrite, i, 2 * i);
}
// Orig. array: [0, 2, 4, 6, 8, 10]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, 8, 10, ...] << lengthTracking
// [4, 6, 8, 10, ...] << lengthTrackingWithOffset
assert.compareArray(ReduceCollecting(fixedLength), [
0,
2,
4,
6
]);
assert.compareArray(ReduceCollecting(fixedLengthWithOffset), [
4,
6
]);
assert.compareArray(ReduceCollecting(lengthTracking), [
0,
2,
4,
6,
8,
10
]);
assert.compareArray(ReduceCollecting(lengthTrackingWithOffset), [
4,
6,
8,
10
]);
}