diff --git a/test/built-ins/Array/prototype/reduceRight/resizable-buffer-grow-mid-iteration.js b/test/built-ins/Array/prototype/reduceRight/resizable-buffer-grow-mid-iteration.js new file mode 100644 index 0000000000..875c905817 --- /dev/null +++ b/test/built-ins/Array/prototype/reduceRight/resizable-buffer-grow-mid-iteration.js @@ -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.reduceright +description: > + Array.p.reduceRight 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 ResizeBufferMidIteration(acc, 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 = 5 * ctor.BYTES_PER_ELEMENT; + Array.prototype.reduceRight.call(fixedLength, ResizeBufferMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4, + 2, + 0 + ]); +} +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.reduceRight.call(fixedLengthWithOffset, ResizeBufferMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4 + ]); +} +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.reduceRight.call(lengthTracking, ResizeBufferMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4, + 2, + 0 + ]); +} +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.reduceRight.call(lengthTrackingWithOffset, ResizeBufferMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4 + ]); +} diff --git a/test/built-ins/Array/prototype/reduceRight/resizable-buffer-shrink-mid-iteration.js b/test/built-ins/Array/prototype/reduceRight/resizable-buffer-shrink-mid-iteration.js new file mode 100644 index 0000000000..5c14e80dc1 --- /dev/null +++ b/test/built-ins/Array/prototype/reduceRight/resizable-buffer-shrink-mid-iteration.js @@ -0,0 +1,95 @@ +// 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.reduceright +description: > + Array.p.reduceRight 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) { + 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.reduceRight.call(fixedLength, ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4 + ]); +} +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.reduceRight.call(fixedLengthWithOffset, ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6 + ]); +} +for (let ctor of ctors) { + values = []; + rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + resizeAfter = 2; + resizeTo = 3 * ctor.BYTES_PER_ELEMENT; + // Unaffected by the shrinking, since we've already iterated past the point. + Array.prototype.reduceRight.call(lengthTracking, ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4, + 2, + 0 + ]); +} +for (let ctor of ctors) { + values = []; + rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + resizeAfter = 1; + resizeTo = 2 * ctor.BYTES_PER_ELEMENT; + Array.prototype.reduceRight.call(lengthTracking, ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 2, + 0 + ]); +} +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; + // Unaffected by the shrinking, since we've already iterated past the point. + Array.prototype.reduceRight.call(lengthTrackingWithOffset, ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4 + ]); +} diff --git a/test/built-ins/Array/prototype/reduceRight/resizable-buffer.js b/test/built-ins/Array/prototype/reduceRight/resizable-buffer.js new file mode 100644 index 0000000000..a1fa69c6b0 --- /dev/null +++ b/test/built-ins/Array/prototype/reduceRight/resizable-buffer.js @@ -0,0 +1,129 @@ +// 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.reduceright +description: > + Array.p.reduceRight 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 ReduceRightCollecting(array) { + const reduceRightValues = []; + Array.prototype.reduceRight.call(array, (acc, n) => { + reduceRightValues.push(n); + }, 'initial value'); + reduceRightValues.reverse(); + return ToNumbers(reduceRightValues); + } + assert.compareArray(ReduceRightCollecting(fixedLength), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(fixedLengthWithOffset), [ + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(lengthTracking), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(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(ReduceRightCollecting(fixedLength), []); + assert.compareArray(ReduceRightCollecting(fixedLengthWithOffset), []); + + assert.compareArray(ReduceRightCollecting(lengthTracking), [ + 0, + 2, + 4 + ]); + assert.compareArray(ReduceRightCollecting(lengthTrackingWithOffset), [4]); + + // Shrink so that the TAs with offset go out of bounds. + rab.resize(1 * ctor.BYTES_PER_ELEMENT); + assert.compareArray(ReduceRightCollecting(fixedLength), []); + assert.compareArray(ReduceRightCollecting(fixedLengthWithOffset), []); + + assert.compareArray(ReduceRightCollecting(lengthTracking), [0]); + + // Shrink to zero. + rab.resize(0); + assert.compareArray(ReduceRightCollecting(fixedLength), []); + assert.compareArray(ReduceRightCollecting(fixedLengthWithOffset), []); + assert.compareArray(ReduceRightCollecting(lengthTrackingWithOffset), []); + + assert.compareArray(ReduceRightCollecting(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(ReduceRightCollecting(fixedLength), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(fixedLengthWithOffset), [ + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(lengthTracking), [ + 0, + 2, + 4, + 6, + 8, + 10 + ]); + assert.compareArray(ReduceRightCollecting(lengthTrackingWithOffset), [ + 4, + 6, + 8, + 10 + ]); +} diff --git a/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer-grow-mid-iteration.js b/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer-grow-mid-iteration.js new file mode 100644 index 0000000000..48f935e89b --- /dev/null +++ b/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer-grow-mid-iteration.js @@ -0,0 +1,85 @@ +// 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.reduceright +description: > + TypedArray.p.reduceRight 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(acc, 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 + +// Test for reduceRight. + +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.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4, + 2, + 0 + ]); +} +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.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4 + ]); +} +for (let ctor of ctors) { + values = []; + rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + resizeAfter = 2; + resizeTo = 5 * ctor.BYTES_PER_ELEMENT; + lengthTracking.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4, + 2, + 0 + ]); +} +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.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4 + ]); +} diff --git a/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer-shrink-mid-iteration.js b/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer-shrink-mid-iteration.js new file mode 100644 index 0000000000..9f2745fcf4 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer-shrink-mid-iteration.js @@ -0,0 +1,101 @@ +// 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.reduceright +description: > + TypedArray.p.reduceRight behaves correctly on TypedArrays 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 ResizeMidIteration(acc, 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 + +// Tests for reduceRight. + +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.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4, + 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.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + undefined + ]); +} +for (let ctor of ctors) { + values = []; + rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + resizeAfter = 2; + resizeTo = 3 * ctor.BYTES_PER_ELEMENT; + // Unaffected by the shrinking, since we've already iterated past the point. + lengthTracking.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4, + 2, + 0 + ]); +} +for (let ctor of ctors) { + values = []; + rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + resizeAfter = 1; + resizeTo = 2 * ctor.BYTES_PER_ELEMENT; + lengthTracking.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + undefined, + 2, + 0 + ]); +} +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; + // Unaffected by the shrinking, since we've already iterated past the point. + lengthTrackingWithOffset.reduceRight(ResizeMidIteration, 'initial value'); + assert.compareArray(values, [ + 6, + 4 + ]); +} diff --git a/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer.js b/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer.js new file mode 100644 index 0000000000..a3c2770c06 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/reduceRight/resizable-buffer.js @@ -0,0 +1,146 @@ +// 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.reduceright +description: > + TypedArray.p.reduceRight 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 ReduceRightCollecting(array) { + const reduceRightValues = []; + array.reduceRight((acc, n) => { + reduceRightValues.push(n); + }, 'initial value'); + reduceRightValues.reverse(); + return ToNumbers(reduceRightValues); + } + assert.compareArray(ReduceRightCollecting(fixedLength), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(fixedLengthWithOffset), [ + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(lengthTracking), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(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, () => { + ReduceRightCollecting(fixedLength); + }); + assert.throws(TypeError, () => { + ReduceRightCollecting(fixedLengthWithOffset); + }); + + assert.compareArray(ReduceRightCollecting(lengthTracking), [ + 0, + 2, + 4 + ]); + assert.compareArray(ReduceRightCollecting(lengthTrackingWithOffset), [4]); + + // Shrink so that the TAs with offset go out of bounds. + rab.resize(1 * ctor.BYTES_PER_ELEMENT); + assert.throws(TypeError, () => { + ReduceRightCollecting(fixedLength); + }); + assert.throws(TypeError, () => { + ReduceRightCollecting(fixedLengthWithOffset); + }); + assert.throws(TypeError, () => { + ReduceRightCollecting(lengthTrackingWithOffset); + }); + + assert.compareArray(ReduceRightCollecting(lengthTracking), [0]); + + // Shrink to zero. + rab.resize(0); + assert.throws(TypeError, () => { + ReduceRightCollecting(fixedLength); + }); + assert.throws(TypeError, () => { + ReduceRightCollecting(fixedLengthWithOffset); + }); + assert.throws(TypeError, () => { + ReduceRightCollecting(lengthTrackingWithOffset); + }); + + assert.compareArray(ReduceRightCollecting(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(ReduceRightCollecting(fixedLength), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(fixedLengthWithOffset), [ + 4, + 6 + ]); + assert.compareArray(ReduceRightCollecting(lengthTracking), [ + 0, + 2, + 4, + 6, + 8, + 10 + ]); + assert.compareArray(ReduceRightCollecting(lengthTrackingWithOffset), [ + 4, + 6, + 8, + 10 + ]); +}