diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java | 60 | ||||
-rw-r--r-- | src/test/resources/maps/island.txt | 10 |
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 @@ | |||
1 | package fr.umlv.java.wallj.board; | ||
2 | |||
3 | import org.junit.jupiter.api.Assertions; | ||
4 | import org.junit.jupiter.api.Test; | ||
5 | |||
6 | import java.io.IOException; | ||
7 | import java.net.URISyntaxException; | ||
8 | import java.nio.file.Path; | ||
9 | import java.nio.file.Paths; | ||
10 | import java.util.List; | ||
11 | |||
12 | /** | ||
13 | * @author Pacien TRAN-GIRARD | ||
14 | */ | ||
15 | final 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 @@ | |||
1 | WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW | ||
2 | W W | ||
3 | W W | ||
4 | W W W | ||
5 | W WWWWW W | ||
6 | W WWWWWWW W | ||
7 | W WWWWWWW W | ||
8 | W WWW W | ||
9 | W W | ||
10 | WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW \ No newline at end of file | ||