Exemplo n.º 1
0
 def test_modify_weights_invalid_operator(self):
     """
     Tests modifyWeightsErorrCheck with an invalid operator.
     """
     bd = Board(30, 30)
     with self.assertRaises(ValueError):
         bd.modifyWeightsErrorCheck('^')
Exemplo n.º 2
0
 def test_check_number_int(self):
     """
     Tests checkNumber using an int.
     """
     try:
         Board.checkNumber(5)
     except ValueError:
         self.fail(msg="checkNumber failed to identify an int")
Exemplo n.º 3
0
 def test_check_number_float(self):
     """
     Tests checkNumber using an float.
     """
     try:
         Board.checkNumber(5.04)
     except ValueError:
         self.fail(msg="checkNumber failed to identify a float")
Exemplo n.º 4
0
 def test_set_weight_too_small(self):
     """
     Tests setting a node's weight to a value under the minimum (0).
     """
     bd = Board(30, 30)
     coord = [0, 5]
     bd.setWeight(coord, -105)
     self.assertEqual(bd.getWeight(coord), 0)
Exemplo n.º 5
0
 def test_check_int_positive(self):
     """
     Tests checkInt using an int.
     """
     try:
         Board.checkInt(5)
     except ValueError:
         self.fail(msg="checkInt failed to identify an int")
Exemplo n.º 6
0
 def test_set_weight_too_large(self):
     """
     Tests setting a node's weight to a value over the maximum (100).
     """
     bd = Board(30, 30)
     coord = [0, 5]
     bd.setWeight(coord, 105)
     self.assertEqual(bd.getWeight(coord), 100)
Exemplo n.º 7
0
    def test_init_sizing(self):
        """
        Tests the Board init function sizing.
        """
        width = 45
        height = 45
        bd = Board(width, height)

        self.assertEqual(bd.getSize(), [width, height])
Exemplo n.º 8
0
 def test_unique_weights_true(self):
     """
     Tests to confirm positive result when node weight is unique.
     """
     bd = Board(30, 30)
     coord = [5, 15]
     weight = 85
     bd.setWeight(coord, weight)
     self.assertEqual(bd.isNodeWeightUnique(coord), True)
Exemplo n.º 9
0
 def test_unique_weights_stress_test(self):
     """
     Stress test finding a unique weight on a board with a large size.
     """
     bd = Board(500, 500)
     coord = [5, 15]
     weight = 51
     bd.setWeight(coord, weight)
     self.assertEqual(bd.isNodeWeightUnique(coord), True)
Exemplo n.º 10
0
 def test_unique_weights_false(self):
     """
     Tests to confirm negative result with more than 1 of the same weight across all nodes.
     """
     bd = Board(30, 30)
     coords = [[5, 15], [25, 3]]
     weight = 85
     for coord in coords:
         bd.setWeight(coord, weight)
     self.assertEqual(bd.isNodeWeightUnique(coords[0]), False)
Exemplo n.º 11
0
 def test_init_weighting(self):
     """
     Tests the Board init function initial weighting.
     """
     width = 40
     height = 40
     bd = Board(width, height)
     for x in range(width):
         for y in range(height):
             self.assertEqual(bd.getWeight([x, y]), 50)
Exemplo n.º 12
0
 def test_count_nodes_with_weight(self):
     """
     Tests counting of nodes that have given weight.
     """
     bd = Board(30, 30)
     weight = 65
     coords = [[5, 5], [17, 7], [19, 9], [29, 16], [5, 14]]
     for coord in coords:
         bd.setWeight(coord, weight)
     self.assertEqual(bd.countNodeWeightCopies(coords[0]), len(coords))
Exemplo n.º 13
0
 def test_get_priority_nodes(self):
     """
     Tests getting 5 nodes of highest priority.
     """
     bd = Board(30, 30)
     coords = [[5, 5], [17, 7], [19, 9], [29, 16], [5, 14]]
     weights = [51, 55, 60, 65, 70]
     for coord, weight in zip(coords, weights):
         bd.setWeight(coord, weight)
     # Returns lowest priority first
     self.assertEqual(bd.getNodesWithPriority(0, 4), coords[::-1])
Exemplo n.º 14
0
 def test_set_and_get_node_weight(self):
     """
     Tests setting and getting node weight.
     """
     bd = Board(30, 30)
     coords = [[12, 4], [0, 27], [8, 12], [4, 16], [1, 0], [8, 26], [3, 10], [22, 5], [24, 14],\
      [4, 17], [1, 7], [27, 2], [21, 25], [10, 22], [25, 12]]
     weights = [54, 82, 42, 12, 32, 95, 87, 0, 53, 89, 10, 34, 15, 46, 72]
     for coord, weight in zip(coords, weights):
         bd.setWeight(coord, weight)
         self.assertEqual(bd.getWeight(coord), weight)
Exemplo n.º 15
0
 def test_unique_weights(self):
     """
     Tests to confirm many weights can be unique on a board.
     """
     bd = Board(30, 30)
     weights = [60, 39, 67, 46, 61, 77, 62, 47, 13, 79, 66, 4, 58, 86, 49]
     coords = [[11, 21], [10, 22], [20, 26], [17, 3], [12, 13], [16, 13], [16, 3], [19, 29],\
      [27, 21], [13, 11], [18, 14], [2, 6], [26, 27], [12, 23], [28, 29]]
     for coord, weight in zip(coords, weights):
         bd.setWeight(coord, weight)
     for coord in coords:
         self.assertEqual(bd.isNodeWeightUnique(coord), True)
Exemplo n.º 16
0
 def test_check_node_getting_setting(self):
     """
     Tests checkNode for valid nodes.
     """
     height = 30
     width = 30
     bd = Board(width, height)
     for x in range(width):
         for y in range(height):
             try:
                 bd.checkNode([x, y])
             except ValueError:
                 self.fail(msg="Valid node raised ValueError")
Exemplo n.º 17
0
    def test_get_priority_node(self):
        """
        Tests getting node of highest priority.
        """
        bd = Board(30, 30)
        coords = [[5, 5], [17, 7], [19, 9], [29, 16], [5, 14]]
        weights = [96, 55, 65, 75, 83]
        for coord, weight in zip(coords, weights):
            bd.setWeight(coord, weight)

        for i in range(len(coords)):
            priority = coords[weights.index(sorted(weights)[::-1][i])] # test all weighted squares
            self.assertEqual(bd.getNodeWithPriority(i), priority)
Exemplo n.º 18
0
 def test_optimum_path_length(self):
     """
     Tests for total length of optimum path.
     Path is expected to be `ideal_path`.
     """
     bd = Board(5, 5)
     coords = [[1, 2], [1, 3], [2, 3], [3, 3], [3, 2]]
     start = [0, 2]
     end = [4, 2]
     ideal_path = [start, end, [1, 2], [2, 2], [3, 2]]
     weight = 60
     for coord in coords:
         bd.setWeight(coord, weight)
     path_length = bd.optimumPathLength(start, end)
     self.assertEqual(path_length, (len(ideal_path)))
Exemplo n.º 19
0
    def __init__(self, data):
        """
        Initialize the Game class.

        param1: dict - all data from /start POST.
        """
        self.width = data['width']
        self.height = data['height']
        self.board = Board(self.width, self.height)

        self.snakes = {}
        self.us = ''
        self.food = []
        self.turn = 0
        self.machine = None
        self.processor = None
Exemplo n.º 20
0
    def test_wall_connected_to_both_sides(self):
        """
        Make a wall can reach enclosed spaces on both sides.
        """
        board = Board(10, 10)
        dset = DisjointSet(board)
        enclosedSpace = [[0, 0], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0]]
        wall = [[0, 1], [1, 2], [2, 2], [3, 1], [4, 0]]
        
        board.setWeights(wall, 0) # disconnect top left

        dset.update()
        self.assertEqual(sorted(dset.getConnectedToNode([0,0])), enclosedSpace[1:])

        for coord in wall:
            self.assertTrue(dset.pathExistsFromWall(coord, [0, 0]))
Exemplo n.º 21
0
    def test_no_optimum_path(self):
        """
        Tests a board which has no optimum path.
        """
        bd = Board(5, 5)
        coords = [[0, 1], [1, 0], [1, 1]]
        start = [2, 0]
        end = [0, 0]
        ideal_path = None
        weight = 0
        for coord in coords:
            bd.setWeight(coord, weight)
        path = bd.optimumPath(start, end)

        # Confirm returned path is same as ideal path from start to end (inclusive)
        self.assertEqual(ideal_path, path)
Exemplo n.º 22
0
 def test_check_node_invalid_format(self):
     """
     Tests checkNode for invalid formats.
     """
     bd = Board(30, 30)
     self.assertRaises(ValueError, bd.checkNode, ['test', 'test'])
     self.assertRaises(ValueError, bd.checkNode, [[1], [2]])
     self.assertRaises(ValueError, bd.checkNode, [{1}, {2}])
Exemplo n.º 23
0
    def test_path_within_bounds(self):
        """
        Make sure a path exists inside an enclosed space.
        """
        board = Board(10, 10)
        dset = DisjointSet(board)
        enclosedSpace = [[0, 0], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0]]
        wall = [[0, 1], [1, 2], [2, 2], [3, 2], [3, 1], [4, 0]]
        
        board.setWeights(wall, 0) # disconnect top left

        dset.update()
        self.assertEqual(sorted(dset.getConnectedToNode([0,0])), enclosedSpace[1:])

        for coord1 in enclosedSpace:
            for coord2 in enclosedSpace:
                self.assertTrue(dset.pathExistsFromNode(coord1, coord2))
Exemplo n.º 24
0
 def test_init_width_too_small(self):
     """
     Tests initilising board that has a width too small.
     """
     width = 1
     height = 20
     with self.assertRaises(ValueError):
         Board(width, height)
Exemplo n.º 25
0
 def test_init_height_too_small(self):
     """
     Tests initilising board that has a height too small.
     """
     width = 20
     height = 1
     with self.assertRaises(ValueError):
         Board(width, height)
Exemplo n.º 26
0
    def test_optimum_path_by_weight(self):
        """
        Tests a board which has no optimal path by length, but does by weight.
        Path is expected to be `coords`.
        """
        bd = Board(5, 5)
        coords = [[0, 1], [0, 0], [1, 0], [2, 0], [3, 0]]
        start = [0, 2]
        end = [4, 0]
        ideal_path = [start] + coords + [end]
        weight = 75
        for coord in coords:
            bd.setWeight(coord, weight)
        path = bd.optimumPath(start, end)

        # Confirm returned path is same as ideal path from start to end (inclusive)
        self.assertEqual(path, ideal_path)
Exemplo n.º 27
0
    def test_optimum_path_many_coords(self):
        """
        Tests a board which has a path obviously much shorter by length, but not as highly weighted.
        Path is expected to be `ideal_path`.
        """
        bd = Board(5, 5)
        coords = [[0, 1], [0, 2], [0, 3], [0, 4], [1, 4], [2, 4], [2, 3], [2, 2], [2, 1]]
        start = [0, 0]
        end = [2, 0]
        ideal_path = [start, [1, 0], end]
        weight = 60
        for coord in coords:
            bd.setWeight(coord, weight)
        path = bd.optimumPath(start, end)

        # Confirm returned path is same as ideal path from start to end (inclusive)
        self.assertEqual(ideal_path, path)
Exemplo n.º 28
0
    def test_optimum_path_longer_by_count(self):
        """
        Tests a board which has a shortest path that is shorter than the path by optimal weight.
        Path is expected to be `ideal_path`.
        """
        bd = Board(5, 5)
        coords = [[1, 2], [1, 3], [2, 3], [3, 3], [3, 2]]
        start = [0, 2]
        end = [4, 2]
        ideal_path = [start, [1, 2], [2, 2], [3, 2], end]
        weight = 60
        for coord in coords:
            bd.setWeight(coord, weight)
        path = bd.optimumPath(start, end)

        # Confirm returned path is same as ideal path from start to end (inclusive)
        self.assertEqual(ideal_path, path)
Exemplo n.º 29
0
    def test_no_path_across_wall(self):
        """
        Make sure no path exists outside of an enclosed space.
        """
        board = Board(10, 10)
        dset = DisjointSet(board)
        enclosedSpace = [[0, 0], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0]]
        wall = [[0, 1], [1, 2], [2, 2], [3, 2], [3, 1], [4, 0]]
        
        board.setWeights(wall, 0) # disconnect top left

        dset.update()
        self.assertEqual(sorted(dset.getConnectedToNode([0,0])), enclosedSpace[1:])

        for x in range(board.width):
            for y in range(board.height):
                if [x, y] not in enclosedSpace and [x, y] not in wall:
                    for coord in enclosedSpace:
                        self.assertFalse(dset.pathExistsFromNode(coord, [x, y]))
Exemplo n.º 30
0
 def test_reset_node_weights(self):
     """
     Tests resetting node weights to default value (50.0).
     """
     bd = Board(30, 30)
     weights = [60, 39, 67, 46, 61, 77, 61, 47, 13, 79, 66, 4, 58, 86, 49]
     coords = [[11, 21], [10, 22], [20, 26], [17, 3], [12, 13], [16, 13], [16, 3], [19, 29],\
      [27, 21], [13, 11], [18, 14], [2, 6], [26, 27], [12, 23], [28, 29]]
     for coord, weight in zip(coords, weights):
         bd.setWeight(coord, weight)
     bd.resetWeights()
     for coord in coords:
         failMsg = "%2.1F != %2.1f at %s" % (bd.getWeight(coord), 0, coord)
         self.assertEqual(bd.getWeight(coord), 50, msg=failMsg)