org.jeometry.test.math.solver.GaussEliminationSolverTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jeometry-test Show documentation
Show all versions of jeometry-test Show documentation
Jeometry, a Mathematic and Geometry library for Java
package org.jeometry.test.math.solver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import org.jeometry.Jeometry;
import org.jeometry.factory.JeometryFactory;
import org.jeometry.math.Matrix;
import org.jeometry.math.Vector;
import org.jeometry.math.solver.Solver;
import org.jeometry.test.math.MathTestData;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
* A Gauss Elimination based solver test.
*
* Usage:
*
* Create a class that extends this one and add the method:
*
* {@literal @}BeforeClass
* public static void initClass() {
* solver = [the solver to test];
* }
*
*
* @author Julien Seinturier - COMEX S.A. - [email protected] - https://github.com/jorigin/jeometry
* @version {@value Jeometry#version}
* @since 1.0.0
* */
public class GaussEliminationSolverTest {
protected static Solver solver = null;
/**
* Initialize the test static context.
*/
@BeforeAll
public static void initClass() {
fail("method public static void init() has to be set up with @BeforeClass annotation");
}
/**
* Test the underlying solver
*/
@Test
public void solveTest() {
assertNotNull(solver, "Solver is not initialized");
Matrix a = JeometryFactory.createMatrix(MathTestData.SOLVER_GAUSS_ELIMINATION_A);
Vector b = JeometryFactory.createVector(MathTestData.SOLVER_GAUSS_ELIMINATION_B);
Vector x;
try {
x = solver.solve(a, b);
assertNotNull(x, "No solution found");
assertEquals(a.getRowsCount(), x.getDimension(), "Invalid x size");
assertEquals(MathTestData.SOLVER_GAUSS_ELIMINATION_X[0], x.getValue(0), 0.000000000001d, "Invalid x vector ("+x.getValue(0)+", "+x.getValue(1)+", "+x.getValue(2)+")");
assertEquals(MathTestData.SOLVER_GAUSS_ELIMINATION_X[1], x.getValue(1), 0.000000000001d, "Invalid x vector ("+x.getValue(0)+", "+x.getValue(1)+", "+x.getValue(2)+")");
assertEquals(MathTestData.SOLVER_GAUSS_ELIMINATION_X[2], x.getValue(2), 0.000000000001d, "Invalid x vector ("+x.getValue(0)+", "+x.getValue(1)+", "+x.getValue(2)+")");
} catch (Exception e) {
fail("Unexpected excpetion "+e.getMessage());
}
}
}