Skip to content

Commit

Permalink
Added missing 'shrink' test for Array.prototype.reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannad authored and ptomato committed Jul 24, 2024
1 parent 5b73026 commit 2023900
Showing 1 changed file with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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 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
]);
}

0 comments on commit 2023900

Please sign in to comment.