Tested DFS search algorithm.

This commit is contained in:
2020-04-20 17:34:24 -05:00
parent a3024f6e91
commit 81fb18ab3d
4 changed files with 129 additions and 44 deletions

View File

@@ -1,38 +0,0 @@
package ca.recrown.islandsurvivalcraft;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@@ -0,0 +1,103 @@
package ca.recrown.islandsurvivalcraft.pathfinding;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class DepthFirstSearchTest extends TestCase
{
private class Validator implements CoordinateValidatable {
private boolean[][] map;
public Validator(boolean[][] map) {
this.map = map;
}
@Override
public boolean validate(int x, int y) {
try {
return map[y][x];
} catch (IndexOutOfBoundsException e) {
return false;
}
}
}
private boolean[][] mapA = new boolean[][] {
{true, true},
{true, false},
{true, true}
};
private boolean[][] mapB = new boolean[][] {
{true, true, true, true},
{true, false, false, false},
{true, true, true, true}
};
private boolean[][] mapC = new boolean[][] {
{true, true, true, true},
{false, false, false, false},
{true, true, true, true}
};
private boolean[][] mapD = new boolean[][] {
{true, true, true, true},
{false, false, false, true},
{true, true, true, true},
{true, false, false, false},
};
/**
* Create the test case
*
* @param testName name of the test case
*/
public DepthFirstSearchTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( DepthFirstSearchTest.class );
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testDFSMapAValid()
{
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapA));
dfs.Setup(1, 0, 1, 2);
assertTrue(dfs.buildTree());
}
public void testDFSMapBValid()
{
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapB));
dfs.Setup(3, 2, 3, 0);
assertTrue(dfs.buildTree());
}
public void testDFSMapDValid()
{
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapD));
dfs.Setup(0, 3, 0, 0);
assertTrue(dfs.buildTree());
}
public void testDFSInvalid()
{
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapC));
dfs.Setup(3, 2, 3, 0);
assertFalse(dfs.buildTree());
}
}