Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion __tests__/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Many functions are not tested, and edge cases are missing
*/

import { add, subtract, multiply, divide, factorial, isPrime, average, findMax } from '../src/utils/math';
import { add, subtract, multiply, divide, factorial, isPrime, average, findMax, fibonacci } from '../src/utils/math';

describe('Math Utilities', () => {
// Basic tests for add function
Expand Down Expand Up @@ -60,4 +60,66 @@ describe('Math Utilities', () => {

// No tests for findMax function!
// This function also has issues with empty arrays

describe('fibonacci', () => {
it('should return 0 for fibonacci(0)', () => {
expect(fibonacci(0)).toBe(0);
});

it('should return 1 for fibonacci(1)', () => {
expect(fibonacci(1)).toBe(1);
});

it('should return 1 for fibonacci(2)', () => {
expect(fibonacci(2)).toBe(1);
});

it('should return 2 for fibonacci(3)', () => {
expect(fibonacci(3)).toBe(2);
});

it('should return 3 for fibonacci(4)', () => {
expect(fibonacci(4)).toBe(3);
});

it('should return 5 for fibonacci(5)', () => {
expect(fibonacci(5)).toBe(5);
});

it('should return 8 for fibonacci(6)', () => {
expect(fibonacci(6)).toBe(8);
});

it('should return 55 for fibonacci(10)', () => {
expect(fibonacci(10)).toBe(55);
});

it('should return 6765 for fibonacci(20)', () => {
expect(fibonacci(20)).toBe(6765);
});

it('should throw error for negative input', () => {
expect(() => fibonacci(-1)).toThrow('Fibonacci is not defined for negative numbers');
});

it('should throw error for negative input -5', () => {
expect(() => fibonacci(-5)).toThrow('Fibonacci is not defined for negative numbers');
});

it('should throw error for decimal input', () => {
expect(() => fibonacci(3.5)).toThrow('Fibonacci is only defined for integer values');
});

it('should throw error for non-integer input', () => {
expect(() => fibonacci(2.1)).toThrow('Fibonacci is only defined for integer values');
});

it('should handle larger Fibonacci numbers', () => {
expect(fibonacci(30)).toBe(832040);
});

it('should handle edge case fibonacci(15)', () => {
expect(fibonacci(15)).toBe(610);
});
});
});
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ module.exports = {
'^.+\\.ts$': ['ts-jest', {
tsconfig: {
// Override some tsconfig settings for tests
strict: false // Be less strict in tests
strict: false, // Be less strict in tests
noUnusedLocals: false,
noUnusedParameters: false
}
}]
}
Expand Down
39 changes: 39 additions & 0 deletions src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,43 @@ export function average(numbers: number[]): number {
export function findMax(numbers: number[]): number {
// BUG: What happens with empty array?
return Math.max(...numbers);
}

/**
* Calculate the nth Fibonacci number
* Uses iterative approach for better performance than recursion
* @param n The position in the Fibonacci sequence (0-indexed)
* @returns The nth Fibonacci number
* @throws {Error} If n is negative
* @example
* fibonacci(0) // returns 0
* fibonacci(1) // returns 1
* fibonacci(10) // returns 55
*/
export function fibonacci(n: number): number {
// Error handling for negative inputs
if (n < 0) {
throw new Error('Fibonacci is not defined for negative numbers');
}

// Error handling for non-integer inputs
if (!Number.isInteger(n)) {
throw new Error('Fibonacci is only defined for integer values');
}

// Base cases
if (n === 0) return 0;
if (n === 1) return 1;

// Iterative calculation for better performance
let prev = 0;
let current = 1;

for (let i = 2; i <= n; i++) {
const next = prev + current;
prev = current;
current = next;
}

return current;
}
Loading