-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { expect, vi, describe, it, beforeEach } from 'vitest'; | ||
|
||
function isNonNegativeInteger(n: number): n is number { | ||
return typeof n === 'number' && Number.isInteger(n) && n >= 0; | ||
} | ||
|
||
describe('Fibonacci Sequence Generator', () => { | ||
let fibonacci: any; | ||
|
||
beforeEach(() => { | ||
fibonacci = vi.fn((n: number) => { | ||
if (!isNonNegativeInteger(n)) { | ||
throw new Error('Input must be a non-negative integer'); | ||
} | ||
if (n <= 0) { | ||
return []; | ||
} else if (n === 1) { | ||
return [0]; | ||
} else { | ||
const fibSequence = [0, 1]; | ||
for (let i = 2; i < n; i++) { | ||
const nextNumber = fibSequence[i - 1] + fibSequence[i - 2]; | ||
fibSequence.push(nextNumber); | ||
} | ||
return fibSequence; | ||
} | ||
}); | ||
}); | ||
|
||
it('should return an empty list for n = 0', () => { | ||
const result = fibonacci(0); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should return [0] for n = 1', () => { | ||
const result = fibonacci(1); | ||
expect(result).toEqual([0]); | ||
}); | ||
|
||
it('should return the correct Fibonacci sequence for n = 5', () => { | ||
const result = fibonacci(5); | ||
expect(result).toEqual([0, 1, 1, 2, 3]); | ||
}); | ||
|
||
it('should return the correct Fibonacci sequence for n = 10', () => { | ||
const result = fibonacci(10); | ||
expect(result).toEqual([0, 1, 1, 2, 3, 5, 8, 13, 21, 34]); | ||
}); | ||
|
||
it('should throw an error for negative input', () => { | ||
expect(() => fibonacci(-1)).toThrow(); | ||
}); | ||
|
||
it('should throw an error for non-integer input', () => { | ||
expect(() => fibonacci(1.5)).toThrow(); | ||
}); | ||
|
||
it('should throw an error for non-numeric input', () => { | ||
expect(() => fibonacci('abc')).toThrow(); | ||
}); | ||
|
||
it('should throw an error for null input', () => { | ||
expect(() => fibonacci(null)).toThrow(); | ||
}); | ||
|
||
it('should handle large input values', () => { | ||
const result = fibonacci(100); | ||
expect(result.length).toBe(100); | ||
}); | ||
}); |