comparison python/pydc/test/types.py @ 29:6cc2b7fc7ea2

bigger pydc update: - cleanups and refactoring - python 2 fixes in var conversions (especially w/ respect to int vs long) - fix to pydc.free() which didn't work at all - fix to return python bool as actual bool - test lib covering all conversions (manual verification, though :-/)
author Tassilo Philipp
date Wed, 08 Apr 2020 22:17:43 +0200
parents
children 2682a627168c
comparison
equal deleted inserted replaced
28:edbbd467f50a 29:6cc2b7fc7ea2
1 import pydc
2 import sys
3 import platform
4
5
6
7
8 def theader(title):
9 print(title)
10 print('%8s %20s %16s %-20s %12s %-12s -> %16s %-16s # %s' % ('DC_SIG', 'C_RET_T', 'C_FSYM', 'C_PARAMLIST', 'PYTHON_ARG_T', 'IN_ARGS', 'RET_VAL', 'PYTHON_RET_T', 'NOTES'))
11
12
13 def t(lib, dcsig, c_rtype, c_fsym, c_paramlist, extra_msg, *args):
14 try:
15 fp = pydc.find(lib, c_fsym)
16 r = pydc.call(fp, dcsig, *args)
17 rt = type(r).__name__
18 except:
19 r = '[EXCEPTION]'
20 rt = '!'
21 extra_msg += ' "'+str(sys.exc_info()[1])+'"'
22
23 inarg_types = '('+','.join(map(lambda x: type(x).__name__, args))+')'
24 inargs = ','.join(map(str, args))
25 print('%8s %20s %16s %-20s %12s %-12s -> %16.16s %-16s # %s' % (dcsig, c_rtype, c_fsym, c_paramlist, inarg_types, inargs, str(r), '('+rt+')', extra_msg))
26
27
28
29 # some libc tests ------------------
30
31 try:
32 if sys.platform == "win32":
33 libc = pydc.load("msvcrt")
34 elif sys.platform == "darwin":
35 libc = pydc.load("/usr/lib/libc.dylib")
36 elif "bsd" in sys.platform:
37 libc = pydc.load("/usr/lib/libc.so")
38 #libc = pydc.load("/lib/libc.so.7")
39 elif platform.architecture()[0] == "64bit":
40 libc = pydc.load("/lib64/libc.so.6")
41 else:
42 libc = pydc.load("/lib/libc.so.6")
43
44 theader('CLIB TESTS:')
45
46 # void()
47 t(libc, ")v", "void", "sranddev", "(void)", '')
48
49 # int()
50 t(libc, ")i", "int", "rand", "(void)", '')
51
52 # void(unsigned int)
53 t(libc, "I)v", "void", "srand", "(int)", '', 123)
54
55 # int() (and one different helper call for test)
56 t(libc, ")i", "int", "rand", "(void)", 'with seed <------,')
57 t(libc, "I)v", "void", "srand", "(int)", 'set same seed |', 123)
58 t(libc, ")i", "int", "rand", "(void)", 'should be same result...')
59 t(libc, ")i", "int", "rand", "(void)", '...and now different result')
60
61 # int(int)
62 t(libc, "i)i", "int", "abs", "(int)", ' 10 => 10', 10)
63 t(libc, "i)i", "int", "abs", "(int)", ' 0 => 0', 0)
64 t(libc, "i)i", "int", "abs", "(int)", ' -9209 => 9209', -9209)
65
66 # long(long)
67 t(libc, "j)j", "long", "labs", "(long)", ' 48 => 48', 48)
68 t(libc, "j)j", "long", "labs", "(long)", ' 0 => 0', 0)
69 t(libc, "j)j", "long", "labs", "(long)", '-4271477497 => 4271477497', -4271477497)
70
71 # long long(long long)
72 t(libc, "l)l", "long long", "labs", "(long long)", ' 6334810198 => 6334810198', 6334810198)
73 t(libc, "l)l", "long long", "labs", "(long long)", ' 1 => 1', 1)
74 t(libc, "l)l", "long long", "labs", "(long long)", ' 0 => 0', 0)
75 t(libc, "l)l", "long long", "labs", "(long long)", ' -1 => 1', -1)
76 t(libc, "l)l", "long long", "labs", "(long long)", '-7358758407 => 7358758407', -7358758407)
77
78 pydc.free(libc)
79 except:
80 print("skipping clib tests because: "+str(sys.exc_info()[1]))
81
82
83 # tests with own .so for testing all conversions ------------------
84
85 l = pydc.load(sys.path[0]+"/test.so")
86
87 # test all possible arg types and their conversions to and from python, with
88 # specific focus/tests in areas where python 2 and 3 differ
89 theader('ARG & RET TYPE CONVERSION TESTS:')
90 t(l, "B)B", "int", "i_plus_one", "(int)", ' False => True (using int func in C)', False)
91 t(l, "c)c", "char", "c_plus_one", "(char)", ' "a" (97) => 98', 'a')
92 t(l, "C)C", "unsigned char", "uc_plus_one", "(unsigned char)", ' "a" (97) => 98', 'a')
93 t(l, "c)c", "char", "c_plus_one", "(char)", ' -2 => -1', -2)
94 t(l, "C)C", "unsigned char", "uc_plus_one", "(unsigned char)", ' 10 => 11', 10)
95 t(l, "s)s", "short", "s_plus_one", "(short)", ' 10 => 11', 10)
96 t(l, "S)S", "unsigned short", "us_plus_one", "(unsigned short)", ' 10 => 11', 10)
97 t(l, "i)i", "int", "i_plus_one", "(int)", ' 10 => 11', 10)
98 t(l, "I)I", "unsigned int", "ui_plus_one", "(unsigned int)", ' 10 => 11', 10)
99 t(l, "j)j", "long", "l_plus_one", "(long)", ' 10 => 11', 10)
100 t(l, "J)J", "unsigned long", "ul_plus_one", "(unsigned long)", ' 10 => 11', 10)
101 t(l, "l)l", "long long", "ll_plus_one", "(long long)", ' 10 => 11', 10)
102 t(l, "L)L", "unsigned long long", "ull_plus_one", "(unsigned long long)", ' 10 => 11', 10)
103 t(l, "f)f", "float", "f_plus_one", "(float)", ' -1.23 => -0.23', -1.23)
104 t(l, "d)d", "double", "d_plus_one", "(double)", ' 5.67 => 6.67', 5.67)
105 t(l, "Z)Z", "const char*", "cc_plus_one", "(const char*)", '"lose char" => "ose char"', 'lose char')
106 t(l, "p)Z", "const char*", "cc_plus_one", "(const char*)", '"w/pointer" => "/pointer"', 'w/pointer')
107 t(l, "p)p", "const char*", "cc_plus_one", "(const char*)", ' "x" => p+1 (retval is prob odd addr)', 'x')
108 t(l, "p)p", "const char*", "cc_plus_one", "(const char*)", ' 0xdeadc0de => 0xdeadc0de+1=3735929055',0xdeadc0de)
109
110 # tested checked value conversions
111 theader('ARG & RET TYPE CONVERSION TESTS FOR RANGE CHECKED TYPES:')
112 t(l, "c)c", "char", "c_plus_one", "(char)", ' "~" => 127', '~')
113 t(l, "c)c", "char", "c_plus_one", "(char)", ' "~" => 127', '~')
114 t(l, "c)c", "char", "c_plus_one", "(char)", ' "" => input exc:', '')
115 t(l, "c)c", "char", "c_plus_one", "(char)", ' "" => input exc:', '')
116 t(l, "C)C", "unsigned char", "uc_plus_one", "(unsigned char)", ' "ab" => input exc:', 'ab')
117 t(l, "C)C", "unsigned char", "uc_plus_one", "(unsigned char)", ' "ab" => input exc:', 'ab')
118
119 t(l, "c)c", "char", "c_plus_one", "(char)", ' -128 => -127', -128)
120 t(l, "c)c", "char", "c_plus_one", "(char)", ' 127 => -128 (wrapped)', 127)
121 t(l, "c)c", "char", "c_plus_one", "(char)", ' -129 => input exc:', -129)
122 t(l, "c)c", "char", "c_plus_one", "(char)", ' 128 => input exc:', 128)
123
124 t(l, "C)C", "unsigned char", "uc_plus_one", "(unsigned char)", ' 0 => 1', 0)
125 t(l, "C)C", "unsigned char", "uc_plus_one", "(unsigned char)", ' 255 => 0 (wrapped)', 255)
126 t(l, "C)C", "unsigned char", "uc_plus_one", "(unsigned char)", ' -1 => input exc:', -1)
127 t(l, "C)C", "unsigned char", "uc_plus_one", "(unsigned char)", ' 256 => input exc:', 256)
128
129 t(l, "s)s", "short", "s_plus_one", "(short)", ' -32768 => -32767', -32768)
130 t(l, "s)s", "short", "s_plus_one", "(short)", ' 32767 => -32768 (wrapped)', 32767)
131 t(l, "s)s", "short", "s_plus_one", "(short)", ' -32769 => input exc:', -32769)
132 t(l, "s)s", "short", "s_plus_one", "(short)", ' 32768 => input exc:', 32768)
133
134 t(l, "S)S", "unsigned short", "us_plus_one", "(unsigned short)", ' 0 => 1', 0)
135 t(l, "S)S", "unsigned short", "us_plus_one", "(unsigned short)", ' 65535 => 0 (wrapped)', 65535)
136 t(l, "S)S", "unsigned short", "us_plus_one", "(unsigned short)", ' -1 => input exc:', -1)
137 t(l, "S)S", "unsigned short", "us_plus_one", "(unsigned short)", ' 65536 => input exc:', 65536)
138
139
140 # int
141 # rand(void);
142 #rand()
143 #define DC_SIGCHAR_VOID 'v' ra
144 #define DC_SIGCHAR_BOOL 'B'
145 #define DC_SIGCHAR_CHAR 'c' 2 versions
146 #define DC_SIGCHAR_UCHAR 'C'
147 #define DC_SIGCHAR_SHORT 's'
148 #define DC_SIGCHAR_USHORT 'S'
149 #define DC_SIGCHAR_INT 'i' r
150 #define DC_SIGCHAR_UINT 'I' a
151 #define DC_SIGCHAR_LONG 'j' ra
152 #define DC_SIGCHAR_ULONG 'J'
153 #define DC_SIGCHAR_LONGLONG 'l' ra
154 #define DC_SIGCHAR_ULONGLONG 'L'
155 #define DC_SIGCHAR_FLOAT 'f'
156 #define DC_SIGCHAR_DOUBLE 'd'
157 #define DC_SIGCHAR_POINTER 'p'
158 #define DC_SIGCHAR_STRING 'Z'
159 #define DC_SIGCHAR_STRUCT 'T'
160 #define DC_SIGCHAR_ENDARG ')' /* also works for end struct */
161
162
163
164 # SIG | FROM PYTHON 2 | FROM PYTHON 3 @@@ | C/C++ | TO PYTHON 2 | TO PYTHON 3 @@@
165 # ----+------------------------------------+------------------------------------+---------------------------------+------------------------------------+-----------------------------------
166 # 'v' | | | void | ?NoneType (returned for "xxx)v") | NoneType (returned for "xxx)v")
167 # 'B' | ?PyBool | ?PyBool | bool | ?PyBool | ?PyBool
168 # 'c' | ?PyInt (range checked) | ?PyLong (range checked) | char | ?PyInt | ?PyLong
169 # 'C' | ?PyInt (range checked) | ?PyLong (range checked) | unsigned char | ?PyInt | ?PyLong
170 # 's' | ?PyInt (range checked) | ?PyLong (range checked) | short | ?PyInt | ?PyLong
171 # 'S' | ?PyInt (range checked) | ?PyLong (range checked) | unsigned short | ?PyInt | ?PyLong
172 # 'i' | ?PyInt | ?PyLong | int | ?PyInt | ?PyLong
173 # 'I' | ?PyInt | ?PyLong | unsigned int | ?PyInt | ?PyLong
174 # 'j' | ?PyLong | ?PyLong | long | ?PyLong | ?PyLong
175 # 'J' | ?PyLong | ?PyLong | unsigned long | ?PyLong | ?PyLong
176 # 'l' | ?PyLongLong | ?PyLongLong | long long | ?PyLongLong | ?PyLongLong
177 # 'L' | ?PyLongLong | ?PyLongLong | unsigned long long | ?PyLongLong | ?PyLongLong
178 # 'f' | ?PyFloat (cast to single precision) | ?PyFloat (cast to single precision) | float | ?PyFloat (cast to double precision) | ?PyFloat (cast to double precision)
179 # 'd' | ?PyFloat | ?PyFloat | double | ?PyFloat | ?PyFloat
180 # 'p' | ?PyUnicode/PyString/PyLong | ?PyUnicode/PyBytes/PyLong | void* | ?Py_ssize_t | ?Py_ssize_t
181 # 'Z' | ?PyUnicode/PyString | ?PyUnicode/PyBytes | const char* (UTF-8 for unicode) | ?PyString | ?PyUnicode
182