def test_init_onNameIsNone_returnsAnonymousStructPlusUniqueId( self, unbound_cint_type): anon1_cstrct_type = cdm.CStructType(None, [('m', unbound_cint_type)]) anon2_cstrct_type = cdm.CStructType(None, [('m', unbound_cint_type)]) assert re.match(r'__anonymous_\d*__', anon1_cstrct_type.struct_name) assert re.match(r'__anonymous_\d*__', anon2_cstrct_type.struct_name) assert anon1_cstrct_type.struct_name != anon2_cstrct_type.struct_name
def test_eq_onNestedStructsWithDifferentInnerStruct_returnsFalse( self, unbound_cint_type, unbound_cint16_type): inner1_cstruct_type = cdm.CStructType('inner1', [('m', unbound_cint_type)]) outer1_cstruct_type = cdm.CStructType('outer', [('m', inner1_cstruct_type)]) inner2_cstruct_type = cdm.CStructType('inner2', [('m', unbound_cint16_type)]) outer2_cstruct_type = cdm.CStructType('outer', [('m', inner2_cstruct_type)]) assert outer1_cstruct_type != outer2_cstruct_type
def test_cDefinitionFull_onNestedAnonymousStructs_indentCorrectly( self, unbound_cint_type): nested_anonym_cstruct_type = cdm.CStructType( 'nested_anonym_strct', [('inner_strct', cdm.CStructType(None, [('member', unbound_cint_type)]))]) assert nested_anonym_cstruct_type.c_definition_full() \ == ('struct nested_anonym_strct {\n' '\tstruct {\n' '\t\tcint member;\n' '\t} inner_strct;\n' '}')
def test_delayedDef_setsMembers(self, unbound_cint_type, unbound_cint16_type): cstruct_type = cdm.CStructType('strct') cstruct_type.delayed_def([('member1', unbound_cint_type), ('member2', unbound_cint16_type)]) assert cstruct_type.member1 is unbound_cint_type assert cstruct_type.member2 is unbound_cint16_type
def test_convertFromCRepr_readAppendedFields(self, unbound_cint_type): cstruct_type = cdm.CStructType('sname', [('member1', unbound_cint_type), ('member2', unbound_cint_type)]) assert cstruct_type.convert_from_c_repr( b'\x11\x11\x11\x11\x22\x22\x22\x22') \ == dict(member1=0x11111111, member2=0x22222222)
def test_offsetof_onPackingSmallerThanMemberAlignment_alignByPacking( self, unbound_cint16_type, unbound_cint_type): cstruct_type = cdm.CStructType('strct_name', [('member_int0', unbound_cint16_type), ('member_int1', unbound_cint_type)], packing=2) assert cstruct_type.offsetof['member_int1'] == 2
def test_offsetof_onAdrSmallerThanMembersAligment_addPadding( self, unbound_cint_type, unbound_cint16_type): cstruct_type = cdm.CStructType('strct_name', [('member_int0', unbound_cint16_type), ('member_int1', unbound_cint_type)]) assert cstruct_type.offsetof['member_int0'] == 0 assert cstruct_type.offsetof['member_int1'] == 4
def test_GetAttr_OnStructMemberWithReservedName_returnsReservedObj( self, name, cint_type, addrspace): cstruct_type = cdm.CStructType('StructWithReservedMemberNames', members=[(name, cint_type)], addrspace=addrspace) cstruct_obj = cstruct_type(123) assert getattr(cstruct_obj, name) != 123
def test_convertToCRepr_onMissingField_assumesNullForField( self, unbound_cint_type): cstruct_type = cdm.CStructType('sname', [('member1', unbound_cint_type), ('member2', unbound_cint_type)]) c_repr = cstruct_type.convert_to_c_repr({'member1': 0x11111111}) assert c_repr == b'\x11\x11\x11\x11\x00\x00\x00\x00'
def test_convertToCRepr_appendsFields(self, unbound_cint_type): cstruct_type = cdm.CStructType('sname', [('member1', unbound_cint_type), ('member2', unbound_cint_type)]) assert cstruct_type.convert_to_c_repr(dict(member1=0x11111111, member2=0x22222222)) \ == b'\x11\x11\x11\x11\x22\x22\x22\x22'
def test_sizeof_onNoExplicitPacking_returnsSizeOfUnpackedStruct( self, unbound_cint_type, unbound_cint16_type): unpacked_cstruct_type = cdm.CStructType('unpacked_struct', [('m1', unbound_cint16_type), ('m2', unbound_cint_type), ('m3', unbound_cint16_type)]) assert unpacked_cstruct_type.sizeof == 3 * 4
def test_convertFromCRepr_onPadding_ignoresSpaceBetweenFields( self, unbound_cint16_type, unbound_cint_type): cstruct_type = cdm.CStructType('sname', [('member1', unbound_cint16_type), ('member2', unbound_cint_type)]) assert cstruct_type.convert_from_c_repr( b'\x11\x11\x00\xFF\x22\x22\x22\x22') \ == dict(member1=0x1111, member2=0x22222222)
def test_convertToCRepr_onPadding_addsZeros(self, unbound_cint16_type, unbound_cint_type): cstruct_type = cdm.CStructType('sname', [('member1', unbound_cint16_type), ('member2', unbound_cint_type)]) assert cstruct_type.convert_to_c_repr(dict(member1=0x1111, member2=0x22222222)) \ == b'\x11\x11\x00\x00\x22\x22\x22\x22'
def test_offsetof_onMultipleSmallAndOneBig_addNoPadding( self, unbound_cint16_type, unbound_cint_type): cstruct_type = cdm.CStructType('strct_name', [('member_int0', unbound_cint16_type), ('member_int1', unbound_cint16_type), ('member_int2', unbound_cint_type)]) assert cstruct_type.offsetof['member_int1'] == 2 assert cstruct_type.offsetof['member_int2'] == 4
def test_cDefinitionFull_onNestedStructs_notRecursive( self, unbound_cstruct_type): nested_cstruct_type = cdm.CStructType( 'nested_cstruct_type', [('inner_strct', unbound_cstruct_type)]) assert nested_cstruct_type.c_definition_full() \ == ('struct nested_cstruct_type {\n' '\tstruct strct_name inner_strct;\n' '}')
def test_iter_ok(self, unbound_cint_type, unbound_cint16_type, unbound_cuint64_type): cstruct_type = cdm.CStructType('sname', [('member1', unbound_cint16_type), ('member2', unbound_cint_type), ('member3', unbound_cuint64_type)]) assert list(cstruct_type) == [ unbound_cint16_type, unbound_cint_type, unbound_cuint64_type ]
def test_sizeof_onPacking1_returnsSizeOfPackedStruct( self, unbound_cint_type, unbound_cint16_type): packed_struct = cdm.CStructType('packed_struct', [('m1', unbound_cint16_type), ('m2', unbound_cint_type), ('m3', unbound_cint16_type)], packing=1) assert packed_struct.sizeof \ == 2*unbound_cint16_type.sizeof + unbound_cint_type.sizeof
def test_cDefinitionFull_onMembers_addsOneMemberPerLine( self, unbound_cint_type, unbound_cint16_type): cstruct_type = cdm.CStructType('strct_name', [('i', unbound_cint_type), ('i16', unbound_cint16_type)]) assert cstruct_type.c_definition_full() \ == ('struct strct_name {\n' '\tcint i;\n' '\tcint16 i16;\n' '}')
def test_init_returnsCStructType(self, unbound_cint_type, unbound_cint16_type): cstruct_type = cdm.CStructType('strct_name', [('member_int', unbound_cint_type), ('member_short', unbound_cint16_type)]) assert isinstance(cstruct_type, cdm.CStructType) assert cstruct_type._members_ \ == {'member_int': unbound_cint_type, 'member_short': unbound_cint16_type} assert cstruct_type._members_order_ == ['member_int', 'member_short']
def test_getVal_onNestedStruct_ok(self, cstruct_type, cint_type, addrspace): nested_cstruct_obj = cdm.CStructType('nested_cstruct_obj', members=[('struct', cstruct_type), ('int', cint_type)], addrspace=addrspace) nested_struct = nested_cstruct_obj(struct={ 'member_int': 2, 'member_short': 99 }, int=888) assert nested_struct.val == \ {'struct':{'member_int':2, 'member_short':99, 'member_int2':0}, 'int':888}
def unbound_cstruct_type(unbound_cint_type, unbound_cint16_type): return cdm.CStructType('strct_name', [('member_int', unbound_cint_type), ('member_short', unbound_cint16_type), ('member_int2', unbound_cint_type)])
def test_convertToCRepr_onInvalidMemberName_returnsValueError( self, unbound_cint_type): cstruct_type = cdm.CStructType('sname', [('member', unbound_cint_type)]) with pytest.raises(ValueError): _ = cstruct_type.convert_to_c_repr(dict(invalid_member=1))
def test_repr_ok(self): cstruct_type = cdm.CStructType('strct_type', []).with_attr('attr') assert repr(cstruct_type) == 'ts.struct.attr_strct_type'
def test_getAlignment_onEmptyStruct_returnsPacking(self): cstruct_type = cdm.CStructType('strct_name', [], packing=2) assert cstruct_type.alignment == 2
def test_offsetof_ofFirstItem_returns0(self, unbound_cint_type): cstruct_type = cdm.CStructType('strct_name', [('member_int0', unbound_cint_type)]) assert cstruct_type.offsetof['member_int0'] == 0
def test_getAlignment_onPackingSmallerThanMaxMemberAlignment_returnsPacking( self, unbound_cint_type): cstruct_type = cdm.CStructType('strct_name', [('member', unbound_cint_type)], packing=2) assert cstruct_type.alignment == 2
def test_getAlignment_returnsMaxAlignmentOfMembers(self, unbound_cint_type, unbound_cuint64_type): cstruct_type = cdm.CStructType('strct_name', [('member_max', unbound_cint_type), ('member', unbound_cuint64_type)]) assert cstruct_type.alignment == unbound_cuint64_type.alignment
def test_convertToCRepr_onIterable_mapsToFields(self, unbound_cint_type): cstruct_type = cdm.CStructType('sname', [('member1', unbound_cint_type), ('member2', unbound_cint_type)]) c_repr = cstruct_type.convert_to_c_repr(iter([1, 2])) assert c_repr == b'\x01\x00\x00\x00\x02\x00\x00\x00'
def test_init_onMemberWithDifferentAddrSpaceSet_raisesInvalidAddressSpaceError( self, cint_type): with pytest.raises(cdm.InvalidAddressSpaceError): _ = cdm.CStructType('strct_name', [('bound_to_different_addrspace', cint_type)], addrspace=VirtualAddressSpace())
def test_init_onPackingIsSet_setsPacking(self, cint_type): cstruct_type = cdm.CStructType('name', [], 1) assert cstruct_type._packing_ == 1