当前位置: 首页>>代码示例>>Python>>正文


Python Block.from_file方法代码示例

本文整理汇总了Python中coilsnake.model.common.blocks.Block.from_file方法的典型用法代码示例。如果您正苦于以下问题:Python Block.from_file方法的具体用法?Python Block.from_file怎么用?Python Block.from_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在coilsnake.model.common.blocks.Block的用法示例。


在下文中一共展示了Block.from_file方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestBlock

# 需要导入模块: from coilsnake.model.common.blocks import Block [as 别名]
# 或者: from coilsnake.model.common.blocks.Block import from_file [as 别名]
class TestBlock(BaseTestCase):
    def setup(self):
        self.block = Block()

    def teardown(self):
        del self.block

    def test_baseline(self):
        pass

    def test_empty(self):
        assert_equal(len(self.block), 0)
        assert_equal(len(self.block.data), 0)

    def test_from_file(self):
        self.block.from_file(os.path.join(TEST_DATA_DIR, "binaries", "1kb_null.bin"))
        assert_equal(len(self.block), 1024)
        assert_list_equal(self.block.to_list(), [0] * 1024)

    def test_from_file_unhappy(self):
        # Attempt to load a directory
        assert_raises(FileAccessError, self.block.from_file, TEST_DATA_DIR)
        # Attempt to load a nonexistent file
        assert_raises(FileAccessError, self.block.from_file, os.path.join(TEST_DATA_DIR, "doesnotexist.bin"))
        # Attempt to load a file in a nonexistent directory
        assert_raises(FileAccessError, self.block.from_file, os.path.join(TEST_DATA_DIR, "dne", "dne.bin"))

    def test_from_list(self):
        self.block.from_list([0, 1, 2, 3, 4, 5])
        assert_equal(len(self.block), 6)
        assert_list_equal(self.block.to_list(), [0, 1, 2, 3, 4, 5])

        self.block.from_list([])
        assert_equal(len(self.block), 0)
        assert_list_equal(self.block.to_list(), [])

        self.block.from_list([69])
        assert_equal(len(self.block), 1)
        assert_list_equal(self.block.to_list(), [69])

    def test_getitem(self):
        self.block.from_file(os.path.join(TEST_DATA_DIR, "binaries", "1kb_rand.bin"))

        assert_equal(self.block[0], 0x25)
        assert_equal(self.block[1023], 0x20)
        assert_equal(self.block[0x3e3], 0xf4)
        assert_equal(self.block[1023], self.block[-1])

        assert_raises(OutOfBoundsError, self.block.__getitem__, 1024)
        assert_raises(OutOfBoundsError, self.block.__getitem__, 9999)

    def test_getitem_slice(self):
        self.block.from_file(os.path.join(TEST_DATA_DIR, "binaries", "1kb_rand.bin"))

        assert_is_instance(self.block[0:1], Block)

        assert_list_equal(self.block[0:0].to_list(), [])
        assert_list_equal(self.block[0x25c:0x25c].to_list(), [])
        assert_list_equal(self.block[0x25c:0x25d].to_list(), [0xa0])
        assert_list_equal(self.block[0x25c:0x25c + 5].to_list(), [0xa0, 0x0b, 0x71, 0x5d, 0x91])
        assert_list_equal(self.block[0x25c:0x25c + 5].to_list(), [0xa0, 0x0b, 0x71, 0x5d, 0x91])
        assert_list_equal(self.block[1022:1024].to_list(), [0x10, 0x20])

        assert_raises(InvalidArgumentError, self.block.__getitem__, slice(0, -1))
        assert_raises(OutOfBoundsError, self.block.__getitem__, slice(-2, -1))
        assert_raises(InvalidArgumentError, self.block.__getitem__, slice(1024, 0))
        assert_raises(InvalidArgumentError, self.block.__getitem__, slice(1024, -1))
        assert_raises(InvalidArgumentError, self.block.__getitem__, slice(1022, 3))

    def test_setitem(self):
        self.block.from_file(os.path.join(TEST_DATA_DIR, "binaries", "1kb_rand.bin"))

        self.block[1] = 0xaa
        assert_equal(self.block[0], 0x25)
        assert_equal(self.block[1], 0xaa)
        assert_equal(self.block[2], 0x38)
        assert_raises(OutOfBoundsError, self.block.__setitem__, 1024, 0xbb)

        assert_raises(InvalidArgumentError, self.block.__setitem__, 5, 0x1234)
        assert_raises(InvalidArgumentError, self.block.__setitem__, 0, 0x100)
        assert_raises(InvalidArgumentError, self.block.__setitem__, 1, -1)

    def test_setitem_slice(self):
        self.block.from_file(os.path.join(TEST_DATA_DIR, "binaries", "1kb_rand.bin"))

        assert_list_equal(self.block[0:3].to_list(), [0x25, 0x20, 0x38])
        self.block[0:3] = [0xeb, 0x15, 0x66]
        assert_list_equal(self.block[0:3].to_list(), [0xeb, 0x15, 0x66])
        self.block[0:1024] = [5] * 1024
        assert_equal(self.block[0:1024].to_list(), [5] * 1024)

        assert_raises(InvalidArgumentError, self.block.__setitem__, slice(5, 0), [])
        assert_raises(InvalidArgumentError, self.block.__setitem__, slice(55, 55), [])
        assert_raises(OutOfBoundsError, self.block.__setitem__, slice(-1, 2), [])
        assert_raises(OutOfBoundsError, self.block.__setitem__, slice(1, 1025), [0] * 1024)
        assert_raises(OutOfBoundsError, self.block.__setitem__, slice(1024, 1025), [1])
        assert_raises(InvalidArgumentError, self.block.__setitem__, slice(0, 1), [])
        assert_raises(InvalidArgumentError, self.block.__setitem__, slice(0, 1), [1, 2, 3])
        assert_raises(InvalidArgumentError, self.block.__setitem__, slice(0, 5), [1, 2])

#.........这里部分代码省略.........
开发者ID:LittleCube13,项目名称:CoilSnake,代码行数:103,代码来源:test_blocks.py


注:本文中的coilsnake.model.common.blocks.Block.from_file方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。