|
| 1 | +# Test cases for IndefiniteIntegralSolver |
| 2 | +from meth import IndefiniteIntegralSolver |
| 3 | + |
| 4 | + |
| 5 | +def test_indefinite_integral_solver(): |
| 6 | + test_cases = [ |
| 7 | + # Easy cases |
| 8 | + ("3*x^2 + 2*x + 1", "1.0*x^3 + 1.0*x^2 + 1*x + C"), |
| 9 | + ("x^2 + x + 1", "0.3333333333333333*x^3 + 0.5*x^2 + 1*x + C"), |
| 10 | + ("5*x + 3", "2.5*x^2 + 3*x + C"), |
| 11 | + |
| 12 | + # Harder cases |
| 13 | + ("4*x^3 + 3*x^2 + 2*x + 1", "1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"), |
| 14 | + ("6*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1", "1.0*x^6 + 1.0*x^5 + 1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"), |
| 15 | + |
| 16 | + # Cases with brackets |
| 17 | + ("(3*x^2) + (2*x) + 1", "1.0*x^3 + 1.0*x^2 + 1*x + C"), |
| 18 | + ("(4*x^3) + (3*x^2) + (2*x) + 1", "1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"), |
| 19 | + |
| 20 | + # Edge cases |
| 21 | + ("x", "0.5*x^2 + C"), |
| 22 | + ("1", "1*x + C"), |
| 23 | + ("x^0.5", "0.6666666666666666*x^1.5 + C"), |
| 24 | + ("1/2", "0.5*x + C"), |
| 25 | + ("1/x", "-1/2*x^-2 + C"), |
| 26 | + ("0", "0*x + C"), |
| 27 | + ("x^2", "0.3333333333333333*x^3 + C"), |
| 28 | + ("x^3", "0.25*x^4 + C"), |
| 29 | + ("x^4", "0.2*x^5 + C"), |
| 30 | + ("x^5", "0.16666666666666666*x^6 + C"), |
| 31 | + ("(1/2)*(x^2)", "0.16666666666666666*x^3 + C"), |
| 32 | + ("(-1/2)*(x^-1)", "x^-2"), |
| 33 | + ("(1/2)*(x^-1/2)", "x^0.5 + C"), |
| 34 | + |
| 35 | + # Invalid cases (should handle gracefully) |
| 36 | + ("3*x^2 + 2*y + 1", "1.0*x^3 + 2.0*x + 1*x + C"), # y is not a valid variable for integration |
| 37 | + ("3*x^2 + 2* + 1", "1.0*x^3 + 2.0*x + 1*x + C"), # Invalid term "2*" |
| 38 | + ] |
| 39 | + |
| 40 | + counter = 0 |
| 41 | + fail_counter = 0 |
| 42 | + for expression, expected in test_cases: |
| 43 | + solver = IndefiniteIntegralSolver(expression) |
| 44 | + result = solver.integrate() |
| 45 | + try: |
| 46 | + assert result == expected, f"Test failed for expression: {expression}. Expected: {expected}, Got: {result}" |
| 47 | + print(f"✅ Test passed for expression: {expression}. Result: {result}") |
| 48 | + except AssertionError: |
| 49 | + print(f"❌ Test failed for expression: {expression}. Expected: {expected}, Got: {result}") |
| 50 | + fail_counter += 1 |
| 51 | + counter += 1 |
| 52 | + print(f"\nTotal tests 🧪: {counter}, " |
| 53 | + f"\nPassed ✅: {counter - fail_counter}, " |
| 54 | + f"\nFailed ❌: {fail_counter}") |
| 55 | + |
| 56 | +# Run the tests |
| 57 | +test_indefinite_integral_solver() |
0 commit comments