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.nqueens.NQueensBoard; 010 import aima.search.nqueens.NQueensGoalTest; 011 import aima.search.nqueens.NQueensSuccessorFunction; 012 import aima.search.uninformed.DepthLimitedSearch; 013 014 public class DepthLimitedSearchTest extends TestCase { 015 016 public void testSuccesfulDepthLimitedSearch() throws Exception { 017 Problem problem = new Problem(new NQueensBoard(8), 018 new NQueensSuccessorFunction(), new NQueensGoalTest()); 019 Search search = new DepthLimitedSearch(8); 020 SearchAgent agent = new SearchAgent(problem, search); 021 List actions = agent.getActions(); 022 assertCorrectPlacement(actions); 023 assertEquals("113", agent.getInstrumentation().getProperty( 024 "nodesExpanded")); 025 026 } 027 028 public void testCutOff() throws Exception { 029 Problem problem = new Problem(new NQueensBoard(8), 030 new NQueensSuccessorFunction(), new NQueensGoalTest()); 031 Search search = new DepthLimitedSearch(1); 032 SearchAgent agent = new SearchAgent(problem, search); 033 List actions = agent.getActions(); 034 assertEquals("cutoff", actions.get(0)); 035 036 } 037 038 public void testFailure() throws Exception { 039 Problem problem = new Problem(new NQueensBoard(3), 040 new NQueensSuccessorFunction(), new NQueensGoalTest()); 041 Search search = new DepthLimitedSearch(5); 042 SearchAgent agent = new SearchAgent(problem, search); 043 List actions = agent.getActions(); 044 assertEquals(0, actions.size()); 045 } 046 047 private void assertCorrectPlacement(List actions) { 048 assertEquals(8, actions.size()); 049 assertEquals("placeQueenAt 0 0", actions.get(0)); 050 assertEquals("placeQueenAt 1 4", actions.get(1)); 051 assertEquals("placeQueenAt 2 7", actions.get(2)); 052 assertEquals("placeQueenAt 3 5", actions.get(3)); 053 assertEquals("placeQueenAt 4 2", actions.get(4)); 054 assertEquals("placeQueenAt 5 6", actions.get(5)); 055 assertEquals("placeQueenAt 6 1", actions.get(6)); 056 assertEquals("placeQueenAt 7 3", actions.get(7)); 057 } 058 059 }