Added testclass and implemented methods

This commit is contained in:
birkn 2019-03-24 14:36:10 +01:00
parent 58e8aa9cd0
commit d014b9f686
2 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,62 @@
package search.breadthfirstsearch;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
public class EightPuzzleNode implements Node
{
private int[][] state;
public EightPuzzleNode(int[][] state) {
if (!isValidState(state))
{
throw new IllegalArgumentException("Allowed numbers are only 0-8 and they must exist uniquely.");
}
this.state = state;
}
@Override
public boolean isTargetReached(Node target) {
return Arrays.equals(this.state, ((EightPuzzleNode) target).state);
}
@Override
public List<Node> generateSuccessors() {
ArrayList<Node> successors = new ArrayList<>();
successors.add(new EightPuzzleNode(null));
return successors;
}
private boolean isValidState(int[][] state)
{
int[] numbers = Arrays.stream(state).flatMapToInt(IntStream::of).toArray();
int[] countOfNumbers = new int[9];
for (int i : numbers)
{
try
{
countOfNumbers[i]++;
}
catch (ArrayIndexOutOfBoundsException e)
{
return false;
}
}
for (int i : countOfNumbers)
{
if (i == 0 || i > 1)
{
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,87 @@
package search.breadthfirstsearch;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class EightPuzzleNodeTest
{
@Test
public void shouldThrowExceptionWhileStateHasDuplicateNumbers()
{
int[][] state = {
{1, 1, 3},
{4, 5, 6},
{7, 8, 0}
};
Assertions.assertThrows(IllegalArgumentException.class, () -> new EightPuzzleNode(state));
}
@Test
public void shouldThrowExceptionWhileStateHasNumbersOutOfRange()
{
int[][] state = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Assertions.assertThrows(IllegalArgumentException.class, () -> new EightPuzzleNode(state));
}
@Test
public void shouldReturnTrueWhenTargetReached()
{
int[][] state = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
var node = new EightPuzzleNode(state);
var target = new EightPuzzleNode(state);
Assertions.assertTrue(node.isTargetReached(target));
}
@Test
public void shouldReturnFalseWhenTargetNotReached()
{
int[][] actualState = {
{7, 1, 6},
{0, 4, 2},
{3, 5, 8}
};
int[][] targetState = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
var node = new EightPuzzleNode(actualState);
var target = new EightPuzzleNode(targetState);
Assertions.assertFalse(node.isTargetReached(target));
}
@Test
public void shouldReturnNonEmptyListOfSuccessors()
{
int[][] state = {
{7, 1, 6},
{0, 4, 2},
{3, 5, 8}
};
var node = new EightPuzzleNode(state);
var successors = node.generateSuccessors();
Assertions.assertFalse(successors.isEmpty());
}
}