diff --git a/pymath/__init__.py b/pymath/__init__.py index e69de29..cbe76dd 100644 --- a/pymath/__init__.py +++ b/pymath/__init__.py @@ -0,0 +1,3 @@ +from pymath.arithmetic import add, subtract, multiply, divide + +__all__ = ["add", "subtract", "multiply", "divide"] diff --git a/pymath/arithmetic.py b/pymath/arithmetic.py index e69de29..9ffb8f4 100644 --- a/pymath/arithmetic.py +++ b/pymath/arithmetic.py @@ -0,0 +1,27 @@ +# mathlib/arithmetic.py + + +def add(a: float, b: float) -> float: + """Return the sum of a and b.""" + return a + b + + +def subtract(a: float, b: float) -> float: + """Return a minus b.""" + return a - b + + +def multiply(a: float, b: float) -> float: + """Return a multiplied by b.""" + return a * b + + +def divide(a: float, b: float) -> float: + """Return a divided by b. + + Raises: + ValueError: if b is zero. + """ + if b == 0: + raise ValueError("Cannot divide by zero") + return a / b diff --git a/pyproject.toml b/pyproject.toml index ffd8eac..365f461 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] requires = ["setuptools>=68"] -build-backend = "setuptools.backends.legacy:build" +build-backend = "setuptools.build_meta" [project] name = "mathlib" @@ -25,4 +25,4 @@ select = ["E", "F", "W"] # pycodestyle + pyflakes rules testpaths = ["tests"] [tool.coverage.report] -omit = ["tests/*"] \ No newline at end of file +omit = ["tests/*"] diff --git a/test/test_arithmetic.py b/test/test_arithmetic.py index e69de29..4848416 100644 --- a/test/test_arithmetic.py +++ b/test/test_arithmetic.py @@ -0,0 +1,42 @@ +# tests/test_arithmetic.py +import pytest +from pymath import add, subtract, multiply, divide + + +class TestAdd: + def test_positive_numbers(self): + assert add(2, 3) == 5 + + def test_negative_numbers(self): + assert add(-1, -2) == -3 + + def test_floats(self): + assert add(0.1, 0.2) == pytest.approx(0.3) # ← important! + + +class TestSubtract: + def test_basic(self): + assert subtract(10, 4) == 6 + + def test_result_is_negative(self): + assert subtract(2, 5) == -3 + + +class TestMultiply: + def test_basic(self): + assert multiply(3, 4) == 12 + + def test_by_zero(self): + assert multiply(99, 0) == 0 + + +class TestDivide: + def test_basic(self): + assert divide(10, 2) == 5.0 + + def test_returns_float(self): + assert isinstance(divide(7, 2), float) + + def test_divide_by_zero_raises(self): + with pytest.raises(ValueError, match="Cannot divide by zero"): + divide(5, 0)