當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。