Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #6
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))
Beispiel #7
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])
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
0
    def test_multiply_weight_float(self):
        """
        Tests multiplying node weight.
        """
        fraction = 0.5
        bd = Board(30, 30)
        coords = [[15, 15], [2, 7], [9, 25]]
        weights = [20, 2, 100]

        for coord, weight in zip(coords, weights):
            bd.setWeight(coord, weight)
            bd.multiplyWeight(coord, fraction)
            self.assertEqual(bd.getWeight(coord), weight * fraction)
Beispiel #11
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)
Beispiel #12
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)
Beispiel #13
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)))
Beispiel #14
0
    def test_divide_weight(self):
        """
        Tests dividing node weight using an even divisor.
        """
        divisor = 4
        bd = Board(30, 30)
        coords = [[15, 15], [2, 7], [9, 25]]
        weights = [20, 0, 99]

        for coord, weight in zip(coords, weights):
            bd.setWeight(coord, weight)
            bd.divideWeight(coord, divisor)

        for coord, weight in zip(coords, weights):
            self.assertEqual(bd.getWeight(coord), weight // divisor)
Beispiel #15
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)
Beispiel #16
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)
Beispiel #17
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)
Beispiel #18
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)
Beispiel #19
0
    def test_multiply_weight_int(self):
        """
        Tests multiplying node weight.
        """
        multiplier = 5
        bd = Board(30, 30)
        coords = [[15, 15], [2, 7], [9, 25]]
        weights = [6, 14, 90]

        for coord, weight in zip(coords, weights):
            bd.setWeight(coord, weight)
            bd.multiplyWeight(coord, multiplier)

        for coord, weight in zip(coords, weights):
            desiredWeight = weight * multiplier
            if desiredWeight < -100:
                desiredWeight = -100
            elif desiredWeight > 100:
                desiredWeight = 100
            self.assertEqual(bd.getWeight(coord), desiredWeight)
Beispiel #20
0
    def test_add_weight(self):
        """
        Tests adding to node weight.
        """
        addend = 35
        bd = Board(30, 30)
        coords = [[15, 15], [2, 7], [9, 25]]
        weights = [20, -99, 99]

        for coord, weight in zip(coords, weights):
            bd.setWeight(coord, weight)
            bd.addWeight(coord, addend)

        for coord, weight in zip(coords, weights):
            desiredWeight = weight + addend
            if desiredWeight > 100:
                desiredWeight = 100
            elif desiredWeight < 0:
                desiredWeight = addend
            self.assertEqual(bd.getWeight(coord), desiredWeight)
Beispiel #21
0
    def test_subtract_weight(self):
        """
        Tests subtracting from node weight.
        """
        subtrahend = 23
        bd = Board(30, 30)
        coords = [[15, 15], [2, 7], [9, 25]]
        weights = [20, -99, 99]

        for coord, weight in zip(coords, weights):
            bd.setWeight(coord, weight)
            bd.subtractWeight(coord, subtrahend)

        for coord, weight in zip(coords, weights):
            desiredWeight = weight - subtrahend
            if desiredWeight > 100:
                desiredWeight = 100
            elif desiredWeight < 0:
                desiredWeight = 0
            self.assertEqual(bd.getWeight(coord), desiredWeight)
Beispiel #22
0
    def test_multiply_many_nodes(self):
        """
        Tests multiplying many nodes weights.
        """
        multiplier = 2
        bd = Board(30, 30)
        coords = [[15, 15], [2, 7], [9, 25]]
        weights = [20, -99, 99]

        for coord, weight in zip(coords, weights):
            bd.setWeight(coord, weight)

        bd.modifyWeights('*', coords, multiplier)

        for coord, weight in zip(coords, weights):
            desiredWeight = weight * multiplier
            if desiredWeight > 100:
                desiredWeight = 100
            elif desiredWeight < 0:
                desiredWeight = 0
            self.assertEqual(bd.getWeight(coord), desiredWeight)
Beispiel #23
0
    def test_divide_many_weights(self):
        """
        Tests dividing many nodes weights using an odd divisor.
        """
        divisor = 3
        bd = Board(30, 30)
        coords = [[15, 15], [2, 7], [9, 25]]
        weights = [21, -99, 99]

        for coord, weight in zip(coords, weights):
            bd.setWeight(coord, weight)

        bd.modifyWeights('/', coords, divisor)

        for coord, weight in zip(coords, weights):
            desiredWeight = weight // divisor
            if desiredWeight > 100:
                desiredWeight = 100
            elif desiredWeight < 0:
                desiredWeight = 0
            self.assertAlmostEqual(bd.getWeight(coord), desiredWeight, places=4)