Skip to content

Commit

Permalink
Stupid Quoc
Browse files Browse the repository at this point in the history
  • Loading branch information
vhpx committed Apr 19, 2024
1 parent e4a2ee1 commit 1b33c44
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions __tests__/new-test.test.tsx
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);
});
});

0 comments on commit 1b33c44

Please sign in to comment.