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


Python Block.read_multi方法代码示例

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


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

示例1: TestBlock

# 需要导入模块: from coilsnake.model.common.blocks import Block [as 别名]
# 或者: from coilsnake.model.common.blocks.Block import read_multi [as 别名]

#.........这里部分代码省略.........
        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])

    def test_read_multi(self):
        self.block.from_list([0x03, 0xa1, 0x44, 0x15, 0x92, 0x65])

        assert_equal(self.block.read_multi(0, 4), 0x1544a103)
        assert_equal(self.block.read_multi(1, 4), 0x921544a1)
        assert_equal(self.block.read_multi(1, 1), 0xa1)
        assert_equal(self.block.read_multi(1, 2), 0x44a1)
        assert_equal(self.block.read_multi(2, 3), 0x921544)
        assert_equal(self.block.read_multi(3, 3), 0x659215)
        assert_equal(self.block.read_multi(5, 1), 0x65)
        assert_equal(self.block.read_multi(0, 0), 0)
        assert_equal(self.block.read_multi(5, 0), 0)

        assert_raises(InvalidArgumentError, self.block.read_multi, 0, -1)
        assert_raises(InvalidArgumentError, self.block.read_multi, 0, -99)
        assert_raises(OutOfBoundsError, self.block.read_multi, -1, 3)
        assert_raises(OutOfBoundsError, self.block.read_multi, 7, 1)
        assert_raises(OutOfBoundsError, self.block.read_multi, 5, 2)
        assert_raises(OutOfBoundsError, self.block.read_multi, 0, 7)

    def test_write_multi(self):
        self.block.from_list([0x03, 0xa1, 0x44, 0x15, 0x92, 0x65])

        self.block.write_multi(0, 0, 0)
        assert_list_equal(self.block.to_list(), [0x03, 0xa1, 0x44, 0x15, 0x92, 0x65])
        self.block.write_multi(0, 0xff, 1)
        assert_list_equal(self.block.to_list(), [0xff, 0xa1, 0x44, 0x15, 0x92, 0x65])
        self.block.write_multi(1, 0xa1b2, 2)
        assert_list_equal(self.block.to_list(), [0xff, 0xb2, 0xa1, 0x15, 0x92, 0x65])
        self.block.write_multi(2, 0x100000f, 4)
        assert_list_equal(self.block.to_list(), [0xff, 0xb2, 0x0f, 0x00, 0x00, 0x01])

        assert_raises(InvalidArgumentError, self.block.write_multi, 0, 0, -1)
        assert_raises(OutOfBoundsError, self.block.write_multi, -1, 0, 1)
        assert_raises(OutOfBoundsError, self.block.write_multi, -1, 0, 1)
        assert_raises(OutOfBoundsError, self.block.write_multi, 0, 0, 7)
        assert_raises(OutOfBoundsError, self.block.write_multi, 1, 0, 6)
        assert_raises(OutOfBoundsError, self.block.write_multi, 3, 0, 4)

    def test_len(self):
        self.block.from_list([0x03, 0xa1, 0x44, 0x15, 0x92, 0x65])
        assert_equal(len(self.block), 6)
        self.block.from_list([])
        assert_equal(len(self.block), 0)
开发者ID:LittleCube13,项目名称:CoilSnake,代码行数:104,代码来源:test_blocks.py


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