<s> import ctypes <EOL> import unittest <EOL> from array import array <EOL> from lantz . foreign import LibraryDriver , RetStr , RetTuple , RetValue , TYPES <EOL> class Array ( array ) : <EOL> @ property <EOL> def _as_parameter_ ( self ) : <EOL> return self . buffer_info ( ) [ <NUM_LIT:0> ] <EOL> def array_len ( self ) : <EOL> return self . buffer_info ( ) <EOL> class MyDriver ( LibraryDriver ) : <EOL> LIBRARY_NAME = '<STR_LIT>' <EOL> class MyWrongDriver ( LibraryDriver ) : <EOL> LIBRARY_NAME = '<STR_LIT>' <EOL> class ForeignTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . driver = MyDriver ( ) <EOL> def test_raise ( self ) : <EOL> self . assertRaises ( OSError , MyWrongDriver , ( ) ) <EOL> def test_simple_return ( self ) : <EOL> a = self . driver . lib . returni10 ( ) <EOL> self . assertEqual ( a , <NUM_LIT:10> ) <EOL> self . assertEqual ( type ( a ) , int ) <EOL> a = self . driver . lib . sumi13 ( <NUM_LIT:5> ) <EOL> self . assertEqual ( a , <NUM_LIT> ) <EOL> self . assertEqual ( type ( a ) , int ) <EOL> def test_return_string ( self ) : <EOL> value = ctypes . create_string_buffer ( b"<STR_LIT>" , <NUM_LIT:20> ) <EOL> value = ( ctypes . c_char * <NUM_LIT:20> ) ( ) <EOL> ret = self . driver . lib . write_in_charp ( value , len ( value ) ) <EOL> value = value . value . decode ( '<STR_LIT:ascii>' ) <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , '<STR_LIT>' , str ) ) <EOL> ret , value = self . driver . lib . write_in_charp ( RetStr ( <NUM_LIT:20> ) , <NUM_LIT:20> ) <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , '<STR_LIT>' , str ) ) <EOL> ret , value = self . driver . lib . write_in_charp ( * RetStr ( <NUM_LIT:20> ) ) <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , '<STR_LIT>' , str ) ) <EOL> def test_atoi ( self ) : <EOL> value = ctypes . create_string_buffer ( b'<STR_LIT>' , <NUM_LIT:20> ) <EOL> ret = self . driver . lib . use_atoi ( value ) <EOL> self . assertEqual ( ret , <NUM_LIT> ) <EOL> ret = self . driver . lib . use_atoi ( '<STR_LIT>' ) <EOL> self . assertEqual ( ret , <NUM_LIT> ) <EOL> def test_return_array ( self ) : <EOL> value = ( ctypes . c_double * <NUM_LIT:3> ) ( ) <EOL> ret = self . driver . lib . double_array_param ( value ) <EOL> value = tuple ( value [ : ] ) <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , tuple ) ) <EOL> ret , value = self . driver . lib . double_array_param ( RetTuple ( '<STR_LIT:d>' , <NUM_LIT:3> ) ) <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , tuple ) ) <EOL> value = ( ctypes . c_double * <NUM_LIT:10> ) ( ) <EOL> ret = self . driver . lib . double_array_length_param ( value , <NUM_LIT:10> ) <EOL> value = tuple ( value [ : ] ) <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , tuple ( range ( <NUM_LIT:10> ) ) , tuple ) ) <EOL> ret , value = self . driver . lib . double_array_length_param ( * RetTuple ( '<STR_LIT:d>' , <NUM_LIT:10> ) ) <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , tuple ( range ( <NUM_LIT:10> ) ) , tuple ) ) <EOL> l = <NUM_LIT:10> <EOL> arr = tuple ( range ( l ) ) <EOL> total = float ( sum ( arr ) ) <EOL> self . driver . lib . internal . sum_double_array_length . restype = ctypes . c_double <EOL> value = ( ctypes . c_double * l ) ( * arr ) <EOL> ret = self . driver . lib . sum_double_array_length ( value , l ) <EOL> self . assertEqual ( ( ret , type ( ret ) ) , ( total , type ( total ) ) ) <EOL> value = Array ( '<STR_LIT:d>' , arr ) <EOL> ret = self . driver . lib . sum_double_array_length ( * value . buffer_info ( ) ) <EOL> self . assertEqual ( ( ret , type ( ret ) ) , ( total , type ( total ) ) ) <EOL> ret = self . driver . lib . sum_double_array_length ( value , l ) <EOL> self . assertEqual ( ( ret , type ( ret ) ) , ( total , type ( total ) ) ) <EOL> ret = self . driver . lib . sum_double_array_length ( * value . array_len ( ) ) <EOL> self . assertEqual ( ( ret , type ( ret ) ) , ( total , type ( total ) ) ) <EOL> def test_return_value ( self ) : <EOL> value = ctypes . c_double ( ) <EOL> ret = self . driver . lib . double_param ( ctypes . byref ( value ) ) <EOL> value = value . value <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , <NUM_LIT> , float ) ) <EOL> ret , value = self . driver . lib . double_param ( RetValue ( '<STR_LIT:d>' ) ) <EOL> self . assertEqual ( ( ret , value , type ( value ) ) , ( <NUM_LIT:1> , <NUM_LIT> , float ) ) </s>
