001    /*
002     * Created on Sep 12, 2004
003     *
004     */
005    package aima.test.search.eightpuzzle;
006    
007    import java.util.List;
008    
009    import junit.framework.TestCase;
010    import aima.search.eightpuzzle.EightPuzzleBoard;
011    import aima.search.eightpuzzle.EightPuzzleSuccessorFunction;
012    import aima.search.framework.Successor;
013    
014    /**
015     * @author Ravi Mohan
016     * 
017     */
018    
019    public class EightPuzzleSuccessorFunctionTest extends TestCase {
020            EightPuzzleBoard board;
021    
022            EightPuzzleSuccessorFunction func = new EightPuzzleSuccessorFunction();
023    
024            @Override
025            public void setUp() {
026                    board = new EightPuzzleBoard(new int[] { 1, 2, 5, 3, 4, 0, 6, 7, 8 });
027            }
028    
029            public void testGenerateCorrect3successors() {
030                    List successors = func.getSuccessors(board);
031                    assertEquals(3, successors.size());
032    
033                    // test first successor
034                    EightPuzzleBoard expectedFirst = new EightPuzzleBoard(new int[] { 1, 2,
035                                    0, 3, 4, 5, 6, 7, 8 });
036                    EightPuzzleBoard actualFirst = (EightPuzzleBoard) ((Successor) successors
037                                    .get(0)).getState();
038                    assertEquals(expectedFirst, actualFirst);
039                    assertEquals(EightPuzzleBoard.UP, ((Successor) successors.get(0))
040                                    .getAction());
041    
042                    // test second successor
043                    EightPuzzleBoard expectedSecond = new EightPuzzleBoard(new int[] { 1,
044                                    2, 5, 3, 4, 8, 6, 7, 0 });
045                    EightPuzzleBoard actualSecond = (EightPuzzleBoard) ((Successor) successors
046                                    .get(1)).getState();
047                    assertEquals(expectedSecond, actualSecond);
048                    assertEquals(EightPuzzleBoard.DOWN, ((Successor) successors.get(1))
049                                    .getAction());
050    
051                    // test third successor
052                    EightPuzzleBoard expectedThird = new EightPuzzleBoard(new int[] { 1, 2,
053                                    5, 3, 0, 4, 6, 7, 8 });
054                    EightPuzzleBoard actualThird = (EightPuzzleBoard) ((Successor) successors
055                                    .get(2)).getState();
056                    assertEquals(expectedThird, actualThird);
057                    assertEquals(EightPuzzleBoard.LEFT, ((Successor) successors.get(2))
058                                    .getAction());
059            }
060    
061            public void testGenerateCorrectWhenGapMovedRightward() {
062                    board.moveGapLeft();// gives { 1, 2, 5, 3, 0, 4, 6, 7, 8 }
063                    assertEquals(new EightPuzzleBoard(
064                                    new int[] { 1, 2, 5, 3, 0, 4, 6, 7, 8 }), board);
065                    List successors = func.getSuccessors(board);
066                    assertEquals(4, successors.size());
067                    EightPuzzleBoard expectedFourth = new EightPuzzleBoard(new int[] { 1,
068                                    2, 5, 3, 4, 0, 6, 7, 8 });
069                    EightPuzzleBoard actualFourth = (EightPuzzleBoard) ((Successor) successors
070                                    .get(3)).getState();
071                    assertEquals(expectedFourth, actualFourth);
072                    assertEquals(EightPuzzleBoard.RIGHT, ((Successor) successors.get(3))
073                                    .getAction());
074    
075            }
076    
077    }