2019-03-27 18:59:28 +00:00
|
|
|
package search;
|
2019-03-24 13:36:10 +00:00
|
|
|
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
class EightPuzzleNodeTest
|
|
|
|
{
|
|
|
|
@Test
|
|
|
|
public void shouldReturnTrueWhenTargetReached()
|
|
|
|
{
|
2019-04-02 18:34:15 +00:00
|
|
|
final int[][] state = {
|
2019-03-24 13:36:10 +00:00
|
|
|
{1, 2, 3},
|
|
|
|
{4, 5, 6},
|
|
|
|
{7, 8, 0}
|
|
|
|
};
|
|
|
|
|
2019-04-02 18:34:15 +00:00
|
|
|
final var node = new EightPuzzleNode(state);
|
|
|
|
final var target = new EightPuzzleNode(state);
|
2019-03-24 13:36:10 +00:00
|
|
|
|
|
|
|
Assertions.assertTrue(node.isTargetReached(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldReturnFalseWhenTargetNotReached()
|
|
|
|
{
|
2019-04-02 18:34:15 +00:00
|
|
|
final int[][] actualState = {
|
2019-03-24 13:36:10 +00:00
|
|
|
{7, 1, 6},
|
|
|
|
{0, 4, 2},
|
|
|
|
{3, 5, 8}
|
|
|
|
};
|
|
|
|
|
2019-04-02 18:34:15 +00:00
|
|
|
final int[][] targetState = {
|
2019-03-24 13:36:10 +00:00
|
|
|
{1, 2, 3},
|
|
|
|
{4, 5, 6},
|
|
|
|
{7, 8, 0}
|
|
|
|
};
|
|
|
|
|
2019-04-02 18:34:15 +00:00
|
|
|
final var node = new EightPuzzleNode(actualState);
|
|
|
|
final var target = new EightPuzzleNode(targetState);
|
2019-03-24 13:36:10 +00:00
|
|
|
|
|
|
|
Assertions.assertFalse(node.isTargetReached(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldReturnNonEmptyListOfSuccessors()
|
|
|
|
{
|
2019-04-02 18:34:15 +00:00
|
|
|
final int[][] state = {
|
2019-03-24 13:36:10 +00:00
|
|
|
{7, 1, 6},
|
|
|
|
{0, 4, 2},
|
|
|
|
{3, 5, 8}
|
|
|
|
};
|
|
|
|
|
2019-04-02 18:34:15 +00:00
|
|
|
final var node = new EightPuzzleNode(state);
|
|
|
|
final var successors = node.generateSuccessors();
|
2019-03-24 13:36:10 +00:00
|
|
|
|
|
|
|
Assertions.assertFalse(successors.isEmpty());
|
|
|
|
}
|
|
|
|
|
2019-03-24 15:41:14 +00:00
|
|
|
@Test
|
|
|
|
public void shouldReturnCorrectSuccessors()
|
|
|
|
{
|
2019-04-02 18:34:15 +00:00
|
|
|
final int[][] state = {
|
2019-03-24 15:41:14 +00:00
|
|
|
{7, 1, 6},
|
|
|
|
{0, 4, 2},
|
|
|
|
{3, 5, 8}
|
|
|
|
};
|
|
|
|
|
2019-04-02 18:34:15 +00:00
|
|
|
final var node = new EightPuzzleNode(state);
|
|
|
|
final var successors = node.generateSuccessors();
|
2019-03-24 15:41:14 +00:00
|
|
|
|
|
|
|
Assertions.assertEquals(3, successors.size());
|
|
|
|
|
2019-04-02 18:34:15 +00:00
|
|
|
final int[][] newState1 = {
|
2019-03-24 15:41:14 +00:00
|
|
|
{0, 1, 6},
|
|
|
|
{7, 4, 2},
|
|
|
|
{3, 5, 8}
|
|
|
|
};
|
|
|
|
|
2019-04-02 18:34:15 +00:00
|
|
|
final int[][] newState2 = {
|
2019-03-24 15:41:14 +00:00
|
|
|
{7, 1, 6},
|
|
|
|
{4, 0, 2},
|
|
|
|
{3, 5, 8}
|
|
|
|
};
|
|
|
|
|
2019-04-02 18:34:15 +00:00
|
|
|
final int[][] newState3 = {
|
2019-03-24 15:41:14 +00:00
|
|
|
{7, 1, 6},
|
|
|
|
{3, 4, 2},
|
|
|
|
{0, 5, 8}
|
|
|
|
};
|
|
|
|
|
2019-03-25 18:14:17 +00:00
|
|
|
Assertions.assertArrayEquals(newState1, successors.get(0).getValue());
|
|
|
|
Assertions.assertArrayEquals(newState2, successors.get(1).getValue());
|
|
|
|
Assertions.assertArrayEquals(newState3, successors.get(2).getValue());
|
2019-03-24 15:41:14 +00:00
|
|
|
}
|
2019-03-24 13:36:10 +00:00
|
|
|
}
|