From: Jérôme Benoit Date: Wed, 15 Feb 2023 14:06:51 +0000 (+0100) Subject: fix(simulator): sanity checks to circular array inputs X-Git-Tag: v1.1.95~46 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4cf1f68c16b8c479208e31b4873e405192044f2e;p=e-mobility-charging-stations-simulator.git fix(simulator): sanity checks to circular array inputs Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/CircularArray.ts b/src/utils/CircularArray.ts index ad25bf85..439f0827 100644 --- a/src/utils/CircularArray.ts +++ b/src/utils/CircularArray.ts @@ -80,8 +80,11 @@ export class CircularArray extends Array { } private checkSize(size: number) { + if (!Number.isSafeInteger(size)) { + throw new TypeError(`Invalid circular array size: ${size} is not a safe integer`); + } if (size < 0) { - throw new RangeError('Invalid circular array size'); + throw new RangeError(`Invalid circular array size: ${size} < 0`); } } } diff --git a/test/utils/CircularArrayTest.ts b/test/utils/CircularArrayTest.ts index 10174520..0a2609c0 100644 --- a/test/utils/CircularArrayTest.ts +++ b/test/utils/CircularArrayTest.ts @@ -27,8 +27,18 @@ describe('Circular array test suite', () => { expect(circularArray.length).toBe(4); }); - it("Verify that circular array size can't be negative at instance creation", () => { - expect(() => new CircularArray(-1)).toThrowError(new RangeError('Invalid circular array size')); + it('Verify that circular array size is valid at instance creation', () => { + expect(() => new CircularArray(0.25)).toThrowError( + new TypeError('Invalid circular array size: 0.25 is not a safe integer') + ); + expect(() => new CircularArray(-1)).toThrowError( + new RangeError('Invalid circular array size: -1 < 0') + ); + expect(() => new CircularArray(Number.MAX_SAFE_INTEGER + 1)).toThrowError( + new TypeError( + `Invalid circular array size: ${Number.MAX_SAFE_INTEGER + 1} is not a safe integer` + ) + ); }); it('Verify that circular array empty works as intended', () => { @@ -102,8 +112,16 @@ describe('Circular array test suite', () => { }); it('Verify that circular array resize works as intended', () => { + expect(() => new CircularArray().resize(0.25)).toThrowError( + new TypeError('Invalid circular array size: 0.25 is not a safe integer') + ); expect(() => new CircularArray().resize(-1)).toThrowError( - new RangeError('Invalid circular array size') + new RangeError('Invalid circular array size: -1 < 0') + ); + expect(() => new CircularArray().resize(Number.MAX_SAFE_INTEGER + 1)).toThrowError( + new TypeError( + `Invalid circular array size: ${Number.MAX_SAFE_INTEGER + 1} is not a safe integer` + ) ); let circularArray = new CircularArray(5, 1, 2, 3, 4, 5); circularArray.resize(0);