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