Skip to content
Open
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
13 changes: 13 additions & 0 deletions src/math_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ def add(a, b):
def multiply(a, b):
"""Multiply two numbers"""
return a * b

def fibonacci(n):
"""Calculate the nth Fibonacci number (0-indexed)."""
if n < 0:
raise ValueError("n must be non-negative")
if n == 0:
return 0
if n == 1:
return 1
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
15 changes: 14 additions & 1 deletion tests/test_math_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sys
import pytest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent / 'src'))

from math_utils import add, multiply
from math_utils import add, multiply, fibonacci

def test_add():
assert add(2, 3) == 5
Expand All @@ -13,3 +14,15 @@ def test_multiply():
assert multiply(2, 3) == 6
assert multiply(-1, 5) == -5
assert multiply(0, 10) == 0

def test_fibonacci_normal_cases():
assert fibonacci(0) == 0
assert fibonacci(1) == 1
assert fibonacci(5) == 5
assert fibonacci(10) == 55
assert fibonacci(20) == 6765

def test_fibonacci_edge_case():
with pytest.raises(ValueError) as exc_info:
fibonacci(-1)
assert "n must be non-negative" in str(exc_info.value)