예제 #1
0
class TestFindingPathEfficiency(unittest.TestCase):
    @max_time(10)
    def test_efficiency_on_blank_map_with_non_heura_algorythm(self):
        size_of_map = _FindPathProblem.MIN_DISTANCE_TO_USE_HEURA / 2 - 1
        self._test_efficiency_on_blank_map(size_of_map)
        assert (distance(self.source, self.destination) < \
                _FindPathProblem.MIN_DISTANCE_TO_USE_HEURA)

    @max_time(150)
    def test_efficiency_on_blank_map_with_heura_algorythm(self):
        size_of_map = 128
        self._test_efficiency_on_blank_map(size_of_map)
        assert (distance(self.source, self.destination) >= \
                _FindPathProblem.MIN_DISTANCE_TO_USE_HEURA)

    def _test_efficiency_on_blank_map(self, size_of_map):
        self.game_map = GameMap((size_of_map, size_of_map), [])
        self.source = size_of_map - 1, 0
        self.destination = 0, size_of_map - 1
        answered_direction = self._find_direction()
        self.assertTrue(answered_direction != None)

    def _find_direction(self):
        return self.game_map.find_direction(self.source, self.destination)
예제 #2
0
class TestFindingPath(unittest.TestCase):
    def test_destination_equal_to_source(self):
        self.game_map = GameMap((4, 4), ())
        self.source = self.destination = (3, 2)
        self._test_answer_equal_to(None)

    def test_destination_is_source_neighbour(self):
        self.game_map = GameMap((4, 4), ())
        self.source = (3, 2)
        self.destination = (3, 3)
        self._test_answer_equal_to(direction.S)

    def test_destination_is_unavaiable_but_its_neighbour_is_not(self):
        s = 'tt    \n' + \
            'ttttt \n' + \
            '      \n' + \
            ' ttttt\n' + \
            ' t    \n' + \
            '   tt*'
        self.game_map = self._create_game_map_from_text(s)
        self.destination = (1, 0)
        self._test_answer_equal_to(direction.N)

    def test_destination_is_far_far_away_but_is_avaiable(self):
        size = 128
        self.game_map = GameMap((size, size), ())
        self.source = 14, 0
        self.destination = 14 + size / 2, size - 1
        answered_direction = self._find_direction()
        self.assertTrue(answered_direction in (direction.E, direction.S))

    def test_destination_is_unavailable_nor_its_neighbours(self):
        s = ' t  \n' + \
            ' t  \n' + \
            '*t  \n' + \
            ' t^ '
        self.game_map = self._create_game_map_from_text(s)
        self._test_answer_equal_to(None)

    def test_road_block(self):
        s = '                         \n' + \
            '  ^                      \n' + \
            '                         \n' + \
            '                         \n' + \
            '                         \n' + \
            ' tt                      \n' + \
            ' t t                     \n' + \
            'tt tttttttttttttt        \n' + \
            '     ttttttttttttt       \n' + \
            '    ttttttttttttttt      \n' + \
            '      tttttttttttt       \n' + \
            '        tttttttttt       \n' + \
            '         ttttttttt       \n' + \
            '                tt       \n' + \
            '                tt       \n' + \
            '    *           tt       \n' + \
            '                tt       \n' + \
            '                tt       \n' + \
            '                         \n' + \
            '                         '
        self.game_map = self._create_game_map_from_text(s)
        answered_direction = self._find_direction()
        self.assertTrue(answered_direction in (direction.E, direction.S))

    def test_destination_is_unavailable_but_its_neighbours_are_not(self):
        s = '*t'
        self.destination = (1, 0)
        self.game_map = self._create_game_map_from_text(s)
        self._test_answer_equal_to(None)

    def test_destination_behind_border(self):
        self.game_map = GameMap((3, 3), ())
        self.destination = (3, 0)
        self.source = (2, 0)
        self._test_answer_equal_to(None)

    def test_skip_if_too_long_searching_time(self):
        assert _FindPathProblem.ITERATIONS_LIMIT == 256
        s = ' ' * 257
        self.game_map = self._create_game_map_from_text(s)
        self.source = (0, 0)
        self.destination = (256, 0)
        self._test_answer_equal_to(None)

    def _create_game_map_from_text(self, s):
        """ '*' means source and '^' - destination """

        split = s.split('\n')

        size_x, size_y = len(split[0]), len(split)

        game_map = GameMap((size_x, size_y), ())

        switch = {
            ' ': lambda field: None,
            't': lambda field: field.place_object(object()),
            '*': lambda field: setattr(self, 'source', field.position),
            '^': lambda field: setattr(self, 'destination', field.position),
        }
        for y, line in enumerate(split):
            for x, char in enumerate(line):
                case = switch[char]
                case(game_map[x, y])

        return game_map

    def _test_answer_equal_to(self, expected_direction):
        answered_direction = self._find_direction()
        self.assertEqual(expected_direction, answered_direction)

    def _find_direction(self):
        return self.game_map.find_direction(self.source, self.destination)