コード例 #1
0
 def test_swapbytes(self):
     assert swapbytes(b"aaaabbbbcccc", 4) == b"ccccbbbbaaaa"
     assert swapbytes(b"abcdefgh", 2) == b"ghefcdab"
     assert swapbytes(b"00011011", 2) == b"11100100"
     assert swapbytes(b"", 2) == b""
     assert raises(swapbytes, b"12345678", 7) == ValueError
     assert raises(swapbytes, b"12345678", -4) == ValueError
コード例 #2
0
 def test_swapbytes(self):
     assert swapbytes(b"aaaabbbbcccc", 4) == b"ccccbbbbaaaa"
     assert swapbytes(b"abcdefgh", 2) == b"ghefcdab"
     assert swapbytes(b"00011011", 2) == b"11100100"
     assert swapbytes(b"", 2) == b""
     assert raises(swapbytes, b"12345678", 7) == ValueError
     assert raises(swapbytes, b"12345678", -4) == ValueError
コード例 #3
0
 def test_bits2bytes(self):
     assert bits2bytes(
         b"\x00\x01\x01\x00\x00\x00\x00\x01\x00\x01\x01\x00\x00\x00\x01\x00"
     ) == b"ab"
     assert bits2bytes(b"") == b""
     assert raises(bits2bytes, b"\x00") == ValueError
     assert raises(bits2bytes,
                   b"\x00\x00\x00\x00\x00\x00\x00") == ValueError
コード例 #4
0
 def test_integer2bits(self):
     assert integer2bits(19, 5) == b"\x01\x00\x00\x01\x01"
     assert integer2bits(19, 8) == b'\x00\x00\x00\x01\x00\x00\x01\x01'
     assert integer2bits(19, 3) == b'\x00\x01\x01'
     assert integer2bits(-13, 5) == b"\x01\x00\x00\x01\x01"
     assert integer2bits(-13, 8) == b"\x01\x01\x01\x01\x00\x00\x01\x01"
     assert raises(integer2bits, 19, 0) == ValueError
     assert raises(integer2bits, 19, -1) == ValueError
     assert raises(integer2bits, -19, 0) == ValueError
     assert raises(integer2bits, -19, -1) == ValueError
コード例 #5
0
 def test_integer2bytes(self):
     assert integer2bytes(0, 4) == b"\x00\x00\x00\x00"
     assert integer2bytes(1, 4) == b"\x00\x00\x00\x01"
     assert integer2bytes(255, 4) == b"\x00\x00\x00\xff"
     assert integer2bytes(-1, 4) == b"\xff\xff\xff\xff"
     assert integer2bytes(-255, 4) == b"\xff\xff\xff\x01"
     assert raises(integer2bytes, 19, 0) == ValueError
     assert raises(integer2bytes, 19, -1) == ValueError
     assert raises(integer2bytes, -19, 0) == ValueError
     assert raises(integer2bytes, -19, -1) == ValueError
コード例 #6
0
 def test_integer2bits(self):
     assert integer2bits(19, 5) == b"\x01\x00\x00\x01\x01"
     assert integer2bits(19, 8) == b'\x00\x00\x00\x01\x00\x00\x01\x01'
     assert integer2bits(19, 3) == b'\x00\x01\x01'
     assert integer2bits(-13, 5) == b"\x01\x00\x00\x01\x01"
     assert integer2bits(-13, 8) == b"\x01\x01\x01\x01\x00\x00\x01\x01"
     assert raises(integer2bits, 19, 0) == ValueError
     assert raises(integer2bits, 19, -1) == ValueError
     assert raises(integer2bits, -19, 0) == ValueError
     assert raises(integer2bits, -19, -1) == ValueError
コード例 #7
0
 def test_integer2bytes(self):
     assert integer2bytes(0, 4) == b"\x00\x00\x00\x00"
     assert integer2bytes(1, 4) == b"\x00\x00\x00\x01"
     assert integer2bytes(255, 4) == b"\x00\x00\x00\xff"
     assert integer2bytes(-1, 4) == b"\xff\xff\xff\xff"
     assert integer2bytes(-255, 4) == b"\xff\xff\xff\x01"
     assert raises(integer2bytes, 19, 0) == ValueError
     assert raises(integer2bytes, 19, -1) == ValueError
     assert raises(integer2bytes, -19, 0) == ValueError
     assert raises(integer2bytes, -19, -1) == ValueError
コード例 #8
0
 def test_delitem(self):
     c = Container(a=1, b=2)
     del c.a
     assert "a" not in c
     assert raises(lambda: c.a) == AttributeError
     assert raises(lambda: c["a"]) == KeyError
     del c["b"]
     assert "b" not in c
     assert raises(lambda: c.b) == AttributeError
     assert raises(lambda: c["b"]) == KeyError
     assert c == Container()
コード例 #9
0
 def test_delitem(self):
     c = Container(a=1, b=2)
     del c.a
     assert "a" not in c
     assert raises(lambda: c.a) == AttributeError
     assert raises(lambda: c["a"]) == KeyError
     del c["b"]
     assert "b" not in c
     assert raises(lambda: c.b) == AttributeError
     assert raises(lambda: c["b"]) == KeyError
     assert c == Container()
コード例 #10
0
 def test_popitem(self):
     c = Container(a=1)(b=2)(c=3)(d=4)
     assert c.popitem() == ("d", 4)
     assert c.popitem() == ("c", 3)
     assert c.popitem() == ("b", 2)
     assert c.popitem() == ("a", 1)
     assert raises(c.popitem) == IndexError
コード例 #11
0
 def test_popitem(self):
     c = Container(a=1)(b=2)(c=3)(d=4)
     assert c.popitem() == ("d",4)
     assert c.popitem() == ("c",3)
     assert c.popitem() == ("b",2)
     assert c.popitem() == ("a",1)
     assert raises(c.popitem) == IndexError
コード例 #12
0
 def test_pop(self):
     c = Container(a=1)(b=2)(c=3)(d=4)
     assert c.pop("b") == 2
     assert c.pop("d") == 4
     assert c.pop("a") == 1
     assert c.pop("c") == 3
     assert raises(c.pop, "missing") == KeyError
     assert c == Container()
コード例 #13
0
 def test_pop(self):
     c = Container(a=1)(b=2)(c=3)(d=4)
     assert c.pop("b") == 2
     assert c.pop("d") == 4
     assert c.pop("a") == 1
     assert c.pop("c") == 3
     assert raises(c.pop, "missing") == KeyError
     assert c == Container()
コード例 #14
0
ファイル: test_protocols.py プロジェクト: yairchu/construct
def common(format, data, obj, size=None):
    assert format.parse(data) == obj
    assert format.build(obj) == data
    # following are guaranteed by the above
    # assert format.parse(format.build(obj)) == obj
    # assert format.build(format.parse(data)) == data
    if isinstance(size, int):
        assert format.sizeof() == size
    else:
        assert raises(format.sizeof) == size
コード例 #15
0
def common(format, data, obj, size=None):
    assert format.parse(data) == obj
    assert format.build(obj) == data
    # following are guaranteed by the above
    # assert format.parse(format.build(obj)) == obj
    # assert format.build(format.parse(data)) == data
    if size is not None:
        if isinstance(size, Exception):
            assert raises(format.sizeof) == size
        else:
            assert format.sizeof() == size
コード例 #16
0
 def test_bits2bytes(self):
     assert bits2bytes(b"\x00\x01\x01\x00\x00\x00\x00\x01\x00\x01\x01\x00\x00\x00\x01\x00") == b"ab"
     assert bits2bytes(b"") == b""
     assert raises(bits2bytes, b"\x00") == ValueError
     assert raises(bits2bytes, b"\x00\x00\x00\x00\x00\x00\x00") == ValueError
コード例 #17
0
 def test_getitem(self):
     c = Container(a=1)
     assert c["a"] == 1
     assert c.a == 1
     assert raises(lambda: c.unknownkey) == AttributeError
     assert raises(lambda: c["unknownkey"]) == KeyError
コード例 #18
0
 def test_getitem(self):
     c = Container(a=1)
     assert c["a"] == 1
     assert c.a == 1
     assert raises(lambda: c.unknownkey) == AttributeError
     assert raises(lambda: c["unknownkey"]) == KeyError