diff --git a/.gitignore b/.gitignore index 5d2b7d35..695f3ca5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ **/*.pyc **/.DS_Store .coverage -venv/ \ No newline at end of file +venv/ diff --git a/src/calculator.py b/src/calculator.py index 5eb03460..622475e3 100644 --- a/src/calculator.py +++ b/src/calculator.py @@ -1,12 +1,24 @@ -# -# Write a program that given two numbers as input make the main operations. -# -# Output: -# Insert first number: 4 -# Insert second number: 2 -# -# SUM: 6 -# Difference: 2 -# Multiplication: 8 -# Division: 2 -# +def add(a: int, b: int) -> int: + return a + b + + +def sub(a: int, b: int) -> int: + return a - b + + +def mul(a: int, b: int) -> int: + return a * b + + +def div(a: int, b: int) -> float | str: + return "You can't divide by 0" if b == 0 else a / b + + +if __name__ == "__main__": + x = int(input("Insert first number: ")) + y = int(input("Insert second number: ")) + + print(f"Sum of {x} and {y} = {add(x, y)}") + print(f"Subtraction of {x} and {y} = {sub(x, y)}") + print(f"Multiplication of {x} and {y} = {mul(x, y)}") + print(f"Division of {x} and {y} = {div(x, y)}") diff --git a/tests/test_calculator.py b/tests/test_calculator.py new file mode 100644 index 00000000..ee52ae83 --- /dev/null +++ b/tests/test_calculator.py @@ -0,0 +1,25 @@ +from src.calculator import add, div, mul, sub + + +def test_add() -> None: + assert add(5, 4) == 9 + assert add(-1, 1) == 0 + assert add(0, 0) == 0 + + +def test_sub() -> None: + assert sub(1, 1) == 0 + assert sub(0, 5) == -5 + + +def test_mul() -> None: + assert mul(5, 1) == 5 + assert mul(0, 5) == 0 + assert mul(-2, 3) == -6 + + +def test_div() -> None: + assert div(5, 1) == 5 + assert div(0, 5) == 0 + assert div(5, 0) == "You can't divide by 0" + assert div(-6, 2) == -3