|
|
| """
|
| 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:
|
|
|
| calculator_path = "/home/user/Desktop/calculator.py"
|
|
|
| if not os.path.exists(calculator_path):
|
| print("ERROR: calculator.py file not found")
|
| return 0.0
|
|
|
|
|
| spec = importlib.util.spec_from_file_location("calculator", calculator_path)
|
| calculator_module = importlib.util.module_from_spec(spec)
|
| spec.loader.exec_module(calculator_module)
|
|
|
|
|
| if not hasattr(calculator_module, 'Calculator'):
|
| print("ERROR: Calculator class not found")
|
| return 0.0
|
|
|
| Calculator = calculator_module.Calculator
|
|
|
|
|
| try:
|
| calc = Calculator()
|
| print("β Calculator instance created successfully")
|
| except Exception as e:
|
| print(f"ERROR: Failed to create Calculator instance: {e}")
|
| return 0.0
|
|
|
|
|
| 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
|
|
|
|
|
| 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
|
|
|
|
|
| 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
|
|
|
|
|
| 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
|
|
|
|
|
| try:
|
| result = calc.divide(10, 0)
|
|
|
| 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
|
|
|
|
|
| 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)
|
|
|