Пример #1
0
 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
Пример #2
0
 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
Пример #3
0
 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'
                '}')
Пример #4
0
 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
Пример #5
0
 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)
Пример #6
0
 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
Пример #7
0
 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
Пример #8
0
 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
Пример #9
0
 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'
Пример #10
0
 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'
Пример #11
0
 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
Пример #12
0
 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)
Пример #13
0
 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'
Пример #14
0
 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
Пример #15
0
 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'
                '}')
Пример #16
0
 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
     ]
Пример #17
0
 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
Пример #18
0
 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'
                '}')
Пример #19
0
 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']
Пример #20
0
 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}
Пример #21
0
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)])
Пример #22
0
 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))
Пример #23
0
 def test_repr_ok(self):
     cstruct_type = cdm.CStructType('strct_type', []).with_attr('attr')
     assert repr(cstruct_type) == 'ts.struct.attr_strct_type'
Пример #24
0
 def test_getAlignment_onEmptyStruct_returnsPacking(self):
     cstruct_type = cdm.CStructType('strct_name', [], packing=2)
     assert cstruct_type.alignment == 2
Пример #25
0
 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
Пример #26
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
Пример #27
0
 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
Пример #28
0
 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'
Пример #29
0
 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())
Пример #30
0
 def test_init_onPackingIsSet_setsPacking(self, cint_type):
     cstruct_type = cdm.CStructType('name', [], 1)
     assert cstruct_type._packing_ == 1