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)
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)
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)
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)
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)
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))
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])
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)
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)
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)
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)
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)
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)))
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)