aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorpacien2018-01-14 00:51:13 +0100
committerpacien2018-01-14 00:51:13 +0100
commitf11d73b61282ca6a2f2574e7ad3d7d46f9ca1f52 (patch)
tree2bd19ae7d91dbfe0e3b5c7384f2b9b02433f98f0 /src/test/java
parent52a87e1d9ffb0b1a0ed45e3af71b0ccc9c05142a (diff)
downloadwallj-f11d73b61282ca6a2f2574e7ad3d7d46f9ca1f52.tar.gz
Implement path finder
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java60
1 files changed, 60 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}