-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Add tests for EulerMethod
#5769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a28c364
Add tests for `EulerMethod`
Hardvan 071faaa
Update directory
Hardvan e6728c0
Fix
Hardvan 93a26fa
Merge remote-tracking branch 'origin/euler_add_tests' into euler_add_…
Hardvan e80af94
Fix
Hardvan 698d0be
Fix
Hardvan 164525d
Merge branch 'master' into euler_add_tests
Hardvan e1d6566
Update directory
Hardvan e85f80d
Fix
Hardvan 986aaac
Merge remote-tracking branch 'origin/euler_add_tests' into euler_add_…
Hardvan 9ea6ed7
Fix
Hardvan c37f4a5
Merge branch 'master' into euler_add_tests
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/test/java/com/thealgorithms/maths/EulerMethodTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.ArrayList; | ||
import java.util.function.BiFunction; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class EulerMethodTest { | ||
|
||
@Test | ||
public void testEulerStepBasic() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> x + y; | ||
double result = EulerMethod.eulerStep(0.0, 0.1, 1.0, equation); | ||
assertEquals(1.1, result, 1e-9, "Euler step failed for basic equation."); | ||
} | ||
|
||
@Test | ||
public void testEulerStepNegativeStepSize() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> x + y; | ||
assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerStep(0.0, -0.1, 1.0, equation), "Expected IllegalArgumentException for negative step size."); | ||
} | ||
|
||
@Test | ||
public void testEulerStepZeroStepSize() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> x + y; | ||
assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerStep(0.0, 0.0, 1.0, equation), "Expected IllegalArgumentException for zero step size."); | ||
} | ||
|
||
@Test | ||
public void testEulerFullBasic() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> x; | ||
ArrayList<double[]> result = EulerMethod.eulerFull(0, 1, 0.5, 0, equation); | ||
|
||
assertEquals(3, result.size(), "Incorrect number of points in the result."); | ||
assertArrayEquals(new double[] {0.0, 0.0}, result.get(0), 1e-9, "Incorrect first point."); | ||
assertArrayEquals(new double[] {0.5, 0.0}, result.get(1), 1e-9, "Incorrect second point."); | ||
assertArrayEquals(new double[] {1.0, 0.25}, result.get(2), 1e-9, "Incorrect third point."); | ||
} | ||
|
||
@Test | ||
public void testEulerFullWithExponentialEquation() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> y; | ||
ArrayList<double[]> result = EulerMethod.eulerFull(0, 1, 0.1, 1, equation); | ||
|
||
assertEquals(12, result.size(), "Incorrect number of points in the result."); | ||
double[] lastPoint = result.get(result.size() - 1); | ||
assertEquals(1.0999999999999999, lastPoint[0], 1e-9, "Incorrect x-value of the last point."); | ||
assertEquals(2.8531167061100002, lastPoint[1], 1e-9, "Incorrect y-value of the last point."); | ||
} | ||
|
||
@Test | ||
public void testEulerFullInvalidRange() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> x + y; | ||
assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerFull(1, 0, 0.1, 1, equation), "Expected IllegalArgumentException for invalid range (xStart >= xEnd)."); | ||
} | ||
|
||
@Test | ||
public void testEulerFullZeroStepSize() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> x + y; | ||
assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerFull(0, 1, 0.0, 1, equation), "Expected IllegalArgumentException for zero step size."); | ||
} | ||
|
||
@Test | ||
public void testEulerFullNegativeStepSize() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> x + y; | ||
assertThrows(IllegalArgumentException.class, () -> EulerMethod.eulerFull(0, 1, -0.1, 1, equation), "Expected IllegalArgumentException for negative step size."); | ||
} | ||
|
||
@Test | ||
public void testEulerFullSingleStep() { | ||
BiFunction<Double, Double, Double> equation = (x, y) -> x + y; | ||
ArrayList<double[]> result = EulerMethod.eulerFull(0, 0.1, 0.1, 1, equation); | ||
assertEquals(2, result.size(), "Incorrect number of points for single step."); | ||
assertArrayEquals(new double[] {0.0, 1.0}, result.get(0), 1e-9, "Incorrect first point."); | ||
assertArrayEquals(new double[] {0.1, 1.1}, result.get(1), 1e-9, "Incorrect second point."); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.