From: Jérôme Benoit Date: Tue, 27 Jun 2023 18:47:53 +0000 (+0200) Subject: fix: fix fibonacci implementation X-Git-Tag: v2.6.6~18 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=024daf59a8f93e67b7a307d1b5690ba564c80bab;p=poolifier.git fix: fix fibonacci implementation Signed-off-by: Jérôme Benoit --- diff --git a/benchmarks/benchmarks-utils.mjs b/benchmarks/benchmarks-utils.mjs index 45e8cc0e..9435b0d3 100644 --- a/benchmarks/benchmarks-utils.mjs +++ b/benchmarks/benchmarks-utils.mjs @@ -8,7 +8,7 @@ import { } from '../lib/index.mjs' import { PoolTypes, WorkerFunctions, WorkerTypes } from './benchmarks-types.mjs' -async function runTest (pool, { taskExecutions, workerData }) { +export async function runTest (pool, { taskExecutions, workerData }) { return new Promise((resolve, reject) => { let executions = 0 for (let i = 1; i <= taskExecutions; i++) { @@ -29,7 +29,7 @@ async function runTest (pool, { taskExecutions, workerData }) { }) } -function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { +export function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { if (max < min || max < 0 || min < 0) { throw new RangeError('Invalid interval') } @@ -55,8 +55,8 @@ function jsonIntegerSerialization (n) { * @param {number} n - The number of fibonacci numbers to generate. * @returns {number} - The nth fibonacci number. */ -function fibonacci (n) { - if (n <= 1) return 1 +export function fibonacci (n) { + if (n <= 1) return n return fibonacci(n - 1) + fibonacci(n - 2) } @@ -65,14 +65,14 @@ function fibonacci (n) { * @param {number} n - The number to calculate the factorial of. * @returns {number} - The factorial of n. */ -function factorial (n) { +export function factorial (n) { if (n === 0) { return 1 } return factorial(n - 1) * n } -function readWriteFiles ( +export function readWriteFiles ( n, baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt( 281474976710655 @@ -93,7 +93,7 @@ function readWriteFiles ( fs.rmSync(baseDirectory, { recursive: true }) } -function executeWorkerFunction (data) { +export function executeWorkerFunction (data) { switch (data.function) { case WorkerFunctions.jsonIntegerSerialization: return jsonIntegerSerialization(data.taskSize || 1000) @@ -108,7 +108,7 @@ function executeWorkerFunction (data) { } } -function buildPool (workerType, poolType, poolSize, poolOptions) { +export function buildPool (workerType, poolType, poolSize, poolOptions) { switch (poolType) { case PoolTypes.fixed: switch (workerType) { @@ -146,12 +146,3 @@ function buildPool (workerType, poolType, poolSize, poolOptions) { break } } - -export { - WorkerFunctions, - buildPool, - executeWorkerFunction, - generateRandomInteger, - readWriteFiles, - runTest -} diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index c1e3a323..b5a53487 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -779,6 +779,6 @@ describe('Abstract pool test suite', () => { const result2 = await pool.execute(data, 'factorial') expect(result2).toBe(3628800) const result3 = await pool.execute(data, 'fibonacci') - expect(result3).toBe(89) + expect(result3).toBe(55) }) }) diff --git a/tests/pools/cluster/dynamic.test.js b/tests/pools/cluster/dynamic.test.js index f67ee01f..ed634083 100644 --- a/tests/pools/cluster/dynamic.test.js +++ b/tests/pools/cluster/dynamic.test.js @@ -19,7 +19,7 @@ describe('Dynamic cluster pool test suite', () => { let result = await pool.execute({ function: WorkerFunctions.fibonacci }) - expect(result).toBe(121393) + expect(result).toBe(75025) result = await pool.execute({ function: WorkerFunctions.factorial }) diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index aa90c8ab..8f3fad8b 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -65,7 +65,7 @@ describe('Fixed cluster pool test suite', () => { let result = await pool.execute({ function: WorkerFunctions.fibonacci }) - expect(result).toBe(121393) + expect(result).toBe(75025) result = await pool.execute({ function: WorkerFunctions.factorial }) diff --git a/tests/pools/thread/dynamic.test.js b/tests/pools/thread/dynamic.test.js index d41fa828..11c99593 100644 --- a/tests/pools/thread/dynamic.test.js +++ b/tests/pools/thread/dynamic.test.js @@ -19,7 +19,7 @@ describe('Dynamic thread pool test suite', () => { let result = await pool.execute({ function: WorkerFunctions.fibonacci }) - expect(result).toBe(121393) + expect(result).toBe(75025) result = await pool.execute({ function: WorkerFunctions.factorial }) diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 8501ff7e..017cd7e3 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -65,7 +65,7 @@ describe('Fixed thread pool test suite', () => { let result = await pool.execute({ function: WorkerFunctions.fibonacci }) - expect(result).toBe(121393) + expect(result).toBe(75025) result = await pool.execute({ function: WorkerFunctions.factorial }) diff --git a/tests/test-utils.js b/tests/test-utils.js index 24b01e44..ceaff7be 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -81,7 +81,7 @@ class TestUtils { * @returns {number} - The nth fibonacci number. */ static fibonacci (n) { - if (n <= 1) return 1 + if (n <= 1) return n return TestUtils.fibonacci(n - 1) + TestUtils.fibonacci(n - 2) }