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_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_get_num_cleaned_tiles_FullIn2(self): """Test get_num_cleaned_tiles for cleaning subset of room in two calls""" 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) room.clean_tile_at_position(ps3.Position(x + random.random(), y + random.random()), 1) cleaned_tiles += 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_unimplemented_methods(self): """Test if student implemented methods in RectangularRoom abstract class that should not be implemented""" room = ps3.RectangularRoom(2, 2, 1) self.assertRaises(NotImplementedError, room.get_num_tiles) pos = ps3.Position(1, 1) self.assertRaises(NotImplementedError, room.is_position_valid, pos) self.assertRaises(NotImplementedError, room.get_random_position)
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_clean_tile_at_position_PosToPos(self): """ Test if clean_tile_at_position removes all dirt""" width, height, dirt_amount = (3, 4, 2) room = ps3.RectangularRoom(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()), dirt_amount - 1) # using random.random in case there is any issue with specific parts of a tile for x, y in xyrange(width, height): self.assertFalse(room.is_tile_cleaned(x, y), "Unclean tile {} was marked clean".format((x, y)) )
def test_is_position_valid(self): """ Test is_position_valid this should be refactored as it's mostly a copy of is_position_in_room code """ width, height, dirt_amount = (3, 4, 2) room = ps3.EmptyRoom(width, height, dirt_amount) solution_room = ps3.EmptyRoom(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 = ps3.Position(x, y) self.assertEquals(solution_room.is_position_valid(pos), room.is_position_valid(pos), "student code and solution code disagree on whether position is valid" )
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 = ps3.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 = ps3.Position(x, y) self.assertEquals( 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_is_position_furnished(self): """ test_is_position_furnished """ for trial in range(5): width, height, dirt_amount = (random.randint(2, 8), random.randint(2, 8), 1) # create room using student's class, set furniture tiles for solution class room = ps3.FurnishedRoom(width, height, dirt_amount) room.add_furniture_to_room() sol_room = ps3.FurnishedRoom(width, height, dirt_amount) # this relies on knowing the underlying details of the class sol_room.furniture_tiles = room.furniture_tiles for x, y in xyrange(width, height): pos = ps3.Position(x + random.random(), y + random.random()) self.assertEquals(room.is_position_furnished(pos), sol_room.is_position_furnished(pos), "student code and solution code disagree on whether position is furnished" )
def test_is_position_valid(self): """ Test is_position_valid """ for trial in range(5): width, height, dirt_amount = (3, 4, 2) room = ps3.FurnishedRoom(width, height, dirt_amount) room.add_furniture_to_room() sol_room = ps3.FurnishedRoom(width, height, dirt_amount) sol_room.furniture_tiles = room.furniture_tiles for x in [0.0, -0.1, width - 0.1, width, width + 0.1, room.furniture_tiles[0][0] + 0.3]: for y in [0.0, -0.1, height - 0.1, height, height + 0.1, room.furniture_tiles[0][1] + 0.3]: pos = ps3.Position(x, y) self.assertEquals(sol_room.is_position_valid(pos), room.is_position_valid(pos), "student code and solution code disagree on whether position is valid" )
import unittest import ps3 import random print('Creating 5x5 room') room = ps3.RectangularRoom(5, 5, 20) print('get_dirt_amopunt(1,3)', room.get_dirt_amount(1, 3)) print('get_num_cleaned_tiles', room.get_num_cleaned_tiles()) print('is_tile_cleaned(1,3)', room.is_tile_cleaned(1, 3)) botpos = ps3.Position(1, 3) room.clean_tile_at_position(botpos, 15) print('1,3 after 15 cleaning (5)', room.is_tile_cleaned(1, 3), room.get_dirt_amount(1, 3)) room.clean_tile_at_position(botpos, 5) print('1,3 after 5 more cleaning (0)', room.is_tile_cleaned(1, 3)) room.clean_tile_at_position(botpos, 5) print('1,3 after 5 more cleaning (-5)', room.is_tile_cleaned(1, 3)) # create robot room = ps3.EmptyRoom(5, 5, 20) for i in range(0, 15): helper = ps3.Robot(room, 1, 1) print(str(helper), str(helper.get_robot_true_position())) upperboundscheck = 0 normal_check = 0 err_check = 0 count = 0 err_list = [] while count < 1000: count += 1 helper = ps3.Robot(room, 1, 1)