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 89740bb commit d6dff33
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 71 deletions.
40 changes: 23 additions & 17 deletions src/graderPrivate/java/h06/H3_MazeSolverRecursiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,7 @@ 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 die Startpunkt korrekt "
+ "enthält.")
@DisplayName("21 | solve(World, Point, Point, Direction) gibt ein Array zurück, das den Startpunkt enthält.")
@JsonClasspathSource(value = {
"MazeSolver/solve/path_complex1.json",
"MazeSolver/solve/path_complex2.json",
Expand All @@ -584,20 +583,20 @@ public void testContainsStart(
}
MethodLink method = getMethod();
World world = properties.createWorld(TestWorld::new);
Point[] actual = solver.solve(world, s, e, d);

Point[] path = solver.solve(world, s, e, d);
Point actual = Arrays.stream(path).filter(p -> p.equals(s)).findFirst().orElse(null);
Context context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
.add("e", s)
.add("d", d)
.add("Expected", expected[0])
.add("Actual", actual[0])
.add("Expected", s)
.add("Actual", actual)
.build();

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

/**
Expand All @@ -611,8 +610,7 @@ 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 korrekt "
+ "enthält.")
@DisplayName("22 | solve(World, Point, Point, Direction) gibt ein Array zurück, das den Endpunkt enthält.")
@JsonClasspathSource(value = {
"MazeSolver/solve/path_complex1.json",
"MazeSolver/solve/path_complex2.json",
Expand All @@ -631,20 +629,20 @@ public void testContainsEnd(
}
MethodLink method = getMethod();
World world = properties.createWorld(TestWorld::new);
Point[] actual = solver.solve(world, s, e, d);

Point[] path = solver.solve(world, s, e, d);
Point actual = Arrays.stream(path).filter(p -> p.equals(e)).findFirst().orElse(null);
Context context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
.add("e", s)
.add("d", d)
.add("Expected", expected[expected.length - 1])
.add("Actual", actual[actual.length - 1])
.add("Expected", e)
.add("Actual", actual)
.build();

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

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

for (int i = 1; i < expected.length - 1; i++) {
int i = 0;
int end = actual.length - 1;
if (actual[i].equals(s)) {
i++;
}
if (actual[end].equals(e)) {
end--;
}
for (; i <= end; i++) {
Context context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
Expand Down
34 changes: 22 additions & 12 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 "
+ "korrekt enthält.")
+ "enthält.")
@JsonClasspathSource(value = {
"MazeSolver/solve/path_complex1.json",
"MazeSolver/solve/path_complex2.json",
Expand All @@ -510,25 +510,28 @@ public void testContainsStartEnd(
}
MethodLink method = getMethod();
World world = properties.createWorld(TestWorld::new);
Point[] actual = solver.solve(world, s, e, d);
Point[] path = solver.solve(world, s, e, d);

Point actualStart = Arrays.stream(path).filter(p -> p.equals(s)).findFirst().orElse(null);

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

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

context.add("Expected", expected[expected.length - 1])
.add("Actual", actual[actual.length - 1]);
assertEquals(expected[0], actual[0], context.build(),
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(),
result -> "MazeSolverIterative#solve(%s, %s, %s, %s) should contain the end point %s, but was %s"
.formatted(world, s, e, d, expected[expected.length - 1], actual[actual.length - 1]));
.formatted(world, s, e, d, e, actualEnd));
}

/**
Expand Down Expand Up @@ -564,8 +567,15 @@ public void testContainsAll(
MethodLink method = getMethod();
World world = properties.createWorld(TestWorld::new);
Point[] actual = solver.solve(world, s, e, d);

for (int i = 1; i < expected.length - 1; i++) {
int i = 0;
int end = actual.length - 1;
if (actual[i].equals(s)) {
i++;
}
if (actual[end].equals(e)) {
end--;
}
for (; i <= end; i++) {
Context context = contextBuilder().subject(method)
.add(buildWorldContext(properties))
.add("s", s)
Expand Down
3 changes: 3 additions & 0 deletions src/graderPrivate/java/h06/TutorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
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 @@ -237,6 +239,7 @@ 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
57 changes: 15 additions & 42 deletions src/main/java/h06/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package h06;

import h06.problems.MazeSolverRecursive;
import h06.problems.ProblemSolver;
import h06.ui.MazeVisualizer;
import h06.ui.ProblemVisualizer;
import h06.world.DirectionVector;
import h06.world.World;

import java.awt.Point;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Main entry point in executing the program.
Expand All @@ -19,39 +14,17 @@ public class Main {
*
* @param args program arguments, currently ignored
*/
public static void main(String[] args) {
System.out.println("Hello World!");

World world = new World(5, 5);

world.placeWall(0, 0, false);
world.placeWall(0, 1, false);
world.placeWall(0, 2, false);
world.placeWall(0, 3, false);
world.placeWall(1, 3, false);
world.placeWall(1, 4, false);
world.placeWall(2, 0, false);
world.placeWall(3, 1, false);
world.placeWall(3, 3, false);

world.placeWall(1, 1, true);
world.placeWall(2, 0, true);
world.placeWall(2, 1, true);
world.placeWall(2, 2, true);
world.placeWall(3, 1, true);
world.placeWall(3, 3, true);
world.placeWall(4, 2, true);


ProblemVisualizer visualizer = new MazeVisualizer();
ProblemSolver solver = new MazeSolverRecursive();
visualizer.init(world);
visualizer.show();

Point start = new Point(2, 0);
Point end = new Point(2, 4);
DirectionVector direction = DirectionVector.UP;

visualizer.run(solver, start, end, direction);
public static void main(String[] args) throws IOException {
var path = "/home/nyanyan/Downloads/rubrics_H06/rubrics/csv"; // "Gesamt,32,18,"
Files.list(Path.of(path)).filter(p -> {
try {
var lines = Files.lines(p).toList();
var last = lines.get(lines.size() - 1);
var parts = last.split(",");
return parts[2].equals("0") || parts[2].equals("1") || parts[2].equals("2");
} catch (IOException e) {
throw new RuntimeException(e);
}
}).forEach(System.out::println);
}
}

0 comments on commit d6dff33

Please sign in to comment.