# test_math_tools.py import sys import os from dotenv import load_dotenv # Load environment variables load_dotenv() # Add tools to path sys.path.append(os.path.join(os.path.dirname(__file__), '..')) def test_math_tools(): """Test all math tool functions""" try: from tools.math_tools import MathTools, add, subtract, multiply, divide, factorial, square_root, average, calculate_expression print(" 📊 Testing basic arithmetic...") # Test basic operations assert add(5, 3) == 8, "Addition failed" assert subtract(10, 4) == 6, "Subtraction failed" assert multiply(6, 7) == 42, "Multiplication failed" assert divide(15, 3) == 5, "Division failed" print(" ✅ Basic arithmetic: PASSED") # Test advanced operations print(" 🧮 Testing advanced operations...") assert factorial(5) == 120, "Factorial failed" assert square_root(16) == 4.0, "Square root failed" assert average([1, 2, 3, 4, 5]) == 3.0, "Average failed" print(" ✅ Advanced operations: PASSED") # Test error handling print(" 🚨 Testing error handling...") assert "Error" in str(divide(5, 0)), "Division by zero should return error" assert "Error" in str(factorial(-1)), "Negative factorial should return error" assert "Error" in str(square_root(-1)), "Negative square root should return error" print(" ✅ Error handling: PASSED") # Test expression calculator print(" 🧠 Testing expression calculator...") assert calculate_expression("2 + 3 * 4") == 14, "Expression calculation failed" assert calculate_expression("2^3") == 8, "Power expression failed" print(" ✅ Expression calculator: PASSED") # Test quadratic solver print(" 📐 Testing quadratic solver...") solutions = MathTools.solve_quadratic(1, -5, 6) assert isinstance(solutions, tuple) and len(solutions) == 2, "Quadratic solver failed" print(" ✅ Quadratic solver: PASSED") # Test practical examples print(" 💰 Testing practical calculations...") compound_interest = MathTools.calculate_compound_interest(1000, 0.05, 2) assert compound_interest > 1000, "Compound interest should be greater than principal" percentage_result = MathTools.percentage(25, 100) assert percentage_result == 25.0, "Percentage calculation failed" print(" ✅ Practical calculations: PASSED") print(" ✅ ALL MATH TESTS PASSED!") # Demo output print("\n 📋 Math Tools Demo:") print(f" 5 + 3 = {add(5, 3)}") print(f" 10! = {factorial(10)}") print(f" √144 = {square_root(144)}") print(f" Average of [10,20,30] = {average([10,20,30])}") print(f" Expression '2^3 + 4*5' = {calculate_expression('2^3 + 4*5')}") except ImportError as e: print(f" ❌ Import error: {e}") except Exception as e: print(f" ❌ Test failed: {e}") raise if __name__ == "__main__": test_math_tools()