def test_noPath(self): grid = SearchGrid(3, 1) grid.set(1, 0, INPENETRABLE) pathFinder = PathFinder(grid) foundPath = pathFinder.find((0, 0), (2, 0)) self.assertEqual(None, foundPath)
def test_directPathBlockedByImpenetrable(self): grid = SearchGrid(5, 5) grid.set(1, 0, INPENETRABLE) pathFinder = PathFinder(grid) foundPath = pathFinder.find((0, 0), (2, 0)) self.assertEqual([(0, 0), (0, 1), (1, 1), (2, 1), (2, 0)], foundPath)
def test_getAdjacentCellCoordinatesMiddle(self): grid = SearchGrid(5, 5) result = grid.getAdjacentCellCoordinates(1, 1) self.assertEqual(4, len(result)) self.assertTrue((0, 1) in result) self.assertTrue((2, 1) in result) self.assertTrue((1, 0) in result) self.assertTrue((1, 2) in result)
def generateSearchGridFromPlayfield(cellValueFunction=None): cellValueFunction = cellValueFunction or SearchGridGenerator.getSearchSpaceCellValueForTile grid = SearchGrid(playfield.width, playfield.height) SearchGridGenerator.generateTerrainBasedGrid(grid, cellValueFunction) SearchGridGenerator.accountForDoubleSize(grid) return grid
def test_processTask(self): task = Task(SearchGrid(2, 2), (0, 0), (1, 0)) worker.start() worker.queueTask(task) time.sleep(0.05) worker.stop() self.assertTrue(task.isCompleted()) self.assertEqual([(0, 0), (1, 0)], task.getPath())
def test_shortestPathGoesOverExtraCostCell(self): grid = SearchGrid(5, 5) grid.set(1, 0, EXTRACOST) grid.set(1, 1, INPENETRABLE) pathFinder = PathFinder(grid) foundPath = pathFinder.find((0, 0), (2, 0)) self.assertEqual([(0, 0), (1, 0), (2, 0)], foundPath)
def test_set(self): grid = SearchGrid(5, 5) grid.set(1, 1, 10) self.assertEqual(10, grid.get(1, 1))
def test_cellsAreZeroUponInitialization(self): grid = SearchGrid(5, 5) for x in range(grid.width): for y in range(grid.height): self.assertEqual(0, grid.get(x, y))
def test_sizeIsSet(self): grid = SearchGrid(5, 5) self.assertEqual(5, grid.width) self.assertEqual(5, grid.height)
def test_freePathMultipleSteps(self): pathFinder = PathFinder(SearchGrid(5, 5)) foundPath = pathFinder.find((0, 0), (3, 0)) self.assertEqual([(0, 0), (1, 0), (2, 0), (3, 0)], foundPath)
def test_getAdjacentCellCoordinatesBottomRight(self): grid = SearchGrid(5, 5) result = grid.getAdjacentCellCoordinates(4, 4) self.assertEqual(2, len(result)) self.assertTrue((4, 3) in result) self.assertTrue((3, 4) in result)
def test_getAdjacentCellCoordinatesTopLeft(self): grid = SearchGrid(5, 5) result = grid.getAdjacentCellCoordinates(0, 0) self.assertEqual(2, len(result)) self.assertTrue((1, 0) in result) self.assertTrue((0, 1) in result)
def test_startLocationIsSameAsEndLocation(self): pathFinder = PathFinder(SearchGrid(5, 5)) foundPath = pathFinder.find((0, 0), (0, 0)) self.assertEqual([(0, 0)], foundPath)
def test_containsCoordinatesInsideBounds(self): grid = SearchGrid(5, 5) self.assertTrue(grid.containsCoordinates(0, 0)) self.assertTrue(grid.containsCoordinates(4, 4))
def test_containsCoordinatesOutOfBounds(self): grid = SearchGrid(5, 5) self.assertFalse(grid.containsCoordinates(-1, 0)) self.assertFalse(grid.containsCoordinates(0, -1)) self.assertFalse(grid.containsCoordinates(5, 0)) self.assertFalse(grid.containsCoordinates(0, 5))