Debian Codegraph @

Lib/test/test_ctypes/test_cfuncs.py

raw
1 import ctypes
2 import unittest
3 from ctypes import (CDLL,
4 c_byte, c_ubyte, c_char,
5 c_short, c_ushort, c_int, c_uint,
6 c_long, c_ulong, c_longlong, c_ulonglong,
7 c_float, c_double, c_longdouble)
8 from test.support import import_helper
9 _ctypes_test = import_helper.import_module("_ctypes_test")
10
11
12 class CFunctions(unittest.TestCase):
13 _dll = CDLL(_ctypes_test.__file__)
14
15 def S(self):
16 return c_longlong.in_dll(self._dll, "last_tf_arg_s").value
17 def U(self):
18 return c_ulonglong.in_dll(self._dll, "last_tf_arg_u").value
19
20 def test_byte(self):
21 self._dll.tf_b.restype = c_byte
22 self._dll.tf_b.argtypes = (c_byte,)
23 self.assertEqual(self._dll.tf_b(-126), -42)
24 self.assertEqual(self.S(), -126)
25
26 def test_byte_plus(self):
27 self._dll.tf_bb.restype = c_byte
28 self._dll.tf_bb.argtypes = (c_byte, c_byte)
29 self.assertEqual(self._dll.tf_bb(0, -126), -42)
30 self.assertEqual(self.S(), -126)
31
32 def test_ubyte(self):
33 self._dll.tf_B.restype = c_ubyte
34 self._dll.tf_B.argtypes = (c_ubyte,)
35 self.assertEqual(self._dll.tf_B(255), 85)
36 self.assertEqual(self.U(), 255)
37
38 def test_ubyte_plus(self):
39 self._dll.tf_bB.restype = c_ubyte
40 self._dll.tf_bB.argtypes = (c_byte, c_ubyte)
41 self.assertEqual(self._dll.tf_bB(0, 255), 85)
42 self.assertEqual(self.U(), 255)
43
44 def test_short(self):
45 self._dll.tf_h.restype = c_short
46 self._dll.tf_h.argtypes = (c_short,)
47 self.assertEqual(self._dll.tf_h(-32766), -10922)
48 self.assertEqual(self.S(), -32766)
49
50 def test_short_plus(self):
51 self._dll.tf_bh.restype = c_short
52 self._dll.tf_bh.argtypes = (c_byte, c_short)
53 self.assertEqual(self._dll.tf_bh(0, -32766), -10922)
54 self.assertEqual(self.S(), -32766)
55
56 def test_ushort(self):
57 self._dll.tf_H.restype = c_ushort
58 self._dll.tf_H.argtypes = (c_ushort,)
59 self.assertEqual(self._dll.tf_H(65535), 21845)
60 self.assertEqual(self.U(), 65535)
61
62 def test_ushort_plus(self):
63 self._dll.tf_bH.restype = c_ushort
64 self._dll.tf_bH.argtypes = (c_byte, c_ushort)
65 self.assertEqual(self._dll.tf_bH(0, 65535), 21845)
66 self.assertEqual(self.U(), 65535)
67
68 def test_int(self):
69 self._dll.tf_i.restype = c_int
70 self._dll.tf_i.argtypes = (c_int,)
71 self.assertEqual(self._dll.tf_i(-2147483646), -715827882)
72 self.assertEqual(self.S(), -2147483646)
73
74 def test_int_plus(self):
75 self._dll.tf_bi.restype = c_int
76 self._dll.tf_bi.argtypes = (c_byte, c_int)
77 self.assertEqual(self._dll.tf_bi(0, -2147483646), -715827882)
78 self.assertEqual(self.S(), -2147483646)
79
80 def test_uint(self):
81 self._dll.tf_I.restype = c_uint
82 self._dll.tf_I.argtypes = (c_uint,)
83 self.assertEqual(self._dll.tf_I(4294967295), 1431655765)
84 self.assertEqual(self.U(), 4294967295)
85
86 def test_uint_plus(self):
87 self._dll.tf_bI.restype = c_uint
88 self._dll.tf_bI.argtypes = (c_byte, c_uint)
89 self.assertEqual(self._dll.tf_bI(0, 4294967295), 1431655765)
90 self.assertEqual(self.U(), 4294967295)
91
92 def test_long(self):
93 self._dll.tf_l.restype = c_long
94 self._dll.tf_l.argtypes = (c_long,)
95 self.assertEqual(self._dll.tf_l(-2147483646), -715827882)
96 self.assertEqual(self.S(), -2147483646)
97
98 def test_long_plus(self):
99 self._dll.tf_bl.restype = c_long
100 self._dll.tf_bl.argtypes = (c_byte, c_long)
101 self.assertEqual(self._dll.tf_bl(0, -2147483646), -715827882)
102 self.assertEqual(self.S(), -2147483646)
103
104 def test_ulong(self):
105 self._dll.tf_L.restype = c_ulong
106 self._dll.tf_L.argtypes = (c_ulong,)
107 self.assertEqual(self._dll.tf_L(4294967295), 1431655765)
108 self.assertEqual(self.U(), 4294967295)
109
110 def test_ulong_plus(self):
111 self._dll.tf_bL.restype = c_ulong
112 self._dll.tf_bL.argtypes = (c_char, c_ulong)
113 self.assertEqual(self._dll.tf_bL(b' ', 4294967295), 1431655765)
114 self.assertEqual(self.U(), 4294967295)
115
116 def test_longlong(self):
117 self._dll.tf_q.restype = c_longlong
118 self._dll.tf_q.argtypes = (c_longlong, )
119 self.assertEqual(self._dll.tf_q(-9223372036854775806), -3074457345618258602)
120 self.assertEqual(self.S(), -9223372036854775806)
121
122 def test_longlong_plus(self):
123 self._dll.tf_bq.restype = c_longlong
124 self._dll.tf_bq.argtypes = (c_byte, c_longlong)
125 self.assertEqual(self._dll.tf_bq(0, -9223372036854775806), -3074457345618258602)
126 self.assertEqual(self.S(), -9223372036854775806)
127
128 def test_ulonglong(self):
129 self._dll.tf_Q.restype = c_ulonglong
130 self._dll.tf_Q.argtypes = (c_ulonglong, )
131 self.assertEqual(self._dll.tf_Q(18446744073709551615), 6148914691236517205)
132 self.assertEqual(self.U(), 18446744073709551615)
133
134 def test_ulonglong_plus(self):
135 self._dll.tf_bQ.restype = c_ulonglong
136 self._dll.tf_bQ.argtypes = (c_byte, c_ulonglong)
137 self.assertEqual(self._dll.tf_bQ(0, 18446744073709551615), 6148914691236517205)
138 self.assertEqual(self.U(), 18446744073709551615)
139
140 def test_float(self):
141 self._dll.tf_f.restype = c_float
142 self._dll.tf_f.argtypes = (c_float,)
143 self.assertEqual(self._dll.tf_f(-42.), -14.)
144 self.assertEqual(self.S(), -42)
145
146 def test_float_plus(self):
147 self._dll.tf_bf.restype = c_float
148 self._dll.tf_bf.argtypes = (c_byte, c_float)
149 self.assertEqual(self._dll.tf_bf(0, -42.), -14.)
150 self.assertEqual(self.S(), -42)
151
152 def test_double(self):
153 self._dll.tf_d.restype = c_double
154 self._dll.tf_d.argtypes = (c_double,)
155 self.assertEqual(self._dll.tf_d(42.), 14.)
156 self.assertEqual(self.S(), 42)
157
158 def test_double_plus(self):
159 self._dll.tf_bd.restype = c_double
160 self._dll.tf_bd.argtypes = (c_byte, c_double)
161 self.assertEqual(self._dll.tf_bd(0, 42.), 14.)
162 self.assertEqual(self.S(), 42)
163
164 def test_longdouble(self):
165 self._dll.tf_D.restype = c_longdouble
166 self._dll.tf_D.argtypes = (c_longdouble,)
167 self.assertEqual(self._dll.tf_D(42.), 14.)
168 self.assertEqual(self.S(), 42)
169
170 def test_longdouble_plus(self):
171 self._dll.tf_bD.restype = c_longdouble
172 self._dll.tf_bD.argtypes = (c_byte, c_longdouble)
173 self.assertEqual(self._dll.tf_bD(0, 42.), 14.)
174 self.assertEqual(self.S(), 42)
175
176 def test_callwithresult(self):
177 def process_result(result):
178 return result * 2
179 self._dll.tf_i.restype = process_result
180 self._dll.tf_i.argtypes = (c_int,)
181 self.assertEqual(self._dll.tf_i(42), 28)
182 self.assertEqual(self.S(), 42)
183 self.assertEqual(self._dll.tf_i(-42), -28)
184 self.assertEqual(self.S(), -42)
185
186 def test_void(self):
187 self._dll.tv_i.restype = None
188 self._dll.tv_i.argtypes = (c_int,)
189 self.assertEqual(self._dll.tv_i(42), None)
190 self.assertEqual(self.S(), 42)
191 self.assertEqual(self._dll.tv_i(-42), None)
192 self.assertEqual(self.S(), -42)
193
194
195 # The following repeats the above tests with stdcall functions (where
196 # they are available)
197 if hasattr(ctypes, 'WinDLL'):
198 class stdcall_dll(ctypes.WinDLL):
199 def __getattr__(self, name):
200 if name[:2] == '__' and name[-2:] == '__':
201 raise AttributeError(name)
202 func = self._FuncPtr(("s_" + name, self))
203 setattr(self, name, func)
204 return func
205
206 class stdcallCFunctions(CFunctions):
207 _dll = stdcall_dll(_ctypes_test.__file__)
208
209
210 if __name__ == '__main__':
211 unittest.main()