86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
```python
|
|
import pytest
|
|
from src.utils.math_functions import fibonacci
|
|
|
|
|
|
class TestFibonacci:
|
|
"""Test cases for the fibonacci function."""
|
|
|
|
def test_fibonacci_n_0(self):
|
|
"""Test fibonacci with n=0 returns [0]."""
|
|
result = fibonacci(0)
|
|
assert result == [0]
|
|
|
|
def test_fibonacci_n_1(self):
|
|
"""Test fibonacci with n=1 returns [0, 1]."""
|
|
result = fibonacci(1)
|
|
assert result == [0, 1]
|
|
|
|
def test_fibonacci_n_2(self):
|
|
"""Test fibonacci with n=2 returns [0, 1, 1]."""
|
|
result = fibonacci(2)
|
|
assert result == [0, 1, 1]
|
|
|
|
def test_fibonacci_n_5(self):
|
|
"""Test fibonacci with n=5 returns correct sequence."""
|
|
result = fibonacci(5)
|
|
assert result == [0, 1, 1, 2, 3, 5]
|
|
|
|
def test_fibonacci_n_10(self):
|
|
"""Test fibonacci with n=10 returns correct sequence."""
|
|
result = fibonacci(10)
|
|
expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
|
assert result == expected
|
|
|
|
def test_fibonacci_large_n(self):
|
|
"""Test fibonacci with a larger n value."""
|
|
result = fibonacci(15)
|
|
expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
|
|
assert result == expected
|
|
|
|
def test_fibonacci_negative_input(self):
|
|
"""Test fibonacci with negative input raises ValueError."""
|
|
with pytest.raises(ValueError):
|
|
fibonacci(-1)
|
|
|
|
with pytest.raises(ValueError):
|
|
fibonacci(-10)
|
|
|
|
def test_fibonacci_non_integer_input(self):
|
|
"""Test fibonacci with non-integer input raises TypeError."""
|
|
with pytest.raises(TypeError):
|
|
fibonacci("5")
|
|
|
|
with pytest.raises(TypeError):
|
|
fibonacci(5.5)
|
|
|
|
with pytest.raises(TypeError):
|
|
fibonacci([5])
|
|
|
|
with pytest.raises(TypeError):
|
|
fibonacci(None)
|
|
|
|
def test_fibonacci_return_type(self):
|
|
"""Test that fibonacci returns a list."""
|
|
result = fibonacci(5)
|
|
assert isinstance(result, list)
|
|
|
|
def test_fibonacci_sequence_length(self):
|
|
"""Test that fibonacci sequence has correct length (n+1)."""
|
|
for n in [0, 1, 5, 10]:
|
|
result = fibonacci(n)
|
|
assert len(result) == n + 1
|
|
|
|
def test_fibonacci_sequence_property(self):
|
|
"""Test that each element satisfies the Fibonacci recurrence relation."""
|
|
n = 10
|
|
result = fibonacci(n)
|
|
|
|
# Check first two elements
|
|
assert result[0] == 0
|
|
assert result[1] == 1
|
|
|
|
# Check recurrence relation for remaining elements
|
|
for i in range(2, n + 1):
|
|
assert result[i] == result[i - 1] + result[i - 2]
|
|
``` |