Exemplo n.º 1
0
class GenericTestDestinationDoor(GenericTestDoor):
    DESTINATION_OFFSET = 0xF0AB0
    NEW_DESTINATION_OFFSET = 0x0F1BC0

    def setup(self):
        self.block = AllocatableBlock()
        self.block.from_list([0] * 0x100000)
        self.block.deallocate((self.NEW_DESTINATION_OFFSET, 0x0F58EE))
        self.block[self.DOOR_OFFSET:self.DOOR_OFFSET + 3] = self.DOOR_DATA
        self.block.write_multi(self.DOOR_OFFSET + 3,
                               self.DESTINATION_OFFSET & 0xffff, 2)
        self.block[self.DESTINATION_OFFSET:self.DESTINATION_OFFSET +
                   len(self.DESTINATION_DATA)] = self.DESTINATION_DATA

    def test_to_block(self):
        super(GenericTestDestinationDoor, self).test_to_block()
        assert_equal(self.block.read_multi(self.NEW_DOOR_OFFSET + 3, 2),
                     self.NEW_DESTINATION_OFFSET & 0xffff)
        assert_list_equal(
            self.block[self.NEW_DESTINATION_OFFSET:(
                self.NEW_DESTINATION_OFFSET +
                len(self.DESTINATION_DATA))].to_list(), self.DESTINATION_DATA)

    def test_to_block_reuse_destination(self):
        tmp_address_labels = dict()
        self.door.write_to_block(self.block, self.NEW_DOOR_OFFSET,
                                 tmp_address_labels)
        self.door.write_to_block(self.block, self.NEW_DOOR_OFFSET + 5,
                                 tmp_address_labels)
        assert_list_equal(
            self.block[self.NEW_DOOR_OFFSET:self.NEW_DOOR_OFFSET +
                       5].to_list(),
            self.block[self.NEW_DOOR_OFFSET + 5:self.NEW_DOOR_OFFSET +
                       10].to_list())
        assert_equal(len(tmp_address_labels), 1)

    def test_to_block_do_not_reuse_destination(self):
        tmp_address_labels = dict()
        self.door.write_to_block(self.block, self.NEW_DOOR_OFFSET,
                                 tmp_address_labels)
        # This works because every door with a destination has a text_pointer
        self.door.text_pointer.address += 1
        self.door.write_to_block(self.block, self.NEW_DOOR_OFFSET + 5,
                                 tmp_address_labels)
        assert_list_equal(
            self.block[self.NEW_DOOR_OFFSET:self.NEW_DOOR_OFFSET +
                       3].to_list(),
            self.block[self.NEW_DOOR_OFFSET + 5:self.NEW_DOOR_OFFSET +
                       8].to_list())
        assert_not_equal(
            self.block[self.NEW_DOOR_OFFSET + 3:self.NEW_DOOR_OFFSET +
                       5].to_list(),
            self.block[self.NEW_DOOR_OFFSET + 8:self.NEW_DOOR_OFFSET +
                       10].to_list())
        assert_equal(len(tmp_address_labels), 2)
Exemplo n.º 2
0
class GenericTestDestinationDoor(GenericTestDoor):
    DESTINATION_OFFSET = 0xF0AB0
    NEW_DESTINATION_OFFSET = 0x0F1BC0

    def setup(self):
        self.block = AllocatableBlock()
        self.block.from_list([0] * 0x100000)
        self.block.deallocate((self.NEW_DESTINATION_OFFSET, 0x0F58EE))
        self.block[self.DOOR_OFFSET:self.DOOR_OFFSET + 3] = self.DOOR_DATA
        self.block.write_multi(self.DOOR_OFFSET + 3, self.DESTINATION_OFFSET & 0xffff, 2)
        self.block[self.DESTINATION_OFFSET:self.DESTINATION_OFFSET + len(self.DESTINATION_DATA)] = self.DESTINATION_DATA

    def test_to_block(self):
        super(GenericTestDestinationDoor, self).test_to_block()
        assert_equal(self.block.read_multi(self.NEW_DOOR_OFFSET + 3, 2), self.NEW_DESTINATION_OFFSET & 0xffff)
        assert_list_equal(self.block[self.NEW_DESTINATION_OFFSET:(
            self.NEW_DESTINATION_OFFSET + len(self.DESTINATION_DATA))].to_list(),
                          self.DESTINATION_DATA)

    def test_to_block_reuse_destination(self):
        tmp_address_labels = dict()
        self.door.write_to_block(self.block, self.NEW_DOOR_OFFSET, tmp_address_labels)
        self.door.write_to_block(self.block, self.NEW_DOOR_OFFSET + 5, tmp_address_labels)
        assert_list_equal(self.block[self.NEW_DOOR_OFFSET:self.NEW_DOOR_OFFSET + 5].to_list(),
                          self.block[self.NEW_DOOR_OFFSET + 5:self.NEW_DOOR_OFFSET + 10].to_list())
        assert_equal(len(tmp_address_labels), 1)

    def test_to_block_do_not_reuse_destination(self):
        tmp_address_labels = dict()
        self.door.write_to_block(self.block, self.NEW_DOOR_OFFSET, tmp_address_labels)
        # This works because every door with a destination has a text_pointer
        self.door.text_pointer.address += 1
        self.door.write_to_block(self.block, self.NEW_DOOR_OFFSET + 5, tmp_address_labels)
        assert_list_equal(self.block[self.NEW_DOOR_OFFSET:self.NEW_DOOR_OFFSET + 3].to_list(),
                          self.block[self.NEW_DOOR_OFFSET + 5:self.NEW_DOOR_OFFSET + 8].to_list())
        assert_not_equal(self.block[self.NEW_DOOR_OFFSET + 3:self.NEW_DOOR_OFFSET + 5].to_list(),
                         self.block[self.NEW_DOOR_OFFSET + 8:self.NEW_DOOR_OFFSET + 10].to_list())
        assert_equal(len(tmp_address_labels), 2)
Exemplo n.º 3
0
class TestAllocatableBlock(TestBlock):
    def setup(self):
        self.block = AllocatableBlock()

    def test_getitem_slice_type(self):
        self.block.from_list([0] * 10)
        assert_is_instance(self.block[0:1], Block)

    def test_deallocate(self):
        self.block.from_list([0] * 10)
        assert_raises(InvalidArgumentError, self.block.deallocate, (1, 0))
        assert_raises(InvalidArgumentError, self.block.deallocate, (8, 2))
        assert_raises(OutOfBoundsError, self.block.deallocate, (-1, 0))
        assert_raises(OutOfBoundsError, self.block.deallocate, (-1, 9))
        assert_raises(OutOfBoundsError, self.block.deallocate, (-1, 10))
        assert_raises(OutOfBoundsError, self.block.deallocate, (0, 10))
        assert_raises(OutOfBoundsError, self.block.deallocate, (1, 11))
        assert_raises(OutOfBoundsError, self.block.deallocate, (9, 10))

        self.block.deallocate((0, 2))
        assert_list_equal(self.block.unallocated_ranges, [(0, 2)])
        self.block.deallocate((4, 9))
        assert_list_equal(self.block.unallocated_ranges, [(0, 2), (4, 9)])

    def test_mark_allocated(self):
        self.block.from_list([0] * 10)
        assert_raises(InvalidArgumentError, self.block.mark_allocated, (1, 0))
        assert_raises(InvalidArgumentError, self.block.mark_allocated, (8, 2))
        assert_raises(OutOfBoundsError, self.block.mark_allocated, (-1, 0))
        assert_raises(OutOfBoundsError, self.block.mark_allocated, (-1, 9))
        assert_raises(OutOfBoundsError, self.block.mark_allocated, (-1, 10))
        assert_raises(OutOfBoundsError, self.block.mark_allocated, (0, 10))
        assert_raises(OutOfBoundsError, self.block.mark_allocated, (1, 11))
        assert_raises(OutOfBoundsError, self.block.mark_allocated, (9, 10))
        assert_raises(CouldNotAllocateError, self.block.mark_allocated, (0, 1))

        self.block.from_list([0] * 100)
        self.block.deallocate((0, 99))
        assert_list_equal(self.block.unallocated_ranges, [(0, 99)])
        # Mark middle as allocated, splitting the range into two
        self.block.mark_allocated((3, 44))
        assert_list_equal(self.block.unallocated_ranges, [(0, 2), (45, 99)])
        # Again, but splitting a range into the smallest possible size
        self.block.mark_allocated((1, 1))
        assert_list_equal(self.block.unallocated_ranges, [(0, 0), (2, 2), (45, 99)])
        # Destroying a range of size 1
        self.block.mark_allocated((0, 0))
        assert_list_equal(self.block.unallocated_ranges, [(2, 2), (45, 99)])
        # Allocate from the beginning
        self.block.mark_allocated((45, 55))
        assert_list_equal(self.block.unallocated_ranges, [(2, 2), (56, 99)])
        # Allocate from the end
        self.block.mark_allocated((80, 99))
        assert_list_equal(self.block.unallocated_ranges, [(2, 2), (56, 79)])
        # Allocate an entire range
        self.block.mark_allocated((56, 79))
        assert_list_equal(self.block.unallocated_ranges, [(2, 2)])

        self.block.from_list([0] * 0x50)
        self.block.unallocated_ranges = [(0, 0xf), (0x10, 0x1f), (0x20, 0x2f), (0x30, 0x3f)]
        # Mark a range free that spans multiple unallocated ranges
        self.block.mark_allocated((0x5, 0x25))
        assert_list_equal(self.block.unallocated_ranges, [(0, 0x4), (0x26, 0x2f), (0x30, 0x3f)])
        self.block.mark_allocated((0x26, 0x31))
        assert_list_equal(self.block.unallocated_ranges, [(0, 0x4), (0x32, 0x3f)])
        # Test invalid allocation
        assert_raises(CouldNotAllocateError, self.block.mark_allocated, (0x31, 0x32))
        assert_raises(CouldNotAllocateError, self.block.mark_allocated, (0x3f, 0x40))
        assert_raises(CouldNotAllocateError, self.block.mark_allocated, (0x31, 0x40))

    def test_get_unallocated_portions_of_range(self):
        self.block.from_list([0] * 50)
        self.block.deallocate((0, 10))
        assert_list_equal(self.block.get_unallocated_portions_of_range((2, 8)), [(2, 8)])
        assert_list_equal(self.block.get_unallocated_portions_of_range((5, 20)), [(5, 10)])
        self.block.deallocate((12, 14))
        assert_list_equal(self.block.get_unallocated_portions_of_range((5, 20)), [(5, 10), (12, 14)])
        self.block.deallocate((18, 30))
        assert_list_equal(self.block.get_unallocated_portions_of_range((5, 20)), [(5, 10), (12, 14), (18, 20)])
        assert_list_equal(self.block.get_unallocated_portions_of_range((16, 32)), [(18, 30)])

    def test_is_unallocated(self):
        self.block.from_list([0] * 10)
        assert_raises(InvalidArgumentError, self.block.is_unallocated, (1, 0))
        assert_raises(InvalidArgumentError, self.block.is_unallocated, (8, 2))
        assert_raises(OutOfBoundsError, self.block.is_unallocated, (-1, 0))
        assert_raises(OutOfBoundsError, self.block.is_unallocated, (-1, 9))
        assert_raises(OutOfBoundsError, self.block.is_unallocated, (-1, 10))
        assert_raises(OutOfBoundsError, self.block.is_unallocated, (0, 10))
        assert_raises(OutOfBoundsError, self.block.is_unallocated, (1, 11))
        assert_raises(OutOfBoundsError, self.block.is_unallocated, (9, 10))

        assert_false(self.block.is_unallocated((0, 0)))
        assert_true(self.block.is_allocated((0, 0)))

        self.block.deallocate((1, 3))
        self.block.deallocate((4, 5))
        self.block.deallocate((9, 9))

        assert_true(self.block.is_unallocated((1, 3)))
        assert_true(self.block.is_unallocated((4, 5)))
        assert_true(self.block.is_unallocated((9, 9)))
        assert_true(self.block.is_unallocated((1, 1)))
        assert_false(self.block.is_unallocated((0, 1)))
        assert_false(self.block.is_unallocated((1, 4)))
        assert_false(self.block.is_unallocated((0, 4)))
        assert_false(self.block.is_unallocated((0, 9)))
        assert_false(self.block.is_unallocated((1, 9)))

    def test_allocate(self):
        self.block.from_list([0] * 100)
        assert_raises(InvalidArgumentError, self.block.allocate)
        assert_raises(InvalidArgumentError, self.block.allocate, None, 0)
        assert_raises(InvalidArgumentError, self.block.allocate, None, -1)
        assert_raises(InvalidArgumentError, self.block.allocate, None, -10)
        assert_raises(InvalidArgumentError, self.block.allocate, [], None)
        assert_raises(InvalidArgumentError, self.block.allocate, [1], 2)

        # Allocate an entire range
        self.block.deallocate((0, 49))
        assert_raises(NotEnoughUnallocatedSpaceError, self.block.allocate, None, 51)
        offset = self.block.allocate(size=50)
        assert_equal(offset, 0)
        assert_equal(self.block.unallocated_ranges, [])

        # Allocate the beginning of a range
        self.block.deallocate((10, 39))
        offset = self.block.allocate(data=[0x12, 0x34, 0xef])
        assert_equal(offset, 10)
        assert_equal(self.block.unallocated_ranges, [(13, 39)])
        assert_equal(self.block[offset:offset + 3].to_list(), [0x12, 0x34, 0xef])
        assert_not_equal(self.block.to_list(), [0] * 100)
        self.block[offset:offset + 3] = [0] * 3
        assert_equal(self.block.to_list(), [0] * 100)

    def test_allocate_across_ranges(self):
        self.block.from_list([0] * 100)
        self.block.deallocate((0, 5))
        self.block.deallocate((6, 9))
        assert_raises(NotEnoughUnallocatedSpaceError, self.block.allocate, None, 10)