240 lines
8.2 KiB
Java
240 lines
8.2 KiB
Java
package net.reslate.islandsurvivalcraft.utilities.pathfinding;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.Scanner;
|
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.TestInstance;
|
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
|
|
|
import net.reslate.islandsurvivalcraft.utilities.datatypes.Point2;
|
|
|
|
/**
|
|
* Unit test for simple App.
|
|
*/
|
|
@TestInstance(Lifecycle.PER_CLASS)
|
|
public class DepthFirstSearchTest {
|
|
|
|
private class Validator implements CoordinateValidatable {
|
|
private byte[][] map;
|
|
|
|
public Validator(byte[][] map) {
|
|
this.map = map;
|
|
}
|
|
|
|
@Override
|
|
public boolean validate(Point2 point) {
|
|
try {
|
|
return map[point.y][point.x] >= 1;
|
|
} catch (IndexOutOfBoundsException e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private class TargetValidator implements CoordinateValidatable {
|
|
private byte[][] map;
|
|
|
|
public TargetValidator(byte[][] map) {
|
|
this.map = map;
|
|
}
|
|
|
|
@Override
|
|
public boolean validate(Point2 point) {
|
|
try {
|
|
return map[point.y][point.x] == 2;
|
|
} catch (IndexOutOfBoundsException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private byte[][] mapA;
|
|
private byte[][] mapB;
|
|
private byte[][] mapC;
|
|
private byte[][] mapD;
|
|
private byte[][] mapE;
|
|
private byte[][] mapF;
|
|
private byte[][] mapG;
|
|
private byte[][] mapH;
|
|
|
|
@BeforeAll
|
|
public void setUp() throws IOException {
|
|
mapA = readMap("DFSTestMapSmallA.txt");
|
|
mapB = readMap("DFSTestMapSmallB.txt");
|
|
mapC = readMap("DFSTestMapSmallC.txt");
|
|
mapD = readMap("DFSTestMapSmallD.txt");
|
|
mapE = readMap("DFSTestMapSmallE.txt");
|
|
|
|
mapF = readMap("DFSTestMapLargeA.txt");
|
|
mapG = readMap("DFSTestMapLargeB.txt");
|
|
mapH = readMap("DFSTestMapLargeC.txt");
|
|
}
|
|
|
|
private byte[][] readMap(String filename) throws IOException {
|
|
byte[][] map = null;
|
|
InputStream stream = getClass().getClassLoader().getResourceAsStream((filename));
|
|
Scanner scanner = new Scanner(stream);
|
|
String line = null;
|
|
ArrayList<byte[]> rows = new ArrayList<>();
|
|
while (scanner.hasNextLine()) {
|
|
line = scanner.nextLine();
|
|
char[] chars = line.toCharArray();
|
|
byte[] rowBytes = new byte[chars.length];
|
|
for (int i = 0; i < chars.length; i++) {
|
|
rowBytes[i] = (byte) Character.getNumericValue(chars[i]);
|
|
}
|
|
rows.add(rowBytes);
|
|
}
|
|
scanner.close();
|
|
map = new byte[rows.size()][rows.get(0).length];
|
|
for (int row = 0; row < rows.size(); row++) {
|
|
map[row] = rows.get(row);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
@Test
|
|
public void testDFSBuildPathToEndNodeMapAValid()
|
|
{
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(1, 2), new Point2(1, 0));
|
|
dfs.buildToGoal(new Validator(mapA), null);
|
|
assertTrue(dfs.getResult() != null);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSBuildPathToEndNodeMapBValid()
|
|
{
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(3, 2));
|
|
dfs.buildToGoal(new Validator(mapB), null);
|
|
assertTrue(dfs.getResult() != null);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSBuildPathToEndNodeMapDValid()
|
|
{
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 3));
|
|
dfs.buildToGoal(new Validator(mapD), null);
|
|
assertTrue(dfs.getResult() != null);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSBuildPathToEndNodeMapCInvalid()
|
|
{
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(3, 2));
|
|
dfs.buildToGoal(new Validator(mapC), null);
|
|
assertFalse(dfs.getResult() != null);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSBuildPathToEndNodeMapEInvalid()
|
|
{
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(3, 2));
|
|
dfs.buildToGoal(new Validator(mapE), null);
|
|
assertFalse(dfs.getResult() != null);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapBValid() {
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(0, 0));
|
|
Validator validator = new Validator(mapB);
|
|
TargetValidator targetValidator = new TargetValidator(mapB);
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
assertTrue(dfs.getResult() != null);
|
|
assertEquals(1, dfs.getResult().x);
|
|
assertEquals(2, dfs.getResult().y);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapDValid() {
|
|
Validator validator = new Validator(mapD);
|
|
TargetValidator targetValidator = new TargetValidator(mapD);
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 0));
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
assertTrue(dfs.getResult() != null);
|
|
assertEquals(0, dfs.getResult().x);
|
|
assertEquals(3, dfs.getResult().y);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapHValid() {
|
|
Validator validator = new Validator(mapH);
|
|
TargetValidator targetValidator = new TargetValidator(mapH);
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(0, 0));
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
|
|
assertTrue(dfs.getResult() != null);
|
|
assertEquals(95, dfs.getResult().x);
|
|
assertEquals(49, dfs.getResult().y);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapHInvalidLimited() {
|
|
Validator validator = new Validator(mapH);
|
|
TargetValidator targetValidator = new TargetValidator(mapH);
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(0, 0), null, 128);
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
assertFalse(dfs.getResult() != null);
|
|
}
|
|
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapEInvalid() {
|
|
Validator validator = new Validator(mapE);
|
|
TargetValidator targetValidator = new TargetValidator(mapE);
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 0), null, 128);
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
assertFalse(dfs.getResult() != null);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapFValid() {
|
|
Validator validator = new Validator(mapF);
|
|
TargetValidator targetValidator = new TargetValidator(mapF);
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 0), null, 1024);
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
assertTrue(dfs.getResult() != null);
|
|
assertEquals(26, dfs.getResult().x);
|
|
assertEquals(32, dfs.getResult().y);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapGInvalid() {
|
|
Validator validator = new Validator(mapG);
|
|
TargetValidator targetValidator = new TargetValidator(mapG);
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 0), null, 128);
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
assertFalse(dfs.getResult() != null);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapHWithAssistValid() {
|
|
Validator validator = new Validator(mapH);
|
|
TargetValidator targetValidator = new TargetValidator(mapH);
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(95, 49), null, 0);
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
assertTrue(dfs.getResult() != null);
|
|
assertEquals(95, dfs.getResult().x);
|
|
assertEquals(49, dfs.getResult().y);
|
|
}
|
|
|
|
@Test
|
|
public void testDFSFindEndNodeMapHWithoutAssistValid() {
|
|
Validator validator = new Validator(mapH);
|
|
TargetValidator targetValidator = new TargetValidator(mapH);
|
|
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(0, 0), null, 0);
|
|
dfs.buildToGoal(validator, targetValidator);
|
|
assertTrue(dfs.getResult() != null);
|
|
assertEquals(95, dfs.getResult().x);
|
|
assertEquals(49, dfs.getResult().y);
|
|
}
|
|
}
|