|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.function.BiFunction; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +public class EulerMethodTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void testEulerStepBasic() { |
| 15 | + BiFunction<Double, Double, Double> equation = (x, y) -> x + y; |
| 16 | + double result = EulerMethod.eulerStep(0.0, 0.1, 1.0, equation); |
| 17 | + assertEquals(1.1, result, 1e-9, "Euler step failed for basic equation."); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testEulerStepNegativeStepSize() { |
| 22 | + BiFunction<Double, Double, Double> equation = (x, y) -> x + y; |
| 23 | + assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerStep(0.0, -0.1, 1.0, equation), "Expected IllegalArgumentException for negative step size."); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testEulerStepZeroStepSize() { |
| 28 | + BiFunction<Double, Double, Double> equation = (x, y) -> x + y; |
| 29 | + assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerStep(0.0, 0.0, 1.0, equation), "Expected IllegalArgumentException for zero step size."); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testEulerFullBasic() { |
| 34 | + BiFunction<Double, Double, Double> equation = (x, y) -> x; |
| 35 | + ArrayList<double[]> result = EulerMethod.eulerFull(0, 1, 0.5, 0, equation); |
| 36 | + |
| 37 | + assertEquals(3, result.size(), "Incorrect number of points in the result."); |
| 38 | + assertArrayEquals(new double[] {0.0, 0.0}, result.get(0), 1e-9, "Incorrect first point."); |
| 39 | + assertArrayEquals(new double[] {0.5, 0.0}, result.get(1), 1e-9, "Incorrect second point."); |
| 40 | + assertArrayEquals(new double[] {1.0, 0.25}, result.get(2), 1e-9, "Incorrect third point."); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testEulerFullWithExponentialEquation() { |
| 45 | + BiFunction<Double, Double, Double> equation = (x, y) -> y; |
| 46 | + ArrayList<double[]> result = EulerMethod.eulerFull(0, 1, 0.1, 1, equation); |
| 47 | + |
| 48 | + assertEquals(12, result.size(), "Incorrect number of points in the result."); |
| 49 | + double[] lastPoint = result.get(result.size() - 1); |
| 50 | + assertEquals(1.0999999999999999, lastPoint[0], 1e-9, "Incorrect x-value of the last point."); |
| 51 | + assertEquals(2.8531167061100002, lastPoint[1], 1e-9, "Incorrect y-value of the last point."); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testEulerFullInvalidRange() { |
| 56 | + BiFunction<Double, Double, Double> equation = (x, y) -> x + y; |
| 57 | + assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerFull(1, 0, 0.1, 1, equation), "Expected IllegalArgumentException for invalid range (xStart >= xEnd)."); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testEulerFullZeroStepSize() { |
| 62 | + BiFunction<Double, Double, Double> equation = (x, y) -> x + y; |
| 63 | + assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerFull(0, 1, 0.0, 1, equation), "Expected IllegalArgumentException for zero step size."); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testEulerFullNegativeStepSize() { |
| 68 | + BiFunction<Double, Double, Double> equation = (x, y) -> x + y; |
| 69 | + assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerFull(0, 1, -0.1, 1, equation), "Expected IllegalArgumentException for negative step size."); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testEulerFullSingleStep() { |
| 74 | + BiFunction<Double, Double, Double> equation = (x, y) -> x + y; |
| 75 | + ArrayList<double[]> result = EulerMethod.eulerFull(0, 0.1, 0.1, 1, equation); |
| 76 | + |
| 77 | + assertEquals(2, result.size(), "Incorrect number of points for single step."); |
| 78 | + assertArrayEquals(new double[] {0.0, 1.0}, result.get(0), 1e-9, "Incorrect first point."); |
| 79 | + assertArrayEquals(new double[] {0.1, 1.1}, result.get(1), 1e-9, "Incorrect second point."); |
| 80 | + } |
| 81 | +} |
0 commit comments