Beispiel #1
0
 def test_set_many_weights(self):
     """
     Tests setting many nodes' weight all at once.
     """
     bd = Board(30, 31)
     coords = [[26, 24], [29, 30], [19, 13], [0, 0]]
     weight = 75
     bd.setWeights(coords, weight)
     for coord in coords:
         if bd.getWeight(coord) != weight:
             self.fail(msg="Weight {:2.1f} != {:2.1f} for node {}".format(bd.getWeight(coord),\
                       weight, coord))
Beispiel #2
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 #3
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 #4
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 #5
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)
Beispiel #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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)
Beispiel #13
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)