Skip to content

Commit

Permalink
Add tests for Solution. Fix couple of issues with loop ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Oct 28, 2024
1 parent 264c002 commit 57bab0c
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/by/andd3dfx/math/pde/solver/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void sUt(String fileName, double[] t) {
}

var sb = new StringBuilder();
for (var i = 0; i <= area.x().n(); i++) {
for (var i = 0; i < area.x().n(); i++) {
sb.append(area.x().x(i));
for (var t_i : t) {
sb.append(" ").append(solution.get(area.t().i(t_i), i));
Expand Down Expand Up @@ -53,7 +53,7 @@ public void sUx(String fileName, double[] x) {
}

var sb = new StringBuilder();
for (int i = 0; i <= area.t().n(); i++) {
for (int i = 0; i < area.t().n(); i++) {
sb.append(area.t().x(i));
for (var x_i : x) {
sb.append(" ").append(solution.get(i, area.x().i(x_i)));
Expand Down Expand Up @@ -124,7 +124,7 @@ public Matrix gUx(int ix) {

int M = solution.getM();
var matrix = new Matrix(2, M);
for (int i = 0; i <= M; i++) {
for (int i = 0; i < M; i++) {
matrix.setX(i, area.t().x(i));
matrix.setY(i, solution.get(i, ix));
}
Expand Down
195 changes: 195 additions & 0 deletions src/test/java/by/andd3dfx/math/pde/solver/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package by.andd3dfx.math.pde.solver;

import by.andd3dfx.math.Area;
import by.andd3dfx.math.Interval;
import by.andd3dfx.math.Matrix;
import by.andd3dfx.math.pde.equation.Equation;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static by.andd3dfx.util.FileComparisonHelper.BUILD_PATH;
import static by.andd3dfx.util.FileComparisonHelper.checkGeneratedFileContent;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class SolutionTest {

@Test
void sUtForArray() throws IOException {
var fileName = "sUt-array.txt";
var solution = buildSolution();

solution.sUt(BUILD_PATH + fileName, new double[]{2.5, 5.0});

checkGeneratedFileContent(fileName);
}

@Test
void sUtForArrayWithWrongParam() {
var solution = buildSolution();

assertThrows(AssertionError.class, () ->
solution.sUt(BUILD_PATH + "sUt-arr.txt", new double[]{1.0, 2.5}));
assertThrows(AssertionError.class, () ->
solution.sUt(BUILD_PATH + "sUt-arr.txt", new double[]{2.5, 2.6, 6.5}));
}

@Test
void sUt() throws IOException {
var fileName = "sUt.txt";
var solution = buildSolution();

solution.sUt(BUILD_PATH + fileName, 5.0);

checkGeneratedFileContent(fileName);
}

@Test
void sUtWithWrongParam() {
var solution = buildSolution();

assertThrows(AssertionError.class, () -> solution.sUt(BUILD_PATH + "sUt.txt", 1.0));
assertThrows(AssertionError.class, () -> solution.sUt(BUILD_PATH + "sUt.txt", 6.5));
}

@Test
void sUxForArray() throws IOException {
var fileName = "sUx-array.txt";
var solution = buildSolution();

solution.sUx(BUILD_PATH + fileName, new double[]{11.0, 12.0});

checkGeneratedFileContent(fileName);
}

@Test
void sUxForArrayWithWrongParam() {
var solution = buildSolution();

assertThrows(AssertionError.class, () ->
solution.sUx(BUILD_PATH + "sUx-arr.txt", new double[]{9.0, 11.0}));
assertThrows(AssertionError.class, () ->
solution.sUx(BUILD_PATH + "sUx-arr.txt", new double[]{10.0, 12.5, 13.5}));
}

@Test
void sUx() throws IOException {
var fileName = "sUx.txt";
var solution = buildSolution();

solution.sUx(BUILD_PATH + fileName, 12.0);

checkGeneratedFileContent(fileName);
}

@Test
void sUxWithWrongParam() {
var solution = buildSolution();

assertThrows(AssertionError.class, () -> solution.sUx(BUILD_PATH + "sUx.txt", 9.0));
assertThrows(AssertionError.class, () -> solution.sUx(BUILD_PATH + "sUx.txt", 13.5));
}

@Test
void gUtForDouble() {
var solution = buildSolution();

var result = solution.gUt(5.0);

checkUtAssertions(result);
}

@Test
void gUtForInt() {
var solution = buildSolution();

var result = solution.gUt(2);

checkUtAssertions(result);
}

@Test
void gUtForIntWithWrongParam() {
var solution = buildSolution();

assertThrows(AssertionError.class, () -> solution.gUt(-1));
assertThrows(AssertionError.class, () -> solution.gUt(solution.solution().getM()));
}

@Test
void gUxForDouble() {
var solution = buildSolution();

var result = solution.gUx(10 + 3 / 4.);

checkUxAssertions(result);
}

@Test
void gUxForInt() {
var solution = buildSolution();

var result = solution.gUx(1);

checkUxAssertions(result);
}

@Test
void gUxForIntWithWrongParam() {
var solution = buildSolution();

assertThrows(AssertionError.class, () -> solution.gUx(-1));
assertThrows(AssertionError.class, () -> solution.gUx(solution.solution().getN()));
}

/**
* <pre>
* Build solution for
* - space interval [10,13] with 3 steps &
* - time interval [2,6] with 2 steps
* - matrix:
* [[50,80,90,99],
* [30,70,120,125],
* [45,56,78,786]]
* </pre>
*/
private Solution<Equation> buildSolution() {
var spaceSteps = 4;
var timeSteps = 3;

var solutionMatrix = new Matrix(timeSteps, spaceSteps);
solutionMatrix.set(0, new double[]{50, 80, 90, 99});
solutionMatrix.set(1, new double[]{30, 70, 120, 125});
solutionMatrix.set(2, new double[]{45, 56, 78, 786});

return new Solution<>(null, new Area(
new Interval(10, 13, spaceSteps),
new Interval(2, 6, timeSteps)
), solutionMatrix);
}

private static void checkUtAssertions(Matrix result) {
assertThat(result.getM()).isEqualTo(2);
assertThat(result.getN()).isEqualTo(4);
assertThat(result.x(0)).isEqualTo(10);
assertThat(result.x(1)).isEqualTo(10 + 3 / 4.);
assertThat(result.x(2)).isEqualTo(10 + 6 / 4.);
assertThat(result.x(3)).isEqualTo(10 + 9 / 4.);
assertThat(result.y(0)).isEqualTo(45);
assertThat(result.y(1)).isEqualTo(56);
assertThat(result.y(2)).isEqualTo(78);
assertThat(result.y(3)).isEqualTo(786);
}

private static void checkUxAssertions(Matrix result) {
assertThat(result.getM()).isEqualTo(2);
assertThat(result.getN()).isEqualTo(3);
assertThat(result.x(0)).isEqualTo(2);
assertThat(result.x(1)).isEqualTo(2 + 4 / 3.);
assertThat(result.x(2)).isEqualTo(2 + 8 / 3.);
assertThat(result.y(0)).isEqualTo(80);
assertThat(result.y(1)).isEqualTo(70);
assertThat(result.y(2)).isEqualTo(56);
}
}
4 changes: 4 additions & 0 deletions src/test/resources/sUt-array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
10.0 50.0 45.0
10.75 80.0 56.0
11.5 90.0 78.0
12.25 99.0 786.0
4 changes: 4 additions & 0 deletions src/test/resources/sUt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
10.0 45.0
10.75 56.0
11.5 78.0
12.25 786.0
3 changes: 3 additions & 0 deletions src/test/resources/sUx-array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2.0 80.0 90.0
3.333333333333333 70.0 120.0
4.666666666666666 56.0 78.0
3 changes: 3 additions & 0 deletions src/test/resources/sUx.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2.0 90.0
3.333333333333333 120.0
4.666666666666666 78.0

0 comments on commit 57bab0c

Please sign in to comment.