From dc36c7eae9ea419d2ab007741318933ececee0f2 Mon Sep 17 00:00:00 2001 From: Ioanna M Dimitriou H <9728696+ioannad@users.noreply.github.com> Date: Tue, 6 Aug 2024 02:26:21 +0200 Subject: [PATCH] RAB: Integrate staging tests for the .values method (#4179) * Import relevant files from #3888 * Removing parts in resizableArrayBufferUtils.js and adding it in includes, while applying review changes from PRs for previously tested methods. * Removes unnecessary .from calls, as suggested in previous PR review comment: https://github.com/tc39/test262/pull/4138#discussion_r1676183221 --- .../resizable-buffer-grow-mid-iteration.js | 60 +++++++ .../resizable-buffer-shrink-mid-iteration.js | 52 ++++++ .../prototype/values/resizable-buffer.js | 156 ++++++++++++++++++ .../resizable-buffer-grow-mid-iteration.js | 60 +++++++ .../resizable-buffer-shrink-mid-iteration.js | 52 ++++++ .../prototype/values/resizable-buffer.js | 149 +++++++++++++++++ 6 files changed, 529 insertions(+) create mode 100644 test/built-ins/Array/prototype/values/resizable-buffer-grow-mid-iteration.js create mode 100644 test/built-ins/Array/prototype/values/resizable-buffer-shrink-mid-iteration.js create mode 100644 test/built-ins/Array/prototype/values/resizable-buffer.js create mode 100644 test/built-ins/TypedArray/prototype/values/resizable-buffer-grow-mid-iteration.js create mode 100644 test/built-ins/TypedArray/prototype/values/resizable-buffer-shrink-mid-iteration.js create mode 100644 test/built-ins/TypedArray/prototype/values/resizable-buffer.js diff --git a/test/built-ins/Array/prototype/values/resizable-buffer-grow-mid-iteration.js b/test/built-ins/Array/prototype/values/resizable-buffer-grow-mid-iteration.js new file mode 100644 index 0000000000..0d1965f983 --- /dev/null +++ b/test/built-ins/Array/prototype/values/resizable-buffer-grow-mid-iteration.js @@ -0,0 +1,60 @@ +// 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.values +description: > + Array.p.values behaves correctly on TypedArrays backed by resizable buffers and + resized mid-iteration. +features: [resizable-arraybuffer] +includes: [compareArray.js, resizableArrayBufferUtils.js] +---*/ + +// 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); + const fixedLength = new ctor(rab, 0, 4); + // The fixed length array is not affected by resizing. + TestIterationAndResize(Array.prototype.values.call(fixedLength), [ + 0, + 2, + 4, + 6 + ], rab, 2, 6 * ctor.BYTES_PER_ELEMENT); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2); + // The fixed length array is not affected by resizing. + TestIterationAndResize(Array.prototype.values.call(fixedLengthWithOffset), [ + 4, + 6 + ], rab, 2, 6 * ctor.BYTES_PER_ELEMENT); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + TestIterationAndResize(Array.prototype.values.call(lengthTracking), [ + 0, + 2, + 4, + 6, + 0, + 0 + ], rab, 2, 6 * ctor.BYTES_PER_ELEMENT); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT); + TestIterationAndResize(Array.prototype.values.call(lengthTrackingWithOffset), [ + 4, + 6, + 0, + 0 + ], rab, 2, 6 * ctor.BYTES_PER_ELEMENT); +} diff --git a/test/built-ins/Array/prototype/values/resizable-buffer-shrink-mid-iteration.js b/test/built-ins/Array/prototype/values/resizable-buffer-shrink-mid-iteration.js new file mode 100644 index 0000000000..79fe5d4bf9 --- /dev/null +++ b/test/built-ins/Array/prototype/values/resizable-buffer-shrink-mid-iteration.js @@ -0,0 +1,52 @@ +// 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.values +description: > + Array.p.values behaves correctly on TypedArrays backed by resizable buffers + that are shrunk mid-iteration. +features: [resizable-arraybuffer] +includes: [compareArray.js, resizableArrayBufferUtils.js] +---*/ + +// 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); + const fixedLength = new ctor(rab, 0, 4); + + // The fixed length array goes out of bounds when the RAB is resized. + assert.throws(TypeError, () => { + TestIterationAndResize(Array.prototype.values.call(fixedLength), null, rab, 2, 3 * ctor.BYTES_PER_ELEMENT); + }); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2); + assert.throws(TypeError, () => { + TestIterationAndResize(Array.prototype.values.call(fixedLengthWithOffset), null, rab, 2, 3 * ctor.BYTES_PER_ELEMENT); + }); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + TestIterationAndResize(Array.prototype.values.call(lengthTracking), [ + 0, + 2, + 4 + ], rab, 2, 3 * ctor.BYTES_PER_ELEMENT); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT); + + // The fixed length array goes out of bounds when the RAB is resized. + TestIterationAndResize(Array.prototype.values.call(lengthTrackingWithOffset), [ + 4, + 6 + ], rab, 2, 3 * ctor.BYTES_PER_ELEMENT); +} diff --git a/test/built-ins/Array/prototype/values/resizable-buffer.js b/test/built-ins/Array/prototype/values/resizable-buffer.js new file mode 100644 index 0000000000..c4b7f77868 --- /dev/null +++ b/test/built-ins/Array/prototype/values/resizable-buffer.js @@ -0,0 +1,156 @@ +// 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.values +description: > + Array.p.values behaves correctly on TypedArrays backed by resizable buffers. +includes: [compareArray.js, resizableArrayBufferUtils.js] +features: [resizable-arraybuffer] +---*/ + +function IteratorToNumbers(iterator) { + const result = []; + for (let value of iterator) { + result.push(Number(value)); + } + return result; +} + +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 + + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(fixedLength)), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(fixedLengthWithOffset)), [ + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(lengthTracking)), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(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 + + // TypedArray.prototype.{entries, keys, values} throw right away when + // called. Array.prototype.{entries, keys, values} don't throw, but when + // we try to iterate the returned ArrayIterator, that throws. + Array.prototype.values.call(fixedLength); + Array.prototype.values.call(fixedLengthWithOffset); + + assert.throws(TypeError, () => { + Array.from(Array.prototype.values.call(fixedLength)); + }); + assert.throws(TypeError, () => { + Array.from(Array.prototype.values.call(fixedLengthWithOffset)); + }); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(lengthTracking)), [ + 0, + 2, + 4 + ]); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(lengthTrackingWithOffset)), [4]); + + // Shrink so that the TAs with offset go out of bounds. + rab.resize(1 * ctor.BYTES_PER_ELEMENT); + Array.prototype.values.call(fixedLength); + Array.prototype.values.call(fixedLengthWithOffset); + Array.prototype.values.call(lengthTrackingWithOffset); + + assert.throws(TypeError, () => { + Array.from(Array.prototype.values.call(fixedLength)); + }); + assert.throws(TypeError, () => { + Array.from(Array.prototype.values.call(fixedLengthWithOffset)); + }); + assert.throws(TypeError, () => { + Array.from(Array.prototype.values.call(lengthTrackingWithOffset)); + }); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(lengthTracking)), [0]); + + // Shrink to zero. + rab.resize(0); + Array.prototype.values.call(fixedLength); + Array.prototype.values.call(fixedLengthWithOffset); + Array.prototype.values.call(lengthTrackingWithOffset); + + assert.throws(TypeError, () => { + Array.from(Array.prototype.values.call(fixedLength)); + }); + assert.throws(TypeError, () => { + Array.from(Array.prototype.values.call(fixedLengthWithOffset)); + }); + assert.throws(TypeError, () => { + Array.from(Array.prototype.values.call(lengthTrackingWithOffset)); + }); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(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(IteratorToNumbers(Array.prototype.values.call(fixedLength)), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(fixedLengthWithOffset)), [ + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(lengthTracking)), [ + 0, + 2, + 4, + 6, + 8, + 10 + ]); + assert.compareArray(IteratorToNumbers(Array.prototype.values.call(lengthTrackingWithOffset)), [ + 4, + 6, + 8, + 10 + ]); +} diff --git a/test/built-ins/TypedArray/prototype/values/resizable-buffer-grow-mid-iteration.js b/test/built-ins/TypedArray/prototype/values/resizable-buffer-grow-mid-iteration.js new file mode 100644 index 0000000000..3ea3141139 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/values/resizable-buffer-grow-mid-iteration.js @@ -0,0 +1,60 @@ +// 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.values +description: > + TypedArray.p.values behaves correctly on TypedArrays backed by resizable + buffers and resized mid-iteration. +features: [resizable-arraybuffer] +includes: [compareArray.js, resizableArrayBufferUtils.js] +---*/ + +// 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); + const fixedLength = new ctor(rab, 0, 4); + // The fixed length array is not affected by resizing. + TestIterationAndResize(fixedLength.values(), [ + 0, + 2, + 4, + 6 + ], rab, 2, 6 * ctor.BYTES_PER_ELEMENT); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2); + // The fixed length array is not affected by resizing. + TestIterationAndResize(fixedLengthWithOffset.values(), [ + 4, + 6 + ], rab, 2, 6 * ctor.BYTES_PER_ELEMENT); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + TestIterationAndResize(lengthTracking.values(), [ + 0, + 2, + 4, + 6, + 0, + 0 + ], rab, 2, 6 * ctor.BYTES_PER_ELEMENT); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT); + TestIterationAndResize(lengthTrackingWithOffset.values(), [ + 4, + 6, + 0, + 0 + ], rab, 2, 6 * ctor.BYTES_PER_ELEMENT); +} diff --git a/test/built-ins/TypedArray/prototype/values/resizable-buffer-shrink-mid-iteration.js b/test/built-ins/TypedArray/prototype/values/resizable-buffer-shrink-mid-iteration.js new file mode 100644 index 0000000000..4547734d1a --- /dev/null +++ b/test/built-ins/TypedArray/prototype/values/resizable-buffer-shrink-mid-iteration.js @@ -0,0 +1,52 @@ +// 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.values +description: > + TypedArray.p.values behaves correctly on TypedArrays backed by resizable + buffers that are shrunk mid-iteration. +features: [resizable-arraybuffer] +includes: [compareArray.js, resizableArrayBufferUtils.js] +---*/ + +// 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); + const fixedLength = new ctor(rab, 0, 4); + + // The fixed length array goes out of bounds when the RAB is resized. + assert.throws(TypeError, () => { + TestIterationAndResize(fixedLength.values(), null, rab, 2, 3 * ctor.BYTES_PER_ELEMENT); + }); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2); + assert.throws(TypeError, () => { + TestIterationAndResize(fixedLengthWithOffset.values(), null, rab, 2, 3 * ctor.BYTES_PER_ELEMENT); + }); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const lengthTracking = new ctor(rab, 0); + TestIterationAndResize(lengthTracking.values(), [ + 0, + 2, + 4 + ], rab, 2, 3 * ctor.BYTES_PER_ELEMENT); +} +for (let ctor of ctors) { + const rab = CreateRabForTest(ctor); + const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT); + + // The fixed length array goes out of bounds when the RAB is resized. + TestIterationAndResize(lengthTrackingWithOffset.values(), [ + 4, + 6 + ], rab, 2, 3 * ctor.BYTES_PER_ELEMENT); +} diff --git a/test/built-ins/TypedArray/prototype/values/resizable-buffer.js b/test/built-ins/TypedArray/prototype/values/resizable-buffer.js new file mode 100644 index 0000000000..e7eb2347ac --- /dev/null +++ b/test/built-ins/TypedArray/prototype/values/resizable-buffer.js @@ -0,0 +1,149 @@ +// 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.values +description: > + TypedArray.p.values behaves correctly on TypedArrays backed by resizable + buffers. +includes: [compareArray.js, resizableArrayBufferUtils.js] +features: [resizable-arraybuffer] +---*/ + +function IteratorToNumbers(iterator) { + let result = []; + for (let value of iterator) { + result.push(Number(value)); + } + return result; +} + +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 + + assert.compareArray(IteratorToNumbers(fixedLength.values()), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(fixedLengthWithOffset.values()), [ + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(lengthTracking.values()), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(lengthTrackingWithOffset.values()), [ + 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 + + // TypedArray.prototype.{entries, keys, values} throw right away when + // called. Array.prototype.{entries, keys, values} don't throw, but when + // we try to iterate the returned ArrayIterator, that throws. + assert.throws(TypeError, () => { + fixedLength.values(); + }); + assert.throws(TypeError, () => { + fixedLengthWithOffset.values(); + }); + + assert.compareArray(IteratorToNumbers(lengthTracking.values()), [ + 0, + 2, + 4 + ]); + assert.compareArray(IteratorToNumbers(lengthTrackingWithOffset.values()), [4]); + + // Shrink so that the TAs with offset go out of bounds. + rab.resize(1 * ctor.BYTES_PER_ELEMENT); + assert.throws(TypeError, () => { + fixedLength.values(); + }); + assert.throws(TypeError, () => { + fixedLengthWithOffset.values(); + }); + assert.throws(TypeError, () => { + lengthTrackingWithOffset.values(); + }); + + assert.compareArray(IteratorToNumbers(lengthTracking.values()), [0]); + + // Shrink to zero. + rab.resize(0); + assert.throws(TypeError, () => { + fixedLength.values(); + }); + assert.throws(TypeError, () => { + fixedLengthWithOffset.values(); + }); + assert.throws(TypeError, () => { + lengthTrackingWithOffset.values(); + }); + + assert.compareArray(IteratorToNumbers(lengthTracking.values()), []); + + // 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(IteratorToNumbers(fixedLength.values()), [ + 0, + 2, + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(fixedLengthWithOffset.values()), [ + 4, + 6 + ]); + assert.compareArray(IteratorToNumbers(lengthTracking.values()), [ + 0, + 2, + 4, + 6, + 8, + 10 + ]); + assert.compareArray(IteratorToNumbers(lengthTrackingWithOffset.values()), [ + 4, + 6, + 8, + 10 + ]); +}