ยปCore Development>Code coverage>Lib/ctypes/test/test_cfuncs.py

Python code coverage for Lib/ctypes/test/test_cfuncs.py

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