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