def test_get_random_position(self): """Test get_random_position checks for distribution of positions and validity of positions """ width, height, dirt_amount = (5, 10, 1) room = ps3.RectangularRoom(width, height, dirt_amount) sol_room = test.RectangularRoom(width, height, dirt_amount) freq_buckets = {} for i in range(50000): pos = room.get_random_position() # confirm from test that this is a valid position self.assertTrue(sol_room.is_position_in_room(pos)) try: x, y = pos.get_x(), pos.get_y() except AttributeError: self.fail( "get_random_position returned {} which is not a Position". format(pos)) self.assertTrue( 0 <= x < width and 0 <= y < height, "get_random_position returned {} which is not in [0, {}), [0, {})" .format(pos, width, height)) x0, y0 = int(x), int(y) freq_buckets[(x0, y0)] = freq_buckets.get((x0, y0), 0) + 1 for t in xyrange(width, height): num_in_bucket = freq_buckets.get(t, 0) self.assertTrue( # This is a 99.7% confidence interval for a uniform # distribution. Fail if the total of any bucket falls outside # this range. 865 < num_in_bucket < 1135, "The distribution of positions from get_random_position " "looks incorrect (it should be uniform)")
def test_is_position_in_room(self): "Test is_position_in_room" width, height, dirt_amount = (3, 4, 2) room = ps3.RectangularRoom(width, height, dirt_amount) solution_room = test.RectangularRoom(width, height, dirt_amount) for x in [0.0, -0.1, width - 0.1, width, width + 0.1]: for y in [0.0, -0.1, height - 0.1, height, height + 0.1]: pos = test.Position(x, y) self.assertEqual( solution_room.is_position_in_room(pos), room.is_position_in_room(pos), "position {},{} is incorrect: expected {}, got {}".format( x, y, solution_room.is_position_in_room(pos), room.is_position_in_room(pos)))
def test_getset_robot_direction(self): """Test get_robot_direction and set_robot_direction""" # instantiate RectangularRoom from solutions for testing width, height, dirt_amount = (3, 4, 2) solution_room = test.RectangularRoom(width, height, dirt_amount) robots = [ps3.Robot(solution_room, 1.0, 1) for i in range(4)] directions = [1, 333, 105, 75, 74.3] for dir_index, robot in enumerate(robots): robot.set_robot_direction(directions[dir_index]) for dir_index, robot in enumerate(robots): robot_dir = robot.get_robot_direction() self.assertEqual( robot_dir, directions[dir_index], "Robot direction set or retrieved incorrectly: expected {}, got {}" .format(directions[dir_index], robot_dir))
def test_unimplemented_methods(self): """Test if student implemented methods in Robot abstract class that should not be implemented""" room = test.RectangularRoom(2, 2, 1) robot = ps3.Robot(room, 1, 1) self.assertRaises(NotImplementedError, robot.update_position_and_clean)