5
dll = CDLL(_ctypes_test.__file__)
8
CALLBACK_FUNCTYPE = WINFUNCTYPE
10
# fake to enable this test on Linux
11
CALLBACK_FUNCTYPE = CFUNCTYPE
13
class POINT(Structure):
14
_fields_ = [("x", c_int), ("y", c_int)]
16
class BasicWrapTestCase(unittest.TestCase):
17
def wrap(self, param):
20
def test_wchar_parm(self):
25
f = dll._testfunc_i_bhilfd
26
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
27
result = f(self.wrap(1), self.wrap("x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
28
self.failUnlessEqual(result, 139)
29
self.failUnless(type(result), int)
31
def test_pointers(self):
33
f.restype = POINTER(c_int)
34
f.argtypes = [POINTER(c_int)]
36
# This only works if the value c_int(42) passed to the
37
# function is still alive while the pointer (the result) is
42
self.failUnlessEqual(pointer(v).contents.value, 42)
43
result = f(self.wrap(pointer(v)))
44
self.failUnlessEqual(type(result), POINTER(c_int))
45
self.failUnlessEqual(result.contents.value, 42)
48
result = f(self.wrap(pointer(v)))
49
self.failUnlessEqual(result.contents.value, v.value)
51
p = pointer(c_int(99))
52
result = f(self.wrap(p))
53
self.failUnlessEqual(result.contents.value, 99)
55
def test_shorts(self):
56
f = dll._testfunc_callback_i_if
59
expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
60
1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
66
CallBack = CFUNCTYPE(c_int, c_int)
68
cb = CallBack(callback)
69
f(self.wrap(2**18), self.wrap(cb))
70
self.failUnlessEqual(args, expected)
72
################################################################
74
def test_callbacks(self):
75
f = dll._testfunc_callback_i_if
78
MyCallback = CFUNCTYPE(c_int, c_int)
81
#print "called back with", value
84
cb = MyCallback(callback)
86
result = f(self.wrap(-10), self.wrap(cb))
87
self.failUnlessEqual(result, -18)
90
f.argtypes = [c_int, MyCallback]
91
cb = MyCallback(callback)
93
result = f(self.wrap(-10), self.wrap(cb))
94
self.failUnlessEqual(result, -18)
96
result = f(self.wrap(-10), self.wrap(cb))
97
self.failUnlessEqual(result, -18)
99
AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
101
# check that the prototype works: we call f with wrong
103
cb = AnotherCallback(callback)
104
self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
106
def test_callbacks_2(self):
107
# Can also use simple datatypes as argument type specifiers
108
# for the callback function.
109
# In this case the call receives an instance of that type
110
f = dll._testfunc_callback_i_if
113
MyCallback = CFUNCTYPE(c_int, c_int)
115
f.argtypes = [c_int, MyCallback]
118
#print "called back with", value
119
self.failUnlessEqual(type(value), int)
122
cb = MyCallback(callback)
123
result = f(self.wrap(-10), self.wrap(cb))
124
self.failUnlessEqual(result, -18)
126
def test_longlong_callbacks(self):
128
f = dll._testfunc_callback_q_qf
129
f.restype = c_longlong
131
MyCallback = CFUNCTYPE(c_longlong, c_longlong)
133
f.argtypes = [c_longlong, MyCallback]
136
self.failUnless(isinstance(value, int))
137
return value & 0x7FFFFFFF
139
cb = MyCallback(callback)
141
self.failUnlessEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))
143
def test_byval(self):
147
# EXPORT int _testfunc_byval(point in, point *pout)
148
result = dll._testfunc_byval(ptin, byref(ptout))
149
got = result, ptout.x, ptout.y
151
self.failUnlessEqual(got, expected)
154
ptin = POINT(101, 102)
156
dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
157
dll._testfunc_byval.restype = c_int
158
result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
159
got = result, ptout.x, ptout.y
160
expected = 203, 101, 102
161
self.failUnlessEqual(got, expected)
163
def test_struct_return_2H(self):
164
class S2H(Structure):
165
_fields_ = [("x", c_short),
167
dll.ret_2h_func.restype = S2H
168
dll.ret_2h_func.argtypes = [S2H]
170
s2h = dll.ret_2h_func(self.wrap(inp))
171
self.failUnlessEqual((s2h.x, s2h.y), (99*2, 88*3))
173
def test_struct_return_8H(self):
174
class S8I(Structure):
175
_fields_ = [("a", c_int),
183
dll.ret_8i_func.restype = S8I
184
dll.ret_8i_func.argtypes = [S8I]
185
inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
186
s8i = dll.ret_8i_func(self.wrap(inp))
187
self.failUnlessEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
188
(9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
190
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192
class AsParamWrapper(object):
193
def __init__(self, param):
194
self._as_parameter_ = param
196
class AsParamWrapperTestCase(BasicWrapTestCase):
197
wrap = AsParamWrapper
199
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201
class AsParamPropertyWrapper(object):
202
def __init__(self, param):
205
def getParameter(self):
207
_as_parameter_ = property(getParameter)
209
class AsParamPropertyWrapperTestCase(BasicWrapTestCase):
210
wrap = AsParamPropertyWrapper
212
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214
if __name__ == '__main__':