-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
520c1e7
commit c3d8560
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
def basic_math_operation(x, y, operation): | ||
"""Performs a basic math operation on two numbers. | ||
:param x: First number | ||
:param y: Second number | ||
:param operation: Operation to perform: 'add', 'subtract', 'multiply', or 'divide' | ||
:return: Result of the operation | ||
""" | ||
if operation == 'add': | ||
return x + y | ||
elif operation == 'subtract': | ||
return x - y | ||
elif operation == 'multiply': | ||
return x * y | ||
elif operation == 'divide': | ||
if y == 0: | ||
raise ValueError("Cannot divide by zero") | ||
return x / y | ||
else: | ||
raise ValueError("Unsupported operation") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
import os | ||
from math_operations import basic_math_operation | ||
|
||
def test_add(): | ||
assert basic_math_operation(2, 3, 'add') == 5 | ||
assert basic_math_operation(-2, 3, 'add') == 1 | ||
assert basic_math_operation(0, 0, 'add') == 0 | ||
|
||
def test_subtract(): | ||
assert basic_math_operation(5, 3, 'subtract') == 2 | ||
assert basic_math_operation(2, 5, 'subtract') == -3 | ||
assert basic_math_operation(0, 0, 'subtract') == 0 | ||
|
||
if os.getenv("RUN_GPU_TESTS") != "true": | ||
def test_multiply(): | ||
assert basic_math_operation(2, 3, 'multiply') == 6 | ||
assert basic_math_operation(-2, 3, 'multiply') == -6 | ||
assert basic_math_operation(0, 5, 'multiply') == 0 | ||
|
||
def test_divide(): | ||
assert basic_math_operation(6, 3, 'divide') == 2 | ||
assert basic_math_operation(7, 2, 'divide') == 3.5 | ||
|
||
with pytest.raises(ValueError): | ||
basic_math_operation(6, 0, 'divide') | ||
|
||
def test_invalid_operation(): | ||
with pytest.raises(ValueError): | ||
basic_math_operation(2, 3, 'modulus') |