From 761e7744501fa4b67f5905045c0ad5d52e34b6b9 Mon Sep 17 00:00:00 2001 From: "Ioanna M. Dimitriou H" Date: Tue, 26 Mar 2024 19:29:05 +0100 Subject: [PATCH] Collection of helper constants and functions for testing resizable array buffers. These are the parts of the code in RAB staging tests that are heavily repeated. In order to somewhat compact the migration of RAB staging tests (see PR #3888). --- harness/resizableArrayBufferUtils.js | 125 +++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 harness/resizableArrayBufferUtils.js diff --git a/harness/resizableArrayBufferUtils.js b/harness/resizableArrayBufferUtils.js new file mode 100644 index 00000000000..b136d9e48e4 --- /dev/null +++ b/harness/resizableArrayBufferUtils.js @@ -0,0 +1,125 @@ +// Copyright 2023 the V8 project authors. All rights reserved. +// Copyright 2024 Igalia S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: | + Collection of helper constants and functions for testing resizable array buffers. +defines: + - floatCtors + - ctors + - MyBigInt64Array + - CreateResizableArrayBuffer + - WriteToTypedArray + - Convert + - ToNumbers + - CreateRabForTest + - CollectValuesAndResize + - TestIterationAndResize +---*/ + +class MyUint8Array extends Uint8Array { +} + +class MyFloat32Array extends Float32Array { +} + +class MyBigInt64Array extends BigInt64Array { +} + +const builtinCtors = [ + Uint8Array, + Int8Array, + Uint16Array, + Int16Array, + Uint32Array, + Int32Array, + Float32Array, + Float64Array, + Uint8ClampedArray, + BigUint64Array, + BigInt64Array +]; + +const floatCtors = [ + Float32Array, + Float64Array, + MyFloat32Array +]; + +const ctors = [ + ...builtinCtors, + MyUint8Array, + MyFloat32Array, + MyBigInt64Array +]; + +function CreateResizableArrayBuffer(byteLength, maxByteLength) { + return new ArrayBuffer(byteLength, { maxByteLength: maxByteLength }); +} + +function WriteToTypedArray(array, index, value) { + if (array instanceof BigInt64Array || array instanceof BigUint64Array) { + array[index] = BigInt(value); + } else { + array[index] = value; + } +} + +function Convert(item) { + if (typeof item == 'bigint') { + return Number(item); + } + return item; +} + +function ToNumbers(array) { + let result = []; + for (let item of array) { + result.push(Convert(item)); + } + return result; +} + +function CreateRabForTest(ctor) { + const rab = CreateResizableArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, 8 * 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); + } + return rab; +} + +function CollectValuesAndResize(n) { + if (typeof n == 'bigint') { + values.push(Number(n)); + } else { + values.push(n); + } + if (values.length == resizeAfter) { + rab.resize(resizeTo); + } + return true; +} + +function TestIterationAndResize(ta, expected, rab, resize_after, new_byte_length) { + let values = []; + let resized = false; + for (const value of ta) { + if (value instanceof Array) { + values.push([ + value[0], + Number(value[1]) + ]); + } else { + values.push(Number(value)); + } + if (!resized && values.length == resize_after) { + rab.resize(new_byte_length); + resized = true; + } + } + assert.compareArray(values.flat(), expected.flat()); + assert(resized); +}