X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Ftest-utils.cjs;h=4448eaf6a1b79539bc5b717c6f22704092d3102c;hb=39c93c16d109194824a53f2b9d8167353d13515c;hp=6dba586641d3b1906caee80c96f38e364b3e9497;hpb=d35e571704515a8b729d3455e4784054f07c368f;p=poolifier.git diff --git a/tests/test-utils.cjs b/tests/test-utils.cjs index 6dba5866..4448eaf6 100644 --- a/tests/test-utils.cjs +++ b/tests/test-utils.cjs @@ -55,18 +55,6 @@ const sleepTaskFunction = async ( }) } -const generateRandomInteger = (max = Number.MAX_SAFE_INTEGER, min = 0) => { - if (max < min || max < 0 || min < 0) { - throw new RangeError('Invalid interval') - } - max = Math.floor(max) - if (min != null && min !== 0) { - min = Math.ceil(min) - return Math.floor(Math.random() * (max - min + 1)) + min - } - return Math.floor(Math.random() * (max + 1)) -} - const jsonIntegerSerialization = n => { for (let i = 0; i < n; i++) { const o = { @@ -78,25 +66,34 @@ const jsonIntegerSerialization = n => { } /** - * Intentionally inefficient implementation. * @param {number} n - The number of fibonacci numbers to generate. * @returns {number} - The nth fibonacci number. */ const fibonacci = n => { - if (n <= 1) return n - return fibonacci(n - 1) + fibonacci(n - 2) + let current = 1 + let previous = 0 + while (--n) { + const tmp = current + current += previous + previous = tmp + } + return current } /** - * Intentionally inefficient implementation. * @param {number} n - The number to calculate the factorial of. * @returns {number} - The factorial of n. */ const factorial = n => { - if (n === 0) { + if (n === 0 || n === 1) { return 1 + } else { + let factorial = 1 + for (let i = 1; i <= n; i++) { + factorial *= i + } + return factorial } - return factorial(n - 1) * n } const executeTaskFunction = data => { @@ -104,7 +101,7 @@ const executeTaskFunction = data => { case TaskFunctions.jsonIntegerSerialization: return jsonIntegerSerialization(data.n || 100) case TaskFunctions.fibonacci: - return fibonacci(data.n || 25) + return fibonacci(data.n || 100) case TaskFunctions.factorial: return factorial(data.n || 100) default: @@ -116,7 +113,6 @@ module.exports = { executeTaskFunction, factorial, fibonacci, - generateRandomInteger, jsonIntegerSerialization, sleep, sleepTaskFunction,