001 package aima.test.search.map; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import junit.framework.TestCase; 007 import aima.search.map.ExtendableMap; 008 009 /** 010 * @author Ciaran O'Reilly 011 * 012 */ 013 014 public class MapTest extends TestCase { 015 016 ExtendableMap aMap; 017 018 @Override 019 public void setUp() { 020 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 028 public void testLocationsLinkedTo() { 029 ArrayList<String> locations = new ArrayList<String>(); 030 List<String> linkedTo; 031 032 linkedTo = aMap.getLocationsLinkedTo("A"); 033 locations.clear(); 034 locations.add("B"); 035 locations.add("C"); 036 assertTrue(locations.containsAll(linkedTo) && linkedTo.size() == 2); 037 038 linkedTo = aMap.getLocationsLinkedTo("B"); 039 locations.clear(); 040 locations.add("A"); 041 locations.add("C"); 042 locations.add("E"); 043 assertTrue(locations.containsAll(linkedTo) && linkedTo.size() == 3); 044 045 linkedTo = aMap.getLocationsLinkedTo("C"); 046 locations.clear(); 047 locations.add("A"); 048 locations.add("B"); 049 locations.add("D"); 050 assertTrue(locations.containsAll(linkedTo) && linkedTo.size() == 3); 051 052 linkedTo = aMap.getLocationsLinkedTo("D"); 053 locations.clear(); 054 locations.add("C"); 055 assertTrue(locations.containsAll(linkedTo) && linkedTo.size() == 1); 056 057 linkedTo = aMap.getLocationsLinkedTo("E"); 058 assertTrue(linkedTo.size() == 0); 059 } 060 061 public void testDistances() { 062 assertEquals(new Double(5), aMap.getDistance("A", "B")); 063 assertEquals(new Double(6), aMap.getDistance("A", "C")); 064 assertEquals(new Double(4), aMap.getDistance("B", "C")); 065 assertEquals(new Double(7), aMap.getDistance("C", "D")); 066 assertEquals(new Double(14), aMap.getDistance("B", "E")); 067 // 068 assertEquals(new Double(5), aMap.getDistance("B", "A")); 069 assertEquals(new Double(6), aMap.getDistance("C", "A")); 070 assertEquals(new Double(4), aMap.getDistance("C", "B")); 071 assertEquals(new Double(7), aMap.getDistance("D", "C")); 072 073 // No distances should be returned if links not established or locations 074 // do not exist 075 assertNull(aMap.getDistance("X", "Z")); 076 assertNull(aMap.getDistance("A", "Z")); 077 assertNull(aMap.getDistance("A", "E")); 078 // B->E is unidirectional so should not have opposite direction 079 assertNull(aMap.getDistance("E", "B")); 080 } 081 082 public void testRandomGeneration() { 083 ArrayList<String> locations = new ArrayList<String>(); 084 locations.add("A"); 085 locations.add("B"); 086 locations.add("C"); 087 locations.add("D"); 088 locations.add("E"); 089 090 for (int i = 0; i < locations.size(); i++) { 091 assertTrue(locations.contains(aMap.randomlyGenerateDestination())); 092 } 093 } 094 }