001    package aima.test.search.map;
002    
003    import java.util.ArrayList;
004    
005    import junit.framework.TestCase;
006    import aima.search.framework.Successor;
007    import aima.search.map.ExtendableMap;
008    import aima.search.map.MapSuccessorFunction;
009    
010    /**
011     * @author Ciaran O'Reilly
012     * 
013     */
014    
015    public class MapSuccessorFunctionTest extends TestCase {
016            MapSuccessorFunction msf;
017    
018            @Override
019            public void setUp() {
020                    ExtendableMap aMap = new ExtendableMap();
021                    aMap.addBidirectionalLink("A", "B", 5.0);
022                    aMap.addBidirectionalLink("A", "C", 6.0);
023                    aMap.addBidirectionalLink("B", "C", 4.0);
024                    aMap.addBidirectionalLink("C", "D", 7.0);
025                    aMap.addUnidirectionalLink("B", "E", 14.0);
026    
027                    msf = new MapSuccessorFunction(aMap);
028            }
029    
030            public void testSuccessors() {
031                    ArrayList<String> locations = new ArrayList<String>();
032    
033                    // A
034                    locations.clear();
035                    locations.add("B");
036                    locations.add("C");
037                    for (Object o : msf.getSuccessors("A")) {
038                            Successor s = (Successor) o;
039                            assertTrue(locations.contains(s.getAction())
040                                            && locations.contains(s.getState()));
041                    }
042    
043                    // B
044                    locations.clear();
045                    locations.add("A");
046                    locations.add("C");
047                    locations.add("E");
048                    for (Object o : msf.getSuccessors("B")) {
049                            Successor s = (Successor) o;
050                            assertTrue(locations.contains(s.getAction())
051                                            && locations.contains(s.getState()));
052                    }
053    
054                    // C
055                    locations.clear();
056                    locations.add("A");
057                    locations.add("B");
058                    locations.add("D");
059                    for (Object o : msf.getSuccessors("C")) {
060                            Successor s = (Successor) o;
061                            assertTrue(locations.contains(s.getAction())
062                                            && locations.contains(s.getState()));
063                    }
064    
065                    // D
066                    locations.clear();
067                    locations.add("C");
068                    for (Object o : msf.getSuccessors("D")) {
069                            Successor s = (Successor) o;
070                            assertTrue(locations.contains(s.getAction())
071                                            && locations.contains(s.getState()));
072                    }
073    
074                    // E
075                    locations.clear();
076                    assertTrue(0 == msf.getSuccessors("E").size());
077            }
078    }