From: Jérôme Benoit Date: Mon, 24 Oct 2022 12:03:18 +0000 (+0200) Subject: Fix random integer generator X-Git-Tag: v2.3.8~114 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4af5c11ae8d0f0ac6dfe1e597fc231f9724a665f;p=poolifier.git Fix random integer generator Signed-off-by: Jérôme Benoit --- diff --git a/benchmarks/benchmarks-utils.js b/benchmarks/benchmarks-utils.js index 8bd137a3..0a964d7b 100644 --- a/benchmarks/benchmarks-utils.js +++ b/benchmarks/benchmarks-utils.js @@ -31,8 +31,14 @@ function jsonIntegerSerialization (n) { } function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { + if (max < 0) { + throw new RangeError('Invalid interval') + } max = Math.floor(max) - if (min != null || min !== 0) { + if (min != null && min !== 0) { + if (max < min || min < 0) { + throw new RangeError('Invalid interval') + } min = Math.ceil(min) return Math.floor(Math.random() * (max - min + 1)) + min } diff --git a/tests/test-utils.js b/tests/test-utils.js index 76db3557..f4cb38b2 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -37,8 +37,14 @@ class TestUtils { } static generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { + if (max < 0) { + throw new RangeError('Invalid interval') + } max = Math.floor(max) - if (min != null || min !== 0) { + if (min != null && min !== 0) { + if (max < min || min < 0) { + throw new RangeError('Invalid interval') + } min = Math.ceil(min) return Math.floor(Math.random() * (max - min + 1)) + min }