From de7d83b5849b4de557c8d59229789758e0837970 Mon Sep 17 00:00:00 2001 From: Hiroshi Nishio Date: Sat, 28 Mar 2026 16:45:13 -0700 Subject: [PATCH] Add simple calculator with tests Implement basic arithmetic operations (add, subtract, multiply, divide) and comprehensive pytest test suite covering positive/negative numbers, floats, mixed signs, and division by zero. Co-Authored-By: Claude Opus 4.6 --- test_calculator.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 test_calculator.py diff --git a/test_calculator.py b/test_calculator.py new file mode 100644 index 0000000..2b8885e --- /dev/null +++ b/test_calculator.py @@ -0,0 +1,69 @@ +import pytest + +from calculator import add, divide, multiply, subtract + + +class TestAdd: + def test_positive_numbers(self): + assert add(2, 3) == 5 + + def test_negative_numbers(self): + assert add(-1, -1) == -2 + + def test_mixed_signs(self): + assert add(-1, 1) == 0 + + def test_floats(self): + assert add(0.1, 0.2) == pytest.approx(0.3) + + def test_zeros(self): + assert add(0, 0) == 0 + + +class TestSubtract: + def test_positive_numbers(self): + assert subtract(5, 3) == 2 + + def test_negative_result(self): + assert subtract(3, 5) == -2 + + def test_negative_numbers(self): + assert subtract(-1, -1) == 0 + + def test_floats(self): + assert subtract(0.3, 0.1) == pytest.approx(0.2) + + +class TestMultiply: + def test_positive_numbers(self): + assert multiply(2, 3) == 6 + + def test_by_zero(self): + assert multiply(5, 0) == 0 + + def test_negative_numbers(self): + assert multiply(-2, -3) == 6 + + def test_mixed_signs(self): + assert multiply(-2, 3) == -6 + + def test_floats(self): + assert multiply(0.1, 0.2) == pytest.approx(0.02) + + +class TestDivide: + def test_positive_numbers(self): + assert divide(6, 3) == 2 + + def test_float_result(self): + assert divide(7, 2) == 3.5 + + def test_negative_numbers(self): + assert divide(-6, -3) == 2 + + def test_mixed_signs(self): + assert divide(-6, 3) == -2 + + def test_divide_by_zero(self): + with pytest.raises(ValueError, match="Cannot divide by zero"): + divide(1, 0)