From d3b0af36f3524bf56f3b3825103ae98587689642 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 25 May 2021 08:30:39 -0700 Subject: [PATCH 1/2] fix(PointerArray): throw error if array size > 4294967295 --- utils/typed-arrays.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/typed-arrays.js b/utils/typed-arrays.js index 474a2cb7..f70bcb7c 100644 --- a/utils/typed-arrays.js +++ b/utils/typed-arrays.js @@ -35,7 +35,7 @@ exports.getPointerArray = function(size) { if (maxIndex <= MAX_32BIT_INTEGER) return Uint32Array; - return Float64Array; + throw new Error('mnemonist: Pointer Array of size > 4294967295 is not supported.'); }; exports.getSignedPointerArray = function(size) { From 76daa2b00f76a44f5be98822e7a499953e72531f Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 25 May 2021 08:44:46 -0700 Subject: [PATCH 2/2] test(TypedArrays): add tests for getPointerArray --- test/_utils.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/_utils.js b/test/_utils.js index ea26716e..7e93043b 100644 --- a/test/_utils.js +++ b/test/_utils.js @@ -13,6 +13,38 @@ describe('utils', function() { describe('typed-arrays', function() { + describe('#.getPointerArray', function() { + var validatePointerArrayConstructor = function (min, max, expectedCtor) { + it(`returns ${expectedCtor} for ${min}`, () => { + assert.strictEqual(typed.getPointerArray(min), expectedCtor); + }); + it(`returns ${expectedCtor} for ${(max - min) / 2}`, () => { + assert.strictEqual(typed.getPointerArray((max - min) / 2), expectedCtor); + }); + it(`returns ${expectedCtor} for ${max}`, () => { + assert.strictEqual(typed.getPointerArray(max), expectedCtor); + }); + }; + + describe('returns Uint8Array for capacity <= Math.pow(2, 8)', function() { + validatePointerArrayConstructor(0, Math.pow(2, 8), Uint8Array); + }); + + describe('returns Uint16Array for Math.pow(2, 8) < capacity <= Math.pow(2, 16)', function() { + validatePointerArrayConstructor(Math.pow(2, 8) + 1, Math.pow(2, 16), Uint16Array); + }); + + describe('returns Uint32Array for Math.pow(2, 16) < capacity <= Math.pow(2, 32)', function() { + validatePointerArrayConstructor(Math.pow(2, 16) + 1, Math.pow(2, 32), Uint32Array); + }); + + describe('throws error for capacity > Math.pow(2, 32)', function() { + assert.throws(function() { + typed.getPointerArray(Math.pow(2, 32) + 1); + }, /Pointer Array of size > 4294967295 is not supported/); + }); + }); + describe('#.getMinimalRepresentation', function() { it('should return the correct type.', function() {