def test_update_pos_and_cleanSimpleRobot(self): "Test SimpleRobot.update_pos_and_clean" r = ps3.iRoom(3, 5, 1) robot = ps3.SimpleRobot(r, 1.0, 1) robot.set_robot_position(ps3.Position(1.5, 2.5)) robot.set_robot_direction(90) robot.update_pos_and_clean() self.assertEqual(robot.get_robot_direction(), 90, "Robot direction is updated incorrectly by update_pos_and_clean: expected %r, got %r" % (90, robot.get_robot_direction())) # check if robot position is valid robotPos = robot.get_robot_position() correctPos = ps3.Position(2.5, 2.5) self.assertTrue(robotPos.get_x() == correctPos.get_x() and robotPos.get_y() == correctPos.get_y(), "Robot position is updated incorrectly by update_pos_and_clean: expected %r, got %r" % (ps3.Position(2.5, 2.5), robot.get_robot_position())) self.assertTrue(2>=r.get_num_cleaned_tiles() >= 1, "update_pos_and_clean should have marked one or two tiles as clean") self.assertTrue(r.is_tile_cleaned(1, 2) or r.is_tile_cleaned(2, 2), "update_pos_and_clean should have marked either (1, 2) or (2, 2) as clean") # Simulate a lot of time passing... for i in range(20): robot.update_pos_and_clean() self.assertTrue(r.is_position_in_room(robot.get_robot_position()), "Robot position %r is not in room!" % (robot.get_robot_position(),)) self.assertNotEqual(robot.get_robot_direction(), 90, "Robot direction should have been changed in update_pos_and_clean") self.assertTrue(r.get_num_cleaned_tiles() >= 1, "update_pos_and_clean should have marked another tile as clean")
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.iRoom(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(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 < 1150, "The distribution of positions from get_random_position " "looks incorrect (it should be uniform)")
def test_is_tile_cleaned_clean(self): """ Test is_tile_cleaned""" width, height, dirt_amount = (3, 4, 0) room = ps3.iRoom(width, height, dirt_amount) # Check all squares are unclean at start, given initial dirt > 1 for x, y in xyrange(width, height): self.assertTrue( room.is_tile_cleaned(x, y), "Unclean tile {} was returned as clean".format((x, y)))
def test_room_dirt_clean(self): """ Can fail either because get_dirt_amount is working incorrectly OR the student is initializing the dirt amount incorrectly """ width, height, dirt_amount = (3, 4, 0) room = ps3.iRoom(width, height, dirt_amount) for x, y in xyrange(width, height): self.assertEqual(room.get_dirt_amount(x, y),dirt_amount, "Tile {} was not initialized with correct dirt amount".format((x, y)) )
def test_clean_tile_at_position_ZeroToZero(self): """ Test if clean_tile_at_position removes all dirt""" width, height, dirt_amount = (3, 4, 0) room = ps3.iRoom(width, height, dirt_amount) # Clean the tiles and confirm they are marked as clean for x, y in xyrange(width, height): room.clean_tile_at_position(ps3.Position(x + random.random(), y + random.random()), 1) # using random.random in case there is any issue with specific parts of a tile for x, y in xyrange(width, height): self.assertTrue(room.is_tile_cleaned(x, y), "Clean tile {} was marked clean, no negative dirt allowed".format((x, y)) )
def test_is_position_in_room(self): "Test is_position_in_room" width, height, dirt_amount = (3, 4, 2) room = ps3.iRoom(width, height, dirt_amount) sols = [True, False,True,False,False,False,False,False,False,False,True,False,True,False,False,False,False,False,False,False,False,False,False,False,False] count = 0 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 = ps3.Position(x, y) self.assertEqual(sols[count],room.is_position_in_room(pos), "position {},{} is incorrect: expected {}, got {}".format(x, y, sols[count], room.is_position_in_room(pos))) count += 1
def test_get_num_cleaned_tiles_Partial(self): "Test get_num_cleaned_tiles for cleaning subset of room incompletely" width, height, dirt_amount = (3, 4, 2) room = ps3.iRoom(width, height, dirt_amount) cleaned_tiles = 0 # Clean some tiles for x, y in xyrange(width-1, height-1): room.clean_tile_at_position(ps3.Position(x + random.random(), y + random.random()), 1) num_cleaned = room.get_num_cleaned_tiles() self.assertEqual(num_cleaned, cleaned_tiles, "Number of clean tiles is incorrect: expected {}, got {}".format(cleaned_tiles, num_cleaned) )
def test_get_num_tiles(self): """ test get_num_tiles method""" widths = [10, 8, 4, 4, 2, 4, 1, 6, 7, 5, 3] heights = [6, 8, 9, 3, 5, 6, 2, 1, 3, 1, 7] sols = [60, 64, 36,12,10,24,2,6,21,5, 21] for i in range(11): # width, height, dirt_amount = (random.randint(1,10), random.randint(1,10), 1) width, height, dirt_amount = (widths[i], heights[i], 1) room_num_tiles = ps3.iRoom(width, height, dirt_amount).get_num_tiles() sol_room_tiles = sols[i] self.assertEqual(room_num_tiles, sol_room_tiles, "student code number of room tiles = {}, not equal to solution code num tiles {}".format(room_num_tiles, sol_room_tiles) )
def test_get_num_cleaned_tiles_OverClean(self): "Test cleaning already clean tiles does not increment counter" width, height, dirt_amount = (3, 4, 2) room = ps3.iRoom(width, height, dirt_amount) # clean all of the tiles in the room for x, y in xyrange(width, height): room.clean_tile_at_position(ps3.Position(x + random.random(), y + random.random()), dirt_amount) for x, y in xyrange(width, height): room.clean_tile_at_position(ps3.Position(x + random.random(), y + random.random()), 1) num_cleaned = room.get_num_cleaned_tiles() self.assertEqual(num_cleaned, width * height, "Number of clean tiles is incorrect: re-cleaning cleaned tiles must not increase number of cleaned tiles" )
def test_getset_robot_direction(self): """Test get_robot_direction and set_robot_direction""" # instantiate iRoom from solutions for testing width, height, dirt_amount = (3, 4, 2) solution_room = ps3.iRoom(width, height, dirt_amount) robots = [ps3.Robot(solution_room, 1.0, 1) for i in range(5)] 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 createRoomAndRobots(self, num_robots): r = ps3.iRoom(5, 7, 1) robots = [ps3.SimpleRobot(r, 1.0, 1) for i in range(num_robots)] return r, robots
def test_unimplemented_methods(self): """Test if student implemented methods in Robot abstract class that should not be implemented""" room = ps3.iRoom(2, 2, 1) robot = ps3.Robot(room, 1, 1) self.assertRaises(NotImplementedError, robot.update_pos_and_clean)