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