001    package aima.test.search.searches;
002    
003    import java.util.List;
004    
005    import junit.framework.TestCase;
006    import aima.search.framework.GraphSearch;
007    import aima.search.framework.Problem;
008    import aima.search.framework.Search;
009    import aima.search.framework.SearchAgent;
010    import aima.search.nqueens.NQueensBoard;
011    import aima.search.nqueens.NQueensGoalTest;
012    import aima.search.nqueens.NQueensSuccessorFunction;
013    import aima.search.uninformed.DepthFirstSearch;
014    
015    public class DepthFirstSearchTest extends TestCase {
016    
017            public void testDepthFirstSuccesfulSearch() throws Exception {
018                    Problem problem = new Problem(new NQueensBoard(8),
019                                    new NQueensSuccessorFunction(), new NQueensGoalTest());
020                    Search search = new DepthFirstSearch(new GraphSearch());
021                    SearchAgent agent = new SearchAgent(problem, search);
022                    List actions = agent.getActions();
023                    assertCorrectPlacement(actions);
024                    assertEquals("113", agent.getInstrumentation().getProperty(
025                                    "nodesExpanded"));
026    
027            }
028    
029            public void testDepthFirstUnSuccessfulSearch() throws Exception {
030                    Problem problem = new Problem(new NQueensBoard(3),
031                                    new NQueensSuccessorFunction(), new NQueensGoalTest());
032                    Search search = new DepthFirstSearch(new GraphSearch());
033                    SearchAgent agent = new SearchAgent(problem, search);
034                    List actions = agent.getActions();
035                    assertEquals(0, actions.size());
036                    assertEquals("6", agent.getInstrumentation().getProperty(
037                                    "nodesExpanded"));
038            }
039    
040            private void assertCorrectPlacement(List actions) {
041                    assertEquals(8, actions.size());
042                    assertEquals("placeQueenAt 0  7", actions.get(0));
043                    assertEquals("placeQueenAt 1  3", actions.get(1));
044                    assertEquals("placeQueenAt 2  0", actions.get(2));
045                    assertEquals("placeQueenAt 3  2", actions.get(3));
046                    assertEquals("placeQueenAt 4  5", actions.get(4));
047                    assertEquals("placeQueenAt 5  1", actions.get(5));
048                    assertEquals("placeQueenAt 6  6", actions.get(6));
049                    assertEquals("placeQueenAt 7  4", actions.get(7));
050            }
051    
052    }