理智检查:我是否以正确的方式使用ctypes?

时间:2015-04-20 12:18:54

标签: python unit-testing ctypes python-2.5

这是Getting the end-memory address of a memory-range via python / ctypes

的后续行动

我终于可以开始我的测试了,但是我不确定我是否做得对,因为地址是包含结构的结构列表中的地址。在C中使用宏计算。

我想我做得对,但我不确定我是否正确设置了argtypes。因此,希望你的意见。

注意:测试中的目标函数具有以下足迹:

void function( ulong startAddress, ulong endAddress, ulong curAddress, int nofPaddingElements )

注意2:我必须针对真实硬件进行测试,因此此时我没有实现许多快捷方式。

我不允许在这里发布真实代码,所以我会尽可能地给出伪代码。

1)buffer-struct的类型定义。包含指向其他结构的指针,但不是指针,而是指ulongs。

class My_Buffer_t( ctypes.Structure ):
    _fields_ = [
       ( "some_bool" , ctypes.c_bool ),
       ( "head",       ctypes.c_ulong ),
       ( "tail",       ctypes.c_ulong ),
       ( "headSize",   ctypes.c_ulong ),
       ( "dataSize",   ctypes.c_ulong ) ]

主数据结构,存在于2个其他结构中

class Struct1( ctypes.Structure ):
    _fields_ = [
       ( "entry1", Struct2 ),
       ( "entry2", Struct3 ) ]

class Struct2( ctypes.Structure ):
# Only contains basic types
    _fields_ = [
       ( "data1", ctypes.c_int ),
       ...
       ( "datax", cyptes.c_ushort ) ]

class Struct3( ctypes.Structure ):
# Mix of basic types and 1 other structure
    _fields_ = [
       ( "data1", ctypes.c_double ),
       ...
       ( "datax", cyptes.c_int ),

       ( "timestamp_data1", TimeStampStruct ),
       ...
       ( "timestamp_datax", TimeStampStruct ) ]

class TimeStampStruct( ctypes.Structure ):
    _fields_ = [
       ( "field1", ctypes.c_ulong ),
       ( "field2", ctypes.c_ulong ) ]

列表创建功能(我基本上复制了C功能):

def create_list( bufsize, buffer ):
    # buffer is of type My_Buffer_t

    # It actually creates some shared memory where headSize also needed to be divided by 
    # ctypes.sizeof( ctypes.c_size_t )
    # And afterwards again was multiplied by ctypes.sizeof( ctypes.c_size_t )
    # But since the shared-memory is not of importance for the function-under test I left
    # that part out.
    headSize = (ctypes.c_size_t )( ( ctypes.sizeof(
            My_Buffer_t ) + ctypes.sizeof( ctypes.c_size_t ) - 1 ) )
    dataSize = ( ctypes.c_size_t ) ( ( bufsize + ctypes.sizeof(
            ctypes.c_size_t ) - 1 ) / ctypes.sizeof( ctypes.c_size_t ) )

    buffer.some_bool = True
    buffer.head      = 0
    buffer.tail      = 0
    buffer.headSize  = (ctypes.c_ulong)(headSize)
    buffer.dataSize  = (ctypes.c_ulong)(dataSize)

    return buffer

我转换为python函数的宏(我再次尽可能接近C代码)

def get_data_address( list_ptr ):
    # list_ptr is of type My_buffer_t, but the data is of type Struct1
    return( ( ctypes.c_ulong )( ctypes.c_void_p )( ctypes.c_byte )(
            list_ptr ) + ( list_ptr.headSize ) )

这一切如何结合在一起

max_entries = 100000
list_size   = max_entries * ctypes.sizeof( Struct1 )

# First argtype use:
create_list.argtypes = ( ctypes.c_ulong, My_Buffer_t )
list_ptr             = create_list( list_size, My_Buffer_t() )

# Determine the addresses (2nd use of argtypes):
get_data_address.argtypes = ( My_Buffer_t )
startAddress              = get_data_address( list_ptr )
endAddress                = startAddress + list_size
curAddress                = startAddress
nofPaddingElements        = ctypes.sizeof( struct3 ) / ctypes.sizeof( ctypes.c_int )

# Call the SUT (final use of argtypes)
function.argtypes = ( ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_int )
function( startAddress, endAddress, curAddress, nofPaddingElements )

# rest of the test is simple basic python

我在这里做错了还是过于复杂?或者我做得对吗?

0 个答案:

没有答案