Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zentox committed Dec 21, 2023
1 parent d6dff33 commit 4961805
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 deletions.
40 changes: 17 additions & 23 deletions src/graderPrivate/java/h06/H3_MazeSolverRecursiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ public void testCorrectArraySize(
* @param node the expected array
*/
@ParameterizedTest(name = "Startpunkt: {1}, Endpunkt: {2}, Richtung: {3}")
@DisplayName("21 | solve(World, Point, Point, Direction) gibt ein Array zurück, das den Startpunkt enthält.")
@DisplayName("21 | solve(World, Point, Point, Direction) gibt ein Array zurück, das die Startpunkt korrekt "
+ "enthält.")
@JsonClasspathSource(value = {
"MazeSolver/solve/path_complex1.json",
"MazeSolver/solve/path_complex2.json",
Expand All @@ -583,20 +584,20 @@ public void testContainsStart(
}
MethodLink method = getMethod();
World world = properties.createWorld(TestWorld::new);
Point[] path = solver.solve(world, s, e, d);
Point actual = Arrays.stream(path).filter(p -> p.equals(s)).findFirst().orElse(null);
Point[] actual = solver.solve(world, s, e, d);

Context context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
.add("e", s)
.add("d", d)
.add("Expected", s)
.add("Actual", actual)
.add("Expected", expected[0])
.add("Actual", actual[0])
.build();

assertEquals(s, actual, context,
assertEquals(expected[0], actual[0], context,
result -> "MazeSolverIterative#solve(%s, %s, %s, %s) should contain the start point %s, but was %s."
.formatted(world, s, e, d, s, actual));
.formatted(world, s, e, d, expected[0], actual[0]));
}

/**
Expand All @@ -610,7 +611,8 @@ public void testContainsStart(
* @param node the expected array
*/
@ParameterizedTest(name = "Startpunkt: {1}, Endpunkt: {2}, Richtung: {3}")
@DisplayName("22 | solve(World, Point, Point, Direction) gibt ein Array zurück, das den Endpunkt enthält.")
@DisplayName("22 | solve(World, Point, Point, Direction) gibt ein Array zurück, das den Endpunkt korrekt "
+ "enthält.")
@JsonClasspathSource(value = {
"MazeSolver/solve/path_complex1.json",
"MazeSolver/solve/path_complex2.json",
Expand All @@ -629,20 +631,20 @@ public void testContainsEnd(
}
MethodLink method = getMethod();
World world = properties.createWorld(TestWorld::new);
Point[] path = solver.solve(world, s, e, d);
Point actual = Arrays.stream(path).filter(p -> p.equals(e)).findFirst().orElse(null);
Point[] actual = solver.solve(world, s, e, d);

Context context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
.add("e", s)
.add("d", d)
.add("Expected", e)
.add("Actual", actual)
.add("Expected", expected[expected.length - 1])
.add("Actual", actual[actual.length - 1])
.build();

assertEquals(e, actual, context,
assertEquals(expected[expected.length - 1], actual[actual.length - 1], context,
result -> "MazeSolverIterative#solve(%s, %s, %s, %s) should contain the end point %s, but was %s."
.formatted(world, s, e, d, e, actual));
.formatted(world, s, e, d, expected[expected.length - 1], actual[actual.length - 1]));
}

/**
Expand Down Expand Up @@ -679,15 +681,7 @@ public void testContainsAll(
World world = properties.createWorld(TestWorld::new);
Point[] actual = solver.solve(world, s, e, d);

int i = 0;
int end = actual.length - 1;
if (actual[i].equals(s)) {
i++;
}
if (actual[end].equals(e)) {
end--;
}
for (; i <= end; i++) {
for (int i = 1; i < expected.length - 1; i++) {
Context context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
Expand Down
34 changes: 12 additions & 22 deletions src/graderPrivate/java/h06/H4_MazeSolverIterativeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public void testCorrectArraySize(
*/
@ParameterizedTest(name = "Startpunkt: {1}, Endpunkt: {2}, Richtung: {3}")
@DisplayName("37 | solve(World, Point, Point, Direction) gibt ein Array zurück, das die Start- und Endpunkt "
+ "enthält.")
+ "korrekt enthält.")
@JsonClasspathSource(value = {
"MazeSolver/solve/path_complex1.json",
"MazeSolver/solve/path_complex2.json",
Expand All @@ -510,28 +510,25 @@ public void testContainsStartEnd(
}
MethodLink method = getMethod();
World world = properties.createWorld(TestWorld::new);
Point[] path = solver.solve(world, s, e, d);

Point actualStart = Arrays.stream(path).filter(p -> p.equals(s)).findFirst().orElse(null);
Point[] actual = solver.solve(world, s, e, d);

Context.Builder<?> context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
.add("e", e)
.add("d", d)
.add("Expected start", s)
.add("Actual start", actualStart);
.add("Expected", expected[0])
.add("Actual", actual[0]);

assertEquals(s, actualStart, context.build(),
assertEquals(expected[0], actual[0], context.build(),
result -> "MazeSolverIterative#solve(%s, %s, %s, %s) should contain the start point %s, but was %s"
.formatted(world, s, e, d, s, actualStart));
.formatted(world, s, e, d, expected[0], actual[0]));

Point actualEnd = Arrays.stream(path).filter(p -> p.equals(e)).findFirst().orElse(null);
context.add("Expected end", e)
.add("Actual end", actualEnd);
assertEquals(e, actualEnd, context.build(),
context.add("Expected", expected[expected.length - 1])
.add("Actual", actual[actual.length - 1]);
assertEquals(expected[0], actual[0], context.build(),
result -> "MazeSolverIterative#solve(%s, %s, %s, %s) should contain the end point %s, but was %s"
.formatted(world, s, e, d, e, actualEnd));
.formatted(world, s, e, d, expected[expected.length - 1], actual[actual.length - 1]));
}

/**
Expand Down Expand Up @@ -567,15 +564,8 @@ public void testContainsAll(
MethodLink method = getMethod();
World world = properties.createWorld(TestWorld::new);
Point[] actual = solver.solve(world, s, e, d);
int i = 0;
int end = actual.length - 1;
if (actual[i].equals(s)) {
i++;
}
if (actual[end].equals(e)) {
end--;
}
for (; i <= end; i++) {

for (int i = 1; i < expected.length - 1; i++) {
Context context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
Expand Down
3 changes: 0 additions & 3 deletions src/graderPrivate/java/h06/TutorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.tudalgo.algoutils.tutor.general.assertions.Assertions2.assertFalse;
Expand Down Expand Up @@ -239,7 +237,6 @@ public static boolean isRecursive(CtMethod<?> method) {
.list()
.stream()
.map(it -> it instanceof CtInvocation<?> ? ((CtInvocation<?>) it).getExecutable() : null)
.filter(Predicate.not(Objects::isNull))
.toList();
if (!method.filterChildren(it -> it instanceof CtLoop).list().isEmpty()) {
return false;
Expand Down

0 comments on commit 4961805

Please sign in to comment.