Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 健全性检查:我是否以正确的方式使用ctypes?_Python_Unit Testing_Ctypes_Python 2.5 - Fatal编程技术网

Python 健全性检查:我是否以正确的方式使用ctypes?

Python 健全性检查:我是否以正确的方式使用ctypes?,python,unit-testing,ctypes,python-2.5,Python,Unit Testing,Ctypes,Python 2.5,这是一个后续行动 我终于可以开始测试了,但我不确定我是否做得对,因为地址是包含结构的结构列表中的地址。在C中,使用宏进行计算 我想我做得对,但是我不确定我是否正确设置了argtypes。因此我想听听你的意见 注:被测目标函数具有以下特征: void function( ulong startAddress, ulong endAddress, ulong curAddress, int nofPaddingElements ) 注2:我必须用真正的硬件来测试这一点,因此目前我没有实现很多快捷方

这是一个后续行动

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

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

注:被测目标函数具有以下特征:

void function( ulong startAddress, ulong endAddress, ulong curAddress, int nofPaddingElements )
注2:我必须用真正的硬件来测试这一点,因此目前我没有实现很多快捷方式

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

1) 缓冲区结构的类型定义。包含指向其他结构的指针,但不是指针,而是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函数):

我翻译成python函数的宏(同样,我尽可能接近C代码)

这一切是如何结合在一起的

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

我是做错了还是太复杂了?或者我做得对吗?

如果
create\u list
是一个C函数,为什么要使用Python实现?您确定它希望通过值而不是引用传递
My\u Buffer\t
struct吗?
list\u ptr
如何与传入的结构相关?
argtypes
需要是一个序列,因此它是
get\u data\u address.argtypes=(My\u Buffer\u t,)
。注意逗号。另外,在这两种情况下都没有设置
restype
,因此它使用默认的
c_int
类型,而我想您希望
restype=c_void\p
来避免截断指针。
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