80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
```python
|
|
import pytest
|
|
from your_module import fibonacci # Replace 'your_module' with actual module name
|
|
|
|
|
|
class TestFibonacci:
|
|
"""Test suite for fibonacci function"""
|
|
|
|
def test_base_cases(self):
|
|
"""Test base cases: n=0, n=1, n=2"""
|
|
assert fibonacci(0) == []
|
|
assert fibonacci(1) == [0]
|
|
assert fibonacci(2) == [0, 1]
|
|
|
|
def test_small_positive_numbers(self):
|
|
"""Test small positive values of n"""
|
|
assert fibonacci(3) == [0, 1, 1]
|
|
assert fibonacci(4) == [0, 1, 1, 2]
|
|
assert fibonacci(5) == [0, 1, 1, 2, 3]
|
|
assert fibonacci(6) == [0, 1, 1, 2, 3, 5]
|
|
assert fibonacci(7) == [0, 1, 1, 2, 3, 5, 8]
|
|
|
|
def test_larger_positive_numbers(self):
|
|
"""Test larger positive values of n"""
|
|
assert fibonacci(10) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
|
|
assert len(fibonacci(15)) == 15
|
|
assert fibonacci(15)[-1] == 377 # 15th Fibonacci number
|
|
|
|
def test_negative_input(self):
|
|
"""Test that negative input raises ValueError"""
|
|
with pytest.raises(ValueError, match="n must be a non-negative integer"):
|
|
fibonacci(-1)
|
|
with pytest.raises(ValueError, match="n must be a non-negative integer"):
|
|
fibonacci(-10)
|
|
|
|
def test_non_integer_input(self):
|
|
"""Test that non-integer input raises TypeError"""
|
|
with pytest.raises(TypeError, match="n must be an integer"):
|
|
fibonacci(3.5)
|
|
with pytest.raises(TypeError, match="n must be an integer"):
|
|
fibonacci("5")
|
|
with pytest.raises(TypeError, match="n must be an integer"):
|
|
fibonacci([1])
|
|
with pytest.raises(TypeError, match="n must be an integer"):
|
|
fibonacci(None)
|
|
|
|
def test_zero_input(self):
|
|
"""Test that n=0 returns empty list"""
|
|
result = fibonacci(0)
|
|
assert result == []
|
|
assert isinstance(result, list)
|
|
|
|
def test_sequence_correctness(self):
|
|
"""Test that the sequence follows Fibonacci rules"""
|
|
# Test that each term is the sum of the two preceding terms
|
|
n = 10
|
|
sequence = fibonacci(n)
|
|
for i in range(2, n):
|
|
assert sequence[i] == sequence[i-1] + sequence[i-2]
|
|
|
|
def test_return_type(self):
|
|
"""Test that the function always returns a list"""
|
|
assert isinstance(fibonacci(0), list)
|
|
assert isinstance(fibonacci(1), list)
|
|
assert isinstance(fibonacci(5), list)
|
|
assert isinstance(fibonacci(10), list)
|
|
|
|
def test_very_small_n(self):
|
|
"""Test edge cases with very small n values"""
|
|
assert fibonacci(0) == []
|
|
assert fibonacci(1) == [0]
|
|
assert fibonacci(2) == [0, 1]
|
|
|
|
def test_consistency_across_calls(self):
|
|
"""Test that multiple calls with same input give same result"""
|
|
result1 = fibonacci(5)
|
|
result2 = fibonacci(5)
|
|
assert result1 == result2
|
|
assert result1 is not result2 # Should return new list each time
|
|
``` |