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
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
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
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
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()
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
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
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()
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
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
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
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