osworld / test_calculator.py
zainaftab-fx's picture
Upload test_calculator.py
9e61641 verified
#!/usr/bin/env python3
"""
Test suite for Calculator class
This file tests the calculator.py implementation
"""
import sys
import os
import importlib.util
def test_calculator_class():
"""
Test the Calculator class implementation
Returns 1.0 if all tests pass, 0.0 if any test fails
"""
try:
# Import the calculator module
calculator_path = "/home/user/Desktop/calculator.py"
if not os.path.exists(calculator_path):
print("ERROR: calculator.py file not found")
return 0.0
# Load the module dynamically
spec = importlib.util.spec_from_file_location("calculator", calculator_path)
calculator_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(calculator_module)
# Check if Calculator class exists
if not hasattr(calculator_module, 'Calculator'):
print("ERROR: Calculator class not found")
return 0.0
Calculator = calculator_module.Calculator
# Test 1: Create instance
try:
calc = Calculator()
print("βœ“ Calculator instance created successfully")
except Exception as e:
print(f"ERROR: Failed to create Calculator instance: {e}")
return 0.0
# Test 2: Test add method
try:
result = calc.add(10, 5)
if result == 15:
print("βœ“ Add method works correctly")
else:
print(f"ERROR: Add method returned {result}, expected 15")
return 0.0
except Exception as e:
print(f"ERROR: Add method failed: {e}")
return 0.0
# Test 3: Test subtract method
try:
result = calc.subtract(10, 5)
if result == 5:
print("βœ“ Subtract method works correctly")
else:
print(f"ERROR: Subtract method returned {result}, expected 5")
return 0.0
except Exception as e:
print(f"ERROR: Subtract method failed: {e}")
return 0.0
# Test 4: Test multiply method
try:
result = calc.multiply(10, 5)
if result == 50:
print("βœ“ Multiply method works correctly")
else:
print(f"ERROR: Multiply method returned {result}, expected 50")
return 0.0
except Exception as e:
print(f"ERROR: Multiply method failed: {e}")
return 0.0
# Test 5: Test divide method (normal case)
try:
result = calc.divide(10, 5)
if result == 2.0:
print("βœ“ Divide method works correctly")
else:
print(f"ERROR: Divide method returned {result}, expected 2.0")
return 0.0
except Exception as e:
print(f"ERROR: Divide method failed: {e}")
return 0.0
# Test 6: Test divide method (division by zero)
try:
result = calc.divide(10, 0)
# Should handle division by zero gracefully
if isinstance(result, str) and "Error" in result:
print("βœ“ Divide method handles division by zero correctly")
elif result == float('inf') or result == float('-inf'):
print("βœ“ Divide method handles division by zero (returns infinity)")
else:
print(f"ERROR: Divide method didn't handle division by zero properly. Got: {result}")
return 0.0
except ZeroDivisionError:
print("βœ“ Divide method raises ZeroDivisionError (acceptable)")
except Exception as e:
print(f"ERROR: Divide method failed on division by zero: {e}")
return 0.0
# Test 7: Test with different numbers
try:
result1 = calc.add(3, 7)
result2 = calc.subtract(20, 8)
result3 = calc.multiply(4, 6)
result4 = calc.divide(15, 3)
if result1 == 10 and result2 == 12 and result3 == 24 and result4 == 5.0:
print("βœ“ Calculator works with different numbers")
else:
print(f"ERROR: Calculator failed with different numbers")
return 0.0
except Exception as e:
print(f"ERROR: Calculator failed with different numbers: {e}")
return 0.0
print("βœ“ All tests passed!")
return 1.0
except Exception as e:
print(f"ERROR: Test suite failed with exception: {e}")
return 0.0
if __name__ == "__main__":
result = test_calculator_class()
sys.exit(0 if result == 1.0 else 1)