aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java60
-rw-r--r--src/test/resources/maps/island.txt10
2 files changed, 70 insertions, 0 deletions
diff --git a/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java b/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java
new file mode 100644
index 0000000..3af4679
--- /dev/null
+++ b/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java
@@ -0,0 +1,60 @@
1package fr.umlv.java.wallj.board;
2
3import org.junit.jupiter.api.Assertions;
4import org.junit.jupiter.api.Test;
5
6import java.io.IOException;
7import java.net.URISyntaxException;
8import java.nio.file.Path;
9import java.nio.file.Paths;
10import java.util.List;
11
12/**
13 * @author Pacien TRAN-GIRARD
14 */
15final class PathFinderTest {
16
17 private Path getResourcePath(String str) throws URISyntaxException {
18 return Paths.get(getClass().getResource(str).toURI());
19 }
20
21 private boolean isPathConnected(List<TileVec2> path) {
22 for (int i = 1; i < path.size(); ++i) {
23 TileVec2 predecessor = path.get(i - 1), current = path.get(i);
24
25 if (Math.abs(predecessor.getCol() - current.getCol()) > 1 ||
26 Math.abs(predecessor.getRow() - current.getRow()) > 1) return false;
27
28 if (Math.abs(predecessor.getCol() - current.getCol()) == 1 &&
29 Math.abs(predecessor.getRow() - current.getRow()) == 1) return false;
30 }
31
32 return true;
33 }
34
35 @Test
36 void testFailImpossibleFindPath() throws URISyntaxException, IOException {
37 Board board = BoardParser.parse(getResourcePath("/maps/island.txt"));
38 PathFinder pathFinder = new PathFinder(board);
39
40 Assertions.assertThrows(IllegalArgumentException.class, () -> {
41 pathFinder.findPath(TileVec2.of(7, 3), TileVec2.of(16, 5)); // into a wall
42 });
43 }
44
45 @Test
46 void testFindPath() throws URISyntaxException, IOException {
47 Board board = BoardParser.parse(getResourcePath("/maps/island.txt"));
48 PathFinder pathFinder = new PathFinder(board);
49
50 TileVec2 origin = TileVec2.of(7, 3);
51 TileVec2 target = TileVec2.of(33, 4);
52 List<TileVec2> path = pathFinder.findPath(origin, target);
53
54 Assertions.assertEquals(path.get(0), origin);
55 Assertions.assertEquals(path.get(path.size() - 1), target);
56 Assertions.assertTrue(isPathConnected(path));
57 Assertions.assertTrue(path.stream().allMatch(v -> board.getBlockTypeAt(v).isTraversable()));
58 }
59
60}
diff --git a/src/test/resources/maps/island.txt b/src/test/resources/maps/island.txt
new file mode 100644
index 0000000..9febab7
--- /dev/null
+++ b/src/test/resources/maps/island.txt
@@ -0,0 +1,10 @@
1WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
2W W
3W W
4W W W
5W WWWWW W
6W WWWWWWW W
7W WWWWWWW W
8W WWW W
9W W
10WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW \ No newline at end of file