def test_add(self): acc = BitString() for i in range(8): acc += BitString.from_int('0o' + str(i)) # >>> hex(0o76543210) # '0xfac688' self.assert_length_value(24, b'\x88\xc6\xfa', acc) acc = BitString() for i in range(7): acc += BitString.from_int('0o' + str(i)) self.assert_length_value(21, b'\x88\xc6\x1a', acc)
def assert_round_trip(self, start, stop=None, length=None): if stop is None: stop = start result = BitString.from_int(start, length=length).to_int() assert result == stop, (result, stop) if length is not None: assert len(result) == length, (result, length)
def matcher(value=None): """ Generate the matcher, given a value. """ if value is None: return LEnd(length) else: return _Constant(BitString.from_int(value, length=length, big_endian=False))
def matcher(value=None): ''' Generate the matcher, given a value. ''' if value is None: return BEnd(length) else: return _Constant(BitString.from_int(value, length=length, big_endian=True))
def matcher(value=None): ''' Generate the matcher, given a value. ''' if value is None: return LEnd(length) else: return _Constant( BitString.from_int(value, length=length, big_endian=False))
def dispatch_table(big_endian=True, encoding=None, errors=STRICT): ''' Convert types appropriately. ''' # pylint: disable-msg=W0108 # consistency return {int: lambda n: BitString.from_int(n, ordered=big_endian), str: lambda s: BitString.from_str(s, encoding, errors), bytes: lambda b: BitString.from_bytearray(b), bytearray: lambda b: BitString.from_bytearray(b), BitString: lambda x: x}
def test_get_item(self): a = BitString.from_int('01001100011100001111b0') b = a[:] assert a == b, (a, b) b = a[0:] assert a == b, (a, b) b = a[-1::-1] assert BitString.from_int('11110000111000110010b0') == b, b b = a[0] assert BitString.from_int('0b0') == b, (b, str(b), BitString.from_int('0b0')) b = a[1] assert BitString.from_int('1b0') == b, b b = a[0:2] assert BitString.from_int('01b0') == b, b b = a[0:2] assert BitString.from_int('0b10') == b, b b = a[-5:] assert BitString.from_int('01111b0') == b, b b = a[-1:-6:-1] assert BitString.from_int('11110b0') == b, b b = a[1:-1] assert BitString.from_int('100110001110000111b0') == b, b
def dispatch_table(big_endian=True, encoding=None, errors=STRICT): ''' Convert types appropriately. ''' # pylint: disable-msg=W0108 # consistency return { int: lambda n: BitString.from_int(n, ordered=big_endian), str: lambda s: BitString.from_str(s, encoding, errors), bytes: lambda b: BitString.from_bytearray(b), bytearray: lambda b: BitString.from_bytearray(b), BitString: lambda x: x }
def test_encode(self): mac = parse(''' Frame( Header( preamble = 0b10101010*7, start = 0b10101011, destn = 010203040506x0, source = 0708090a0b0cx0, ethertype = 0800x0 ), Data(1/8,2/8,3/8,4/8), CRC(234d0/4.) ) ''') serial = simple_serialiser(mac, dispatch_table()) bs = serial.bytes() for _index in range(7): b = next(bs) assert b == BitString.from_int('0b10101010').to_int(), b b = next(bs) assert b == BitString.from_int('0b10101011').to_int(), b
def _assert(self, repr_, value): try: b = BitString.from_int(repr_) assert str(b) == value + 'b0/' + str(len(b)), str(b) except ValueError: assert value is None
def test_invert(self): #basicConfig(level=DEBUG) self.assert_length_value(12, b'\x00\x0c', ~BitString.from_int('0x3ff'))
def test_str(self): b = BitString.from_int32(0xabcd1234) assert str(b) == '00101100 01001000 10110011 11010101b0/32', str(b) b = BitString.from_int('0b110') assert str(b) == '011b0/3', str(b)
def test_from_int_with_length(self): self.assert_error(lambda: BitString.from_int(1, 0)) self.assert_error(lambda: BitString.from_int(0, 1)) self.assert_error(lambda: BitString.from_int(0, 7)) self.assert_length_value(8, b'\x00', BitString.from_int(0, 8)) self.assert_error(lambda: BitString.from_int(0, 0.1)) self.assert_length_value(8, b'\x00', BitString.from_int(0, 1.)) self.assert_length_value(1, b'\x00', BitString.from_int('0x0', 1)) self.assert_length_value(7, b'\x00', BitString.from_int('0x0', 7)) self.assert_length_value(8, b'\x00', BitString.from_int('0x0', 8)) self.assert_length_value(1, b'\x00', BitString.from_int('0x0', 0.1)) self.assert_length_value(8, b'\x00', BitString.from_int('0x0', 1.)) self.assert_length_value(16, b'\x34\x12', BitString.from_int(0x1234, 16)) self.assert_length_value(16, b'\x34\x12', BitString.from_int('0x1234', 16)) self.assert_length_value(16, b'\x12\x34', BitString.from_int('1234x0', 16)) self.assert_length_value(16, b'\x34\x12', BitString.from_int('4660', 16)) self.assert_length_value(16, b'\x34\x12', BitString.from_int('0d4660', 16)) self.assert_length_value(16, b'\x12\x34', BitString.from_int('4660d0', 16))
def bassert(self, value, expected, length=None): x = BitString.from_int(expected, length) assert value == x, (value, x)
def __init__(self, value, length=None): if not isinstance(value, BitString): value = BitString.from_int(value, length) super(Const, self).__init__(value)
def test_from_int(self): self.assert_length_value(3, b'\x00', BitString.from_int('0o0')) self.assert_error(lambda: BitString.from_int('1o0')) self.assert_error(lambda: BitString.from_int('00o0')) self.assert_error(lambda: BitString.from_int('100o0')) self.assert_error(lambda: BitString.from_int('777o0')) self.assert_length_value(9, b'\x40\x00', BitString.from_int('0o100')) self.assert_length_value(9, b'\xfe\x01', BitString.from_int('0o776')) self.assert_length_value(12, b'\xff\x03', BitString.from_int('0x3ff')) self.assert_length_value(12, b'\xff\x03', BitString.from_int('0o1777')) self.assert_length_value(16, b'\x03\xff', BitString.from_int('03ffx0')) self.assert_length_value(3, b'\x04', BitString.from_int('0b100')) self.assert_length_value(1, b'\x01', BitString.from_int('1b0')) self.assert_length_value(2, b'\x02', BitString.from_int('01b0')) self.assert_length_value(9, b'\x00\x01', BitString.from_int('000000001b0')) self.assert_length_value(9, b'\x01\x01', BitString.from_int('100000001b0')) self.assert_length_value(16, b'\x0f\x33', BitString.from_int('1111000011001100b0'))