Skip to content

Commit

Permalink
Simplify the tests and add documentation.
Browse files Browse the repository at this point in the history
New approach replaces ...Helper with CollectResize to make the tests easier to understand.
In particular:
- Replaced the need for '...Helper' abstractions with a more descriptive
  function 'ResizeBufferMidIteration', which the .every method can call
  directly.
- Moved 'let values' back outside of the loops, together with a function
  definition 'ResizeBufferMidIteration' to persist the side effects that
  'CollectValuesAndResize' has on 'values' but without having to define
  this inside every loop.
  + So this reverts commit d9a2bac and
  + this reverts commit 0bc2d66.
- Added documentation.
  • Loading branch information
ioannad authored and ptomato committed Jun 28, 2024
1 parent 8051692 commit 48a43bb
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@ includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/

const ArrayEveryHelper = (ta, values, rab, resizeAfter, resizeTo) => {
return Array.prototype.every.call(
ta,
(n) => CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo));
};
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) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
const values = [];
const resizeAfter = 2;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(ArrayEveryHelper(fixedLength, values, rab, resizeAfter, resizeTo));
values = [];
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(fixedLength, ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
Expand All @@ -36,24 +44,24 @@ for (let ctor of ctors) {
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const values = [];
const resizeAfter = 1;
const resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(ArrayEveryHelper(fixedLengthWithOffset, values, rab, resizeAfter, resizeTo));
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) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
const values = [];
const resizeAfter = 2;
const resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(ArrayEveryHelper(lengthTracking, values, rab, resizeAfter, resizeTo));
values = [];
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(lengthTracking, ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
Expand All @@ -62,12 +70,12 @@ for (let ctor of ctors) {
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
const values = [];
const resizeAfter = 1;
const resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(ArrayEveryHelper(lengthTrackingWithOffset, values, rab, resizeAfter, resizeTo));
values = [];
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(lengthTrackingWithOffset, ResizeBufferMidIteration));
assert.compareArray(values, [
4,
6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,65 @@ includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/

const ArrayEveryHelper = (ta, values, rab, resizeAfter, resizeTo) => {
return Array.prototype.every.call(
ta,
(n) => CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo));
};
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) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
const values = [];
const resizeAfter = 2;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(ArrayEveryHelper(fixedLength, values, rab, resizeAfter, resizeTo));
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) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const values = [];
const resizeAfter = 1;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(ArrayEveryHelper(fixedLengthWithOffset, values, rab, resizeAfter, resizeTo));
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) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
const values = [];
const resizeAfter = 2;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(ArrayEveryHelper(lengthTracking, values, rab, resizeAfter, resizeTo));
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) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
const values = [];
const resizeAfter = 1;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(ArrayEveryHelper(lengthTrackingWithOffset, values, rab, resizeAfter, resizeTo));
values = [];
resizeAfter = 1;
resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
assert(Array.prototype.every.call(lengthTrackingWithOffset, ResizeBufferMidIteration));
assert.compareArray(values, [4]);
}
80 changes: 45 additions & 35 deletions test/built-ins/Array/prototype/every/resizable-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ includes: [resizableArrayBufferUtils.js ]
features: [resizable-arraybuffer]
---*/

const ArrayEveryHelper = (ta, ...rest) => {
return Array.prototype.every.call(ta, ...rest);
};
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);
}

for (let ctor of ctors) {
const rab = CreateResizableArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, 8 * ctor.BYTES_PER_ELEMENT);
Expand Down Expand Up @@ -42,14 +52,14 @@ for (let ctor of ctors) {
function over10(n) {
return Number(n) > 10;
}
assert(!ArrayEveryHelper(fixedLength, div3));
assert(ArrayEveryHelper(fixedLength, even));
assert(!ArrayEveryHelper(fixedLengthWithOffset, div3));
assert(ArrayEveryHelper(fixedLengthWithOffset, even));
assert(!ArrayEveryHelper(lengthTracking, div3));
assert(ArrayEveryHelper(lengthTracking, even));
assert(!ArrayEveryHelper(lengthTrackingWithOffset, div3));
assert(ArrayEveryHelper(lengthTrackingWithOffset, even));
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);
Expand All @@ -59,33 +69,33 @@ for (let ctor of ctors) {
// [4, ...] << lengthTrackingWithOffset

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

assert(!ArrayEveryHelper(lengthTracking, div3));
assert(ArrayEveryHelper(lengthTracking, even));
assert(!ArrayEveryHelper(lengthTrackingWithOffset, div3));
assert(ArrayEveryHelper(lengthTrackingWithOffset, 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 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(ArrayEveryHelper(fixedLength, div3));
assert(ArrayEveryHelper(fixedLengthWithOffset, div3));
assert(ArrayEveryHelper(lengthTrackingWithOffset, div3));
assert(Array.prototype.every.call(fixedLength, div3));
assert(Array.prototype.every.call(fixedLengthWithOffset, div3));
assert(Array.prototype.every.call(lengthTrackingWithOffset, div3));

assert(ArrayEveryHelper(lengthTracking, div3));
assert(ArrayEveryHelper(lengthTracking, even));
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(ArrayEveryHelper(fixedLength, div3));
assert(ArrayEveryHelper(fixedLengthWithOffset, div3));
assert(ArrayEveryHelper(lengthTrackingWithOffset, div3));
assert(Array.prototype.every.call(fixedLength, div3));
assert(Array.prototype.every.call(fixedLengthWithOffset, div3));
assert(Array.prototype.every.call(lengthTrackingWithOffset, div3));

assert(ArrayEveryHelper(lengthTracking, div3));
assert(ArrayEveryHelper(lengthTracking, even));
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);
Expand All @@ -99,12 +109,12 @@ for (let ctor of ctors) {
// [0, 2, 4, 6, 8, 10, ...] << lengthTracking
// [4, 6, 8, 10, ...] << lengthTrackingWithOffset

assert(!ArrayEveryHelper(fixedLength, div3));
assert(ArrayEveryHelper(fixedLength, even));
assert(!ArrayEveryHelper(fixedLengthWithOffset, div3));
assert(ArrayEveryHelper(fixedLengthWithOffset, even));
assert(!ArrayEveryHelper(lengthTracking, div3));
assert(ArrayEveryHelper(lengthTracking, even));
assert(!ArrayEveryHelper(lengthTrackingWithOffset, div3));
assert(ArrayEveryHelper(lengthTrackingWithOffset, even));
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
Expand Up @@ -10,23 +10,32 @@ includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/

const TypedArrayEveryHelper = (ta, values, rab, resizeAfter, resizeTo) => {
return ta.every(
(n) => CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo));
};
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) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
const values = [];
const resizeAfter = 2;
const resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(TypedArrayEveryHelper(fixedLength, values, rab, resizeAfter, resizeTo));
values = [];
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(fixedLength.every(ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
Expand All @@ -35,24 +44,24 @@ for (let ctor of ctors) {
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const values = [];
const resizeAfter = 1;
const resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(TypedArrayEveryHelper(fixedLengthWithOffset, values, rab, resizeAfter, resizeTo));
values = [];
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(fixedLengthWithOffset.every(ResizeBufferMidIteration));
assert.compareArray(values, [
4,
6
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
const values = [];
const resizeAfter = 2;
const resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(TypedArrayEveryHelper(lengthTracking, values, rab, resizeAfter, resizeTo));
values = [];
resizeAfter = 2;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(lengthTracking.every(ResizeBufferMidIteration));
assert.compareArray(values, [
0,
2,
Expand All @@ -61,12 +70,12 @@ for (let ctor of ctors) {
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
const values = [];
const resizeAfter = 1;
const resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(TypedArrayEveryHelper(lengthTrackingWithOffset, values, rab, resizeAfter, resizeTo));
values = [];
resizeAfter = 1;
resizeTo = 5 * ctor.BYTES_PER_ELEMENT;
assert(lengthTrackingWithOffset.every(ResizeBufferMidIteration));
assert.compareArray(values, [
4,
6
Expand Down
Loading

0 comments on commit 48a43bb

Please sign in to comment.