def create_int_carray_obj(self, bits, init_val): cint_type = cdm.CIntType('i' + str(bits), bits, False, cdm.ENDIANESS) content = b''.join(map(cint_type.convert_to_c_repr, init_val)) addrspace = VirtualAddressSpace(content) carray_type = cdm.CArrayType(cint_type.bind(addrspace), len(init_val), addrspace) return cdm.CArray(carray_type, 0)
def test_convertFromCRepr_returnsArrayOfCorrectSize(self): carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 5) py_repr = carray_type.convert_from_c_repr( b'\x00\x00\x00\x11\x00\x00\x00\x22\x33\x44\x55\x66') assert py_repr == [0x11, 0x22, 0x33445566, 0, 0]
def test_convertToCRepr_onUtf8WithBigCodepoint_returnsArrayOfCorrectSize( self): carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 4) c_repr = carray_type.convert_to_c_repr('A\u1122') assert c_repr == b'\x00\x00\x00\x41\x00\x00\x11\x22' \ b'\x00\x00\x00\x00\x00\x00\x00\x00'
def test_convertToCRepr_onPyIterable_initializesElementsWithIterablePlusNullVals( self): carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 5) c_repr = carray_type.convert_to_c_repr([0x11, 0x22, 0x33445566]) assert c_repr == b'\x00\x00\x00\x11\x00\x00\x00\x22\x33\x44\x55\x66' \ b'\x00\x00\x00\x00\x00\x00\x00\x00'
def carray_type(cint_type, addrspace): return cdm.CArrayType(cint_type, 10, addrspace)
def test_repr_returnsBaseNamePlusArray(self, unbound_cint_type): cptr_type = cdm.CArrayType(unbound_cint_type, 123).with_attr('attr') assert repr(cptr_type) == 'ts.cint_attr_array123'
def test_eq_onSamePointer_returnsTrue(self, diff_carr_type): basetype = cdm.CIntType('x', 32, True, cdm.ENDIANESS) assert cdm.CArrayType(basetype, 10) != diff_carr_type
def test_bind_bindsAlsoBaseElement(self, addrspace): ctype = cdm.CProxyType(1) carray_type = cdm.CArrayType(ctype, 10) bound_carray_type = carray_type.bind(addrspace) assert bound_carray_type.base_type.__addrspace__ is addrspace
def test_repr_returnsClassNameAndContent(self, cint_type, addrspace): carray_type = cdm.CArrayType(cint_type, 3, addrspace) carray_obj = carray_type([1, 2, 3]) assert repr(carray_obj) == 'ts.cint_array3([1, 2, 3])'
def test_init_onBaseTypeWithDifferentAddrSpaceSet_raisesInvalidAddressSpace( self, cint_type): other_addrspace = VirtualAddressSpace() with pytest.raises(cdm.InvalidAddressSpaceError): _ = cdm.CArrayType(cint_type, 10, other_addrspace)
def test_init_returnsArrayCProxy(self, unbound_cint_type): carray_type = cdm.CArrayType(unbound_cint_type, 10) assert carray_type.__addrspace__ is None assert carray_type.base_type is unbound_cint_type assert carray_type.element_count == 10
class TestCArrayType: def test_init_returnsArrayCProxy(self, unbound_cint_type): carray_type = cdm.CArrayType(unbound_cint_type, 10) assert carray_type.__addrspace__ is None assert carray_type.base_type is unbound_cint_type assert carray_type.element_count == 10 def test_init_onBaseTypeWithDifferentAddrSpaceSet_raisesInvalidAddressSpace( self, cint_type): other_addrspace = VirtualAddressSpace() with pytest.raises(cdm.InvalidAddressSpaceError): _ = cdm.CArrayType(cint_type, 10, other_addrspace) def test_bind_bindsAlsoBaseElement(self, addrspace): ctype = cdm.CProxyType(1) carray_type = cdm.CArrayType(ctype, 10) bound_carray_type = carray_type.bind(addrspace) assert bound_carray_type.base_type.__addrspace__ is addrspace def test_shallowIterSubTypes_returnsBaseType(self, carray_type): assert list(carray_type.shallow_iter_subtypes()) \ == [carray_type.base_type] def test_eq_onSamePointer_returnsTrue(self, cint_type): assert cdm.CPointerType(cint_type, 32, 'little') \ == cdm.CPointerType(cint_type, 32, 'little') @pytest.mark.parametrize('diff_carr_type', [ "othertype", cdm.CArrayType(cdm.CIntType('x', 32, True, cdm.ENDIANESS), 10).with_attr('attr'), cdm.CArrayType(cdm.CIntType('x', 32, True, cdm.ENDIANESS), 1000), cdm.CArrayType(cdm.CIntType('y', 16, False, cdm.ENDIANESS), 10) ]) def test_eq_onSamePointer_returnsTrue(self, diff_carr_type): basetype = cdm.CIntType('x', 32, True, cdm.ENDIANESS) assert cdm.CArrayType(basetype, 10) != diff_carr_type def test_len_returnsSizeOfObject(self, carray_type): assert len(carray_type) == carray_type.element_count def test_sizeof_returnsSizeInBytes(self, carray_type): assert carray_type.sizeof \ == carray_type.element_count * carray_type.base_type.sizeof @patch.object(cdm.CIntType, 'null_val') def test_nullValue_ok(self, null_val, carray_type): assert carray_type.null_val == [null_val] * carray_type.element_count def test_cDefinition_onRefDef_returnsWithRefDef(self, cint_type): assert cint_type.array(12).c_definition('x') == 'cint x[12]' def test_cDefinition_onNoRefDef_returnsWithoutRefDef(self, cint_type): assert cint_type.array(12).c_definition() == 'cint [12]' def test_cDefinition_onArrayOfArrays_ok(self, cint_type): assert cint_type.array(11).array(22).c_definition() == 'cint [22][11]' def test_cDefinition_onArrayOfPtr_ok(self, cint_type): assert cint_type.ptr.array(10).c_definition('x') == 'cint *x[10]' def test_cDefinition_onPtrToArray_ok(self, cint_type): assert cint_type.array(10).ptr.c_definition('x') == 'cint (*x)[10]' def test_repr_returnsBaseNamePlusArray(self, unbound_cint_type): cptr_type = cdm.CArrayType(unbound_cint_type, 123).with_attr('attr') assert repr(cptr_type) == 'ts.cint_attr_array123' def test_convertToCRepr_onPyIterable_initializesElementsWithIterablePlusNullVals( self): carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 5) c_repr = carray_type.convert_to_c_repr([0x11, 0x22, 0x33445566]) assert c_repr == b'\x00\x00\x00\x11\x00\x00\x00\x22\x33\x44\x55\x66' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' def test_convertToCRepr_onUtf8WithBigCodepoint_returnsArrayOfCorrectSize( self): carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 4) c_repr = carray_type.convert_to_c_repr('A\u1122') assert c_repr == b'\x00\x00\x00\x41\x00\x00\x11\x22' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' def test_convertFromCRepr_returnsArrayOfCorrectSize(self): carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 5) py_repr = carray_type.convert_from_c_repr( b'\x00\x00\x00\x11\x00\x00\x00\x22\x33\x44\x55\x66') assert py_repr == [0x11, 0x22, 0x33445566, 0, 0] def test_init_onConstArray_ok(self, cint_type): carray_type = cint_type.with_attr('const').array(1) _ = carray_type() @pytest.mark.parametrize('size', [1, 4]) def test_getAlignment_returnsAlignmentOfBase(self, size, unbound_cint_type): with patch.object(cdm.CIntType, 'alignment', size): carray_type = cdm.CArrayType(unbound_cint_type, 4) assert carray_type.alignment == size
def test_getAlignment_returnsAlignmentOfBase(self, size, unbound_cint_type): with patch.object(cdm.CIntType, 'alignment', size): carray_type = cdm.CArrayType(unbound_cint_type, 4) assert carray_type.alignment == size