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