def test_write(self): expected = struct.pack('<bBhHiIqQfd', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) n = len(expected) buf = SegmentBuilder(n) buf.allocate(n) buf.write_int8(0, 10) buf.write_uint8(1, 20) buf.write_int16(2, 30) buf.write_uint16(4, 40) buf.write_int32(6, 50) buf.write_uint32(10, 60) buf.write_int64(14, 70) buf.write_uint64(22, 80) buf.write_float32(30, 90) buf.write_float64(34, 100) s = buf.as_string() assert s == expected
def test_alloc_list(self): buf = SegmentBuilder(64) buf.allocate(16) a = buf.alloc_list(0, size_tag=ptr.LIST_SIZE_8, item_count=4, body_length=4) bb = buf.alloc_list(8, size_tag=ptr.LIST_SIZE_16, item_count=2, body_length=4) buf.write_int8(a, ord('f')) buf.write_int8(a+1, ord('o')) buf.write_int8(a+2, ord('o')) buf.write_int8(a+3, ord('\x00')) # buf.write_int16(bb, 0x1234) buf.write_int16(bb+2, 0x5678) # s = buf.as_string() assert s == b('\x05\x00\x00\x00\x22\x00\x00\x00' # ptrlist int8, item_count=4 '\x05\x00\x00\x00\x13\x00\x00\x00' # ptrlist int16, item_count=2 'foo\x00\x00\x00\x00\x00' '\x34\x12\x78\x56\x00\x00\x00\x00') # 0x1234 0x5678