def test_get_random_position(self): """Test get_random_position for FurnishedRoom tests for validity of positions - could add distribution checking similar to empty room """ width, height, dirt_amount = (5, 10, 1) # instanciate student's room room = ps3.FurnishedRoom(width, height, dirt_amount) room.add_furniture_to_room() # instanciate solution's room based on student's furniture sol_room = ps3.FurnishedRoom(width, height, dirt_amount) sol_room.furniture_tiles = room.furniture_tiles for i in range(50000): pos = room.get_random_position() self.assertTrue(sol_room.is_position_valid(pos))
def test_is_tile_furnished(self): """ test is_tile_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): self.assertEquals(room.is_tile_furnished(x, y), sol_room.is_tile_furnished(x, y), "student code and solution code disagree on whether tile 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 = test.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 = test.Position(x, y) self.assertEqual( sol_room.is_position_valid(pos), room.is_position_valid(pos), "student code and solution code disagree on whether position is valid:" + str(pos), )
def test_get_num_tiles(self): """ test get_num_tiles method should refactor - is mostly copy of EmptyRoom test """ for i in range(10): width, height, dirt_amount = (random.randint(2,10), random.randint(2,10), 1) # instanciate student's room room = ps3.FurnishedRoom(width, height, dirt_amount) room.add_furniture_to_room() # instanciate solution's room based on student's furniture sol_room = ps3.FurnishedRoom(width, height, dirt_amount) sol_room.furniture_tiles = room.furniture_tiles # generate answers room_num_tiles = room.get_num_tiles() sol_room_num_tiles = sol_room.get_num_tiles() self.assertEquals(room_num_tiles, sol_room_num_tiles, "student code number of room tiles = {}, not equal to solution code num tiles {}".format(room_num_tiles, sol_room_num_tiles) )
err_list = [] while count < 10000: pos = room.get_random_position() x, y = pos.get_x(), pos.get_y() count += 1 if x >= 0 and x < room.width + 1 and y >= 0 and y < room.height + 1: normal_check += 1 else: err_list.append(str(x) + ', ' + str(y)) err_check += 1 print('Random position test') print('Sims run:', count, 'Good runs:', normal_check, 'Errors:', err_check) print('Error list:', err_list, '\n') ### start trials on furnished room room = ps3.FurnishedRoom(5, 5, 2) list_furnished = [] room.add_furniture_to_room() for i in room.tiles.keys(): # print(i, float(i[0]), float(i[1])) x, y = i[0], i[1] # print(room.is_tile_furnished(x,y)) if room.is_tile_furnished(x, y): list_furnished.append((x, y)) #(str(x)+', '+ str(y)) print("Furnished tile:\n", room.furniture_tiles) print("is_tile_furnished:\n", sorted(list_furnished)) check = room.furniture_tiles == sorted(list_furnished) print('sorted lists match:', check) ### pos trials on furnished room
import ps3 x = ps3.FurnishedRoom(2, 3, 40) rand = x.get_random_position() print(rand.get_x(), rand.get_y())