AbstractEquationSystemSolver implements EquationSystemSolver<E> { @Override public final List<E> solve(Matrix<E> coefficients, List<E> values) { Objects.requireNonNull(coefficients); Objects.requireNonNull(values); if (coefficients.getRows() != coefficients.getColumns()) { throw new IllegalArgumentException("Coefficient matrix must be square"); } if (values.size() != coefficients.getColumns()) { throw new IllegalArgumentException("Expected the number of values to be compatible with the dimension of the coefficients matrix"); } if (coefficients.getRows() == 0) { return Collections.emptyList(); } final List<E> result = doSolve(MatrixUtils.copyOf(coefficients), new ArrayList<E>(values)); Objects.requireNonNull(result); if (result.size() != values.size()) { throw new IllegalStateException("Expected the number of resolved variables to be the same as the initial equations"); } return result; } AbstractEquationSystemSolver(Class<E> type); @Override final List<E> solve(Matrix<E> coefficients, List<E> values);  }
@Test(expectedExceptions = NullPointerException.class) public void testThatItIsUnhappyWhenCoefficientsAreNull() throws Exception { final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, null); solver.solve(null, Collections.<Integer>emptyList()); }
@Test(expectedExceptions = NullPointerException.class) public void testThatItIsUnhappyWhenValuesAreNull() throws Exception { final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, null); solver.solve(new ArrayMatrix<Integer>(0, 0), null); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*square.*") public void testThatItIsUnhappyIfCoefficientsIsNotSquare() throws Exception { final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, null); solver.solve(new ArrayMatrix<Integer>(3, 2), Collections.<Integer>emptyList()); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*compatible.*") public void testThatItComplainsIfValueIsNotCompatibleWithCoefficients() throws Exception { final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, null); solver.solve(new ArrayMatrix<Integer>(4, 4), Arrays.asList(1, 2, 3)); }
@Test public void testThatItReturnsAnEmptyListWhenInputsAreEmpty() throws Exception { final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, null); final List<Integer> solution = solver.solve(new ArrayMatrix<Integer>(0, 0), Collections.<Integer>emptyList()); assertThat(solution, is(Matchers.<Integer>empty())); }
@Test(expectedExceptions = NullPointerException.class) public void testThatItComplainsWhenSolutionIsNull() throws Exception { final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, null); solver.solve(new ArrayMatrix<Integer>(3, 3), Arrays.asList(1, 2, 3)); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*variables.*") public void testThatItComplainsWhenSolutionIsIncompatible() throws Exception { final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, Arrays.asList(4, 5, 6, 7)); solver.solve(new ArrayMatrix<Integer>(3, 3), Arrays.asList(1, 2, 3)); }
@Test public void testThatItReturnsTheOriginalResultIfItIsCompatible() throws Exception { final List<Integer> result = Arrays.asList(1, 2, 3); final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, result); final List<Integer> solution = solver.solve(new ArrayMatrix<Integer>(3, 3), Arrays.asList(1, 2, 3)); assertThat(solution, is(result)); assertThat(solution == result, is(true)); }
@Test public void testThatItPassesACopyOfTheInputDown() throws Exception { final SampleAbstractEquationSystemSolver<Integer> solver = new SampleAbstractEquationSystemSolver<>(Integer.class, Arrays.asList(1, 2, 3)); final Matrix<Integer> coefficients = new ArrayMatrix<>(3, 3); final List<Integer> values = Arrays.asList(4, 5, 6); solver.solve(coefficients, values); assertThat(solver.getCoefficients() == coefficients, is(false)); assertThat(solver.getValues() == values, is(false)); }