Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RAB: Integrate staging tests for the .every method #4074

Merged
merged 10 commits into from
Jun 28, 2024
Original file line number Diff line number Diff line change
@@ -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-array.prototype.every
description: >
Array.p.every behaves correctly when receiver is backed by resizable
buffer that 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 ResizeBufferMidIteration(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) {
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
values = [];
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(fixedLength, ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
4,
6
]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
values = [];
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(fixedLengthWithOffset, ResizeBufferMidIteration));
assert.compareArray(values, [
4,
6
]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
values = [];
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(lengthTracking, ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
4,
6
]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
values = [];
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(lengthTrackingWithOffset, ResizeBufferMidIteration));
assert.compareArray(values, [
4,
6
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.every
description: >
Array.p.every behaves correctly when receiver is backed by resizable
buffer that 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 ResizeBufferMidIteration(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) {
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
values = [];
resizeAfter = 2;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(fixedLength, ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2
]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
values = [];
resizeAfter = 1;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(fixedLengthWithOffset, ResizeBufferMidIteration));
assert.compareArray(values, [4]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
values = [];
resizeAfter = 2;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(lengthTracking, ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
4
]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
values = [];
resizeAfter = 1;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(lengthTrackingWithOffset, ResizeBufferMidIteration));
assert.compareArray(values, [4]);
}
106 changes: 106 additions & 0 deletions test/built-ins/Array/prototype/every/resizable-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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.every
description: >
Array.p.every behaves correctly when the receiver is backed by
resizable buffer
includes: [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 div3(n) {
return Number(n) % 3 == 0;
}
function even(n) {
return Number(n) % 2 == 0;
}
function over10(n) {
return Number(n) > 10;
}
assert(!Array.prototype.every.call(fixedLength, div3));
assert(Array.prototype.every.call(fixedLength, even));
assert(!Array.prototype.every.call(fixedLengthWithOffset, div3));
assert(Array.prototype.every.call(fixedLengthWithOffset, even));
assert(!Array.prototype.every.call(lengthTracking, div3));
assert(Array.prototype.every.call(lengthTracking, even));
assert(!Array.prototype.every.call(lengthTrackingWithOffset, div3));
assert(Array.prototype.every.call(lengthTrackingWithOffset, even));

// 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

// Calling .every on an out of bounds TA doesn't throw.
assert(Array.prototype.every.call(fixedLength, div3));
assert(Array.prototype.every.call(fixedLengthWithOffset, div3));

assert(!Array.prototype.every.call(lengthTracking, div3));
assert(Array.prototype.every.call(lengthTracking, even));
assert(!Array.prototype.every.call(lengthTrackingWithOffset, div3));
assert(Array.prototype.every.call(lengthTrackingWithOffset, even));

// Shrink so that the TAs with offset go out of bounds.
rab.resize(1 * ctor.BYTES_PER_ELEMENT);
// Calling .every on an out of bounds TA doesn't throw.
assert(Array.prototype.every.call(fixedLength, div3));
assert(Array.prototype.every.call(fixedLengthWithOffset, div3));
assert(Array.prototype.every.call(lengthTrackingWithOffset, div3));

assert(Array.prototype.every.call(lengthTracking, div3));
assert(Array.prototype.every.call(lengthTracking, even));

// Shrink to zero.
rab.resize(0);
// Calling .every on an out of bounds TA doesn't throw.
assert(Array.prototype.every.call(fixedLength, div3));
assert(Array.prototype.every.call(fixedLengthWithOffset, div3));
assert(Array.prototype.every.call(lengthTrackingWithOffset, div3));

assert(Array.prototype.every.call(lengthTracking, div3));
assert(Array.prototype.every.call(lengthTracking, even));

// 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(!Array.prototype.every.call(fixedLength, div3));
assert(Array.prototype.every.call(fixedLength, even));
assert(!Array.prototype.every.call(fixedLengthWithOffset, div3));
assert(Array.prototype.every.call(fixedLengthWithOffset, even));
assert(!Array.prototype.every.call(lengthTracking, div3));
assert(Array.prototype.every.call(lengthTracking, even));
assert(!Array.prototype.every.call(lengthTrackingWithOffset, div3));
assert(Array.prototype.every.call(lengthTrackingWithOffset, even));
}
Original file line number Diff line number Diff line change
@@ -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.every
description: >
TypedArray.p.every behaves correctly when receiver is backed by resizable
buffer that 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 ResizeBufferMidIteration(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) {
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
values = [];
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(fixedLength.every(ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
4,
6
]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
values = [];
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(fixedLengthWithOffset.every(ResizeBufferMidIteration));
assert.compareArray(values, [
4,
6
]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
values = [];
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(lengthTracking.every(ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
4,
6
]);
}
for (let ctor of ctors) {
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
values = [];
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(lengthTrackingWithOffset.every(ResizeBufferMidIteration));
assert.compareArray(values, [
4,
6
]);
}
Loading
Loading