def testOneRoute(self): adjList = AdjacencyList() addingRoute = Route(orig = 1, dest = 2, distance = 20) adjList.add(addingRoute) queue = adjList.getPQ() onlyRoute = queue.get() self.assertEqual(addingRoute, onlyRoute) self.assertTrue(queue.empty())
def testMultipleNeighbors(self): adjList = AdjacencyList() newRoute1 = Route(orig = 1, dest = 2, distance = 567) newRoute2 = Route(orig = 1, dest = 77, distance = 99) newRoute3 = Route(orig = 1, dest = 3222, distance = 0.54) adjList.add(newRoute1) adjList.add(newRoute2) adjList.add(newRoute3) self.assertEqual(adjList.findAllNeighbors(1), [2, 77, 3222])
def testAddBeyondFirst(self): adjList = AdjacencyList() newRoute1 = Route(orig = 1, dest = 2, distance = 567) adjList.add(newRoute1) newRoute2 = Route(orig = 2, dest = 3, distance = 784) adjList.add(newRoute2) self.assertEqual(adjList.findRoutePair(newRoute1.orig, newRoute1.dest), newRoute1) self.assertEqual(adjList.findRoutePair(newRoute2.orig, newRoute2.dest), newRoute2)
def testMatchingRoute(self): adjList = AdjacencyList() route1 = Route(orig = 1, dest = 2, distance = 20) route2 = Route(orig = 1, dest = 3, distance = 40) adjList.add(route1) adjList.add(route2) adjList = adjList.removeFromList(orig = 1, dest = 2) self.assertIsNone(adjList.findRoutePair(orig = 1, dest = 2)) self.assertEqual(adjList.findRoutePair(orig = 1, dest = 3), route2)
def testAddFirstRoute(self): adjList = AdjacencyList() newRoute = Route(orig = 1, dest = 2, distance = 567) self.assertIsNone(adjList.findRoutePair(newRoute.orig, newRoute.dest)) self.assertTrue(adjList.add(newRoute)) routeFoundInList = adjList.findRoutePair(newRoute.orig, newRoute.dest) self.assertIsNotNone(routeFoundInList) self.assertEqual(routeFoundInList.orig, newRoute.orig) self.assertEqual(routeFoundInList.dest, newRoute.dest) self.assertEqual(routeFoundInList.distance, newRoute.distance)
def readRouteData(fileObject): adjList = AdjacencyList() queue = PriorityQueue() while True: s = fileObject.readline() if not s: fileObject.close() break s = s[0:-1] split = re.split("\s+", s) try: adjList.add(Route(int(split[0]), int(split[1]), float(split[2]))) adjList.add(Route(int(split[1]), int(split[0]), float(split[2]))) queue.put(Route(int(split[0]), int(split[1]), float(split[2]))) except: fileObject.close() break fileObject.close() return adjList, queue
def testMultipleRoutes(self): adjList = AdjacencyList() addRoute1 = Route(orig = 1, dest = 2, distance = 20) addRoute2 = Route(orig = 2, dest = 3, distance = 50) addRoute3 = Route(orig = 2, dest = 4, distance = 30) #Sorted by ascending distance routesByDist = [addRoute1, addRoute3, addRoute2] adjList.add(addRoute1) adjList.add(addRoute2) adjList.add(addRoute3) queue = adjList.getPQ() #Match the routes and ordering of those routes for i in range(0, len(routesByDist)): pqEntry = queue.get() self.assertEqual(pqEntry, routesByDist[i]) #There should be any additional routes in the PQ. self.assertTrue(queue.empty())
def load(cities, cityMap, cityMapInvert, numberOfCities, fileName, list, distPQ, pricePQ): file = open(fileName, 'r+') numberOfCities = int(file.readline()) #read how many cities there are cities = [] cityMap = {} cityMapInvert = {} list = AdjacencyList() for i in range (1, numberOfCities+1): #read the names of the cities s = file.readline()[0:-1] cities.append(s) cityMap[s] = int(i) cityMapInvert[i] = s distPQ = DistPQ() pricePQ = PricePQ() while True: #read the route information s = file.readline() if not s: file.close() break s = s[0:-1] split = re.split('\s+', s) try: list.add(Route(int(split[0]), int(split[1]), float(split[2]), float(split[3]))) list.add(Route(int(split[1]), int(split[0]), float(split[2]), float(split[3]))) distPQ.add(Route(int(split[0]), int(split[1]), float(split[2]), float(split[3]))) pricePQ.add(Route(int(split[0]), int(split[1]), float(split[2]), float(split[3]))) except: file.close() break return cities, cityMap, cityMapInvert, numberOfCities, fileName, list, distPQ, pricePQ
def testMoreExpensivePath(self): numCities = 3 menu = Menu() cities = ["Honolulu", "Tokyo", "Los Angeles"] cityMap = {'Honolulu': 1, "Tokyo": 2, "Los Angeles": 3} cityMapInvert = {1: "Honolulu", 2: "Tokyo", 3: "Los Angeles"} choice = 3 #2 is distance, 3 is segments queue = PriorityQueue() adjList = AdjacencyList() #Note HNL-Tokyo is 500, while HNL-LA-Tokyo is 450 hnlToTokyo = Route(orig = 1, dest = 2, distance = 500) hnlToLA = Route(orig = 1, dest = 3, distance = 350) LAtoTokyo = Route(orig = 3, dest = 2, distance = 100) queue.put(hnlToTokyo) queue.put(hnlToLA) queue.put(LAtoTokyo) adjList.add(hnlToTokyo) adjList.add(hnlToLA) adjList.add(LAtoTokyo) self.assertEqual(dijkstra(numCities, cities, cityMap, queue, choice, cityMapInvert, adjList, menu, userInput = ["Honolulu", "Tokyo"]), ["Honolulu", "Tokyo"])
def testOneNeighbor(self): adjList = AdjacencyList() newRoute = Route(orig = 1, dest = 2, distance = 567) adjList.add(newRoute) self.assertEqual(adjList.findAllNeighbors(1), [2])
def testNonMatchingRoute(self): adjList = AdjacencyList() adjList.add(Route(orig = 1, dest = 2, distance = 20)) self.assertEqual(adjList.removeFromList(orig = 3, dest = 4), adjList)
def testAddNone(self): adjList = AdjacencyList() self.assertFalse(adjList.add(None))