def test_poly_pointer_castless(cls, expected_val): pp_type = PolyPointerType(FieldPath(), PolyPointer[cls], DataclassStruct) pp_supreme = pp_type.from_bytes( SUPREME_POINTER_BYTES, MemContext(LOWEST_BYTES_READER) ) assert pp_supreme.addr == SUPREME_POINTER_ADDR assert pp_supreme.value == expected_val
def test_dataclass_struct_nested(): meta_0 = {} StructFieldMeta(0x0, deferred_uint8).put_into(meta_0) meta_2 = {} StructFieldMeta(0x2, deferred_uint8).put_into(meta_2) @dataclass class Inner: field_a: int = field(metadata=meta_0) field_b: int = field(metadata=meta_2) def deferred_inner(path, field_type): return DataclassStruct(path, field_type) meta_3_inner = {} StructFieldMeta(0x3, deferred_inner).put_into(meta_3_inner) @dataclass class Outer: field_0: int = field(metadata=meta_0) field_2: int = field(metadata=meta_2) inner: Inner = field(metadata=meta_3_inner) dc_outer = DataclassStruct(FieldPath(), Outer) assert dc_outer.from_bytes(b"\x00\x01\x02\x03\x04\x05", MemContext()) == Outer( 0, 2, Inner(3, 5) )
def test_vesctor_bad_buf(): vec_buf = b"" arr_buf = b"\xff\x03\x00\x09\x00" mem_type = Vector(FieldPath(), Optional[Tuple[int, ...]], sc_uint8) mem_ctx = MemContext(BytesReader(arr_buf)) with pytest.raises(ValueError): mem_type.from_bytes(vec_buf, mem_ctx)
def test_dataclass_struct_scalar_c_error_passthrough(): meta_0 = {} StructFieldMeta(0x0, deferred_uint8).put_into(meta_0) @dataclass class MyStruct: field_a: FourEnum = field(metadata=meta_0) dc_struct = DataclassStruct(FieldPath(), MyStruct) with pytest.raises(ScalarCValueConstructionError): dc_struct.from_bytes(b"\x00", MemContext())
def test_vector_dsl(): vec_wrap_buf = ( b"\xdc" * 9 # offset 1 and unused pointer + b"\x01\x00\x00\x00\x00\x00\x00\x00" + b"\xde" * 4 + b"\x02\x00\x00\x00" ) arr_buf = b"\xff\x03\x00\x09\x00" mem_type = DataclassStruct(FieldPath(), VecWrap) mem_ctx = MemContext(BytesReader(arr_buf)) assert mem_type.from_bytes(vec_wrap_buf, mem_ctx) == VecWrap((3, 9))
def test_state(): state_mt = DataclassStruct(FieldPath(), State) state_bytes = b"\x05\xff\x00\x01\x01\x63\x2a\xff\x08\x01\xff\x02\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00" # pylint: disable=line-too-long mem_ctx = MemContext(BytesReader(b"\x00\x00\x03\x01\x0f\x08")) expected = State( 5, (False, True, True), frozenset([Player(99, 42), Player(8, 1)]), 259, PolyPointer(4, Player(15, 8), mem_ctx), ) assert state_mt.from_bytes(state_bytes, mem_ctx) == expected
def test_dataclass_struct_general_error(): meta_0 = {} StructFieldMeta(0x0, deferred_uint8).put_into(meta_0) @dataclass class MyStruct: field_a: FourEnum = field(metadata=meta_0) dc_struct = DataclassStruct(FieldPath(), MyStruct) with pytest.raises(ValueError): # Too few bytes in buffer dc_struct.from_bytes(b"", MemContext())
def test_dataclass_struct_simple(): meta_0 = {} StructFieldMeta(0x0, deferred_uint8).put_into(meta_0) meta_3 = {} StructFieldMeta(0x3, deferred_uint8).put_into(meta_3) @dataclass class MyStruct: field_a: int = field(metadata=meta_0) field_b: int = field(metadata=meta_3) dc_struct = DataclassStruct(FieldPath(), MyStruct) assert dc_struct.from_bytes(b"\x00\x01\x02\x03", MemContext()) == MyStruct(0, 3)
def test_poly_pointer_cast_up(cls): pp_type = PolyPointerType(FieldPath(), PolyPointer[Lowest], DataclassStruct) pp_lowest = pp_type.from_bytes( SUPREME_POINTER_BYTES, MemContext(LOWEST_BYTES_READER) ) expected_val = Lowest(1, 2, 3) cast_val = pp_lowest.as_type(cls) assert cast_val == expected_val pp_poly = pp_lowest.as_poly_type(cls) assert pp_poly.addr == SUPREME_POINTER_ADDR assert pp_poly.value == expected_val
def test_unordered_map_type(): def uint32_deferred(field, py_type): assert field == FieldPath() assert py_type is int return ScalarCType(FieldPath(), int, ctypes.c_uint32) def uint16_deferred(field, py_type): assert field == FieldPath() assert py_type is int return ScalarCType(FieldPath(), int, ctypes.c_uint16) mem_type = UnorderedMapType( FieldPath(), UnorderedMap[int, int], uint32_deferred, uint16_deferred ) mem_ctx = MemContext(BytesReader(UNORDERED_MAP_MEM)) uo_map: UnorderedMap = mem_type.from_bytes(UNORDERED_MAP_BYTES, mem_ctx) # Sanity check key fields before we try to call get() assert uo_map.meta.buckets_ptr == 1 assert uo_map.meta.mask == 1 value = uo_map.get(2) assert value == 3
def test_vector_dsl(): vec_wrap_buf = ( b"\xdc" * 9 # offset 1 and unused pointer + b"\x01\x00\x00\x00\x00\x00\x00\x00" + b"\xde" * 4 + b"\x02\x00\x00\x00" ) arr_buf = b"\xff\x03\x00\x09\x00" mem_type = DataclassStruct(FieldPath(), VecWrap) mem_ctx = MemContext(BytesReader(arr_buf)) assert mem_type.from_bytes(vec_wrap_buf, mem_ctx) == VecWrap((3, 9)) uint8_type = ScalarCType(FieldPath(), int, ctypes.c_uint8) uint16_type = ScalarCType(FieldPath(), int, ctypes.c_uint16) @pytest.mark.parametrize( "key_type,val_type,node_bytes,expected", [ ( uint8_type, uint8_type, ( b"\x00\x01" + b"\x00" * 6 + b"\x10" + b"\x00" * 7 + b"\x07"
def read(): sc_type = ScalarCType(FieldPath(), FourEnum, ctypes.c_uint8) sc_type.from_bytes(b"\x00", MemContext())
def test_scalar_c_type_pointer(addr_bytes, expected): sc_type = ScalarCType(FieldPath(), int, ctypes.c_void_p) assert sc_type.from_bytes(addr_bytes, MemContext()) == expected
def test_scalar_c_type_byte(py_type, expected): sc_type = ScalarCType(FieldPath(), py_type, ctypes.c_uint8) assert sc_type.from_bytes(b"\x04", MemContext()) == expected
def uint16_deferred(field, py_type): assert field == FieldPath() assert py_type is int return ScalarCType(FieldPath(), int, ctypes.c_uint16)
def test_vector_model(vec_buf, expected): mem_type = Vector(FieldPath(), Optional[Tuple[int, ...]], sc_uint8) arr_buf = b"\x0a\x0b\x0c\x0d" mem_ctx = MemContext(BytesReader(arr_buf)) assert mem_type.from_bytes(vec_buf, mem_ctx) == expected
def build(): ScalarCType(FieldPath(), int, ctypes.c_float)
def read(): sc_type = ScalarCType(FieldPath(), int, ctypes.c_uint8) sc_type.from_bytes(b"", MemContext())
def test_player(): player_mt = DataclassStruct(FieldPath(), Player) state_bytes = b"\x10\x20" assert player_mt.from_bytes(state_bytes, MemContext()) == Player(16, 32)