From: Jérôme Benoit Date: Tue, 28 May 2024 18:14:09 +0000 (+0200) Subject: refactor: refine priority queue bucket size validation X-Git-Tag: v4.0.13~6 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=579aa5bd0da2971c57a8f26be4a425e78f5ad040;p=poolifier.git refactor: refine priority queue bucket size validation Signed-off-by: Jérôme Benoit --- diff --git a/src/priority-queue.ts b/src/priority-queue.ts index ed4d63f5..142fa5e8 100644 --- a/src/priority-queue.ts +++ b/src/priority-queue.ts @@ -37,10 +37,12 @@ export class PriorityQueue { */ public constructor (bucketSize: number = defaultBucketSize) { if (!Number.isSafeInteger(bucketSize)) { - throw new TypeError('bucketSize must be an integer') + throw new TypeError( + `Invalid bucket size: '${bucketSize}' is not an integer` + ) } - if (bucketSize < 1) { - throw new RangeError('bucketSize must be greater than or equal to 1') + if (bucketSize < 0) { + throw new RangeError(`Invalid bucket size: ${bucketSize} < 0`) } this.bucketSize = bucketSize this.clear() diff --git a/tests/priority-queue.test.mjs b/tests/priority-queue.test.mjs index bcd3cf0a..ddc8c09d 100644 --- a/tests/priority-queue.test.mjs +++ b/tests/priority-queue.test.mjs @@ -6,13 +6,10 @@ import { defaultBucketSize, PriorityQueue } from '../lib/priority-queue.cjs' describe('Priority queue test suite', () => { it('Verify constructor() behavior', () => { expect(() => new PriorityQueue('')).toThrow( - new TypeError('bucketSize must be an integer') + new TypeError("Invalid bucket size: '' is not an integer") ) expect(() => new PriorityQueue(-1)).toThrow( - new RangeError('bucketSize must be greater than or equal to 1') - ) - expect(() => new PriorityQueue(0)).toThrow( - new RangeError('bucketSize must be greater than or equal to 1') + new RangeError('Invalid bucket size: -1 < 0') ) let priorityQueue = new PriorityQueue() expect(priorityQueue.bucketSize).toBe(defaultBucketSize)