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

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

#countcontent
1n/afrom ctypes import *
2n/afrom ctypes.test import need_symbol
3n/aimport unittest
4n/aimport sys
5n/a
6n/aclass Test(unittest.TestCase):
7n/a
8n/a def test_array2pointer(self):
9n/a array = (c_int * 3)(42, 17, 2)
10n/a
11n/a # casting an array to a pointer works.
12n/a ptr = cast(array, POINTER(c_int))
13n/a self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
14n/a
15n/a if 2*sizeof(c_short) == sizeof(c_int):
16n/a ptr = cast(array, POINTER(c_short))
17n/a if sys.byteorder == "little":
18n/a self.assertEqual([ptr[i] for i in range(6)],
19n/a [42, 0, 17, 0, 2, 0])
20n/a else:
21n/a self.assertEqual([ptr[i] for i in range(6)],
22n/a [0, 42, 0, 17, 0, 2])
23n/a
24n/a def test_address2pointer(self):
25n/a array = (c_int * 3)(42, 17, 2)
26n/a
27n/a address = addressof(array)
28n/a ptr = cast(c_void_p(address), POINTER(c_int))
29n/a self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
30n/a
31n/a ptr = cast(address, POINTER(c_int))
32n/a self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
33n/a
34n/a def test_p2a_objects(self):
35n/a array = (c_char_p * 5)()
36n/a self.assertEqual(array._objects, None)
37n/a array[0] = b"foo bar"
38n/a self.assertEqual(array._objects, {'0': b"foo bar"})
39n/a
40n/a p = cast(array, POINTER(c_char_p))
41n/a # array and p share a common _objects attribute
42n/a self.assertIs(p._objects, array._objects)
43n/a self.assertEqual(array._objects, {'0': b"foo bar", id(array): array})
44n/a p[0] = b"spam spam"
45n/a self.assertEqual(p._objects, {'0': b"spam spam", id(array): array})
46n/a self.assertIs(array._objects, p._objects)
47n/a p[1] = b"foo bar"
48n/a self.assertEqual(p._objects, {'1': b'foo bar', '0': b"spam spam", id(array): array})
49n/a self.assertIs(array._objects, p._objects)
50n/a
51n/a def test_other(self):
52n/a p = cast((c_int * 4)(1, 2, 3, 4), POINTER(c_int))
53n/a self.assertEqual(p[:4], [1,2, 3, 4])
54n/a self.assertEqual(p[:4:], [1, 2, 3, 4])
55n/a self.assertEqual(p[3:-1:-1], [4, 3, 2, 1])
56n/a self.assertEqual(p[:4:3], [1, 4])
57n/a c_int()
58n/a self.assertEqual(p[:4], [1, 2, 3, 4])
59n/a self.assertEqual(p[:4:], [1, 2, 3, 4])
60n/a self.assertEqual(p[3:-1:-1], [4, 3, 2, 1])
61n/a self.assertEqual(p[:4:3], [1, 4])
62n/a p[2] = 96
63n/a self.assertEqual(p[:4], [1, 2, 96, 4])
64n/a self.assertEqual(p[:4:], [1, 2, 96, 4])
65n/a self.assertEqual(p[3:-1:-1], [4, 96, 2, 1])
66n/a self.assertEqual(p[:4:3], [1, 4])
67n/a c_int()
68n/a self.assertEqual(p[:4], [1, 2, 96, 4])
69n/a self.assertEqual(p[:4:], [1, 2, 96, 4])
70n/a self.assertEqual(p[3:-1:-1], [4, 96, 2, 1])
71n/a self.assertEqual(p[:4:3], [1, 4])
72n/a
73n/a def test_char_p(self):
74n/a # This didn't work: bad argument to internal function
75n/a s = c_char_p(b"hiho")
76n/a self.assertEqual(cast(cast(s, c_void_p), c_char_p).value,
77n/a b"hiho")
78n/a
79n/a @need_symbol('c_wchar_p')
80n/a def test_wchar_p(self):
81n/a s = c_wchar_p("hiho")
82n/a self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
83n/a "hiho")
84n/a
85n/aif __name__ == "__main__":
86n/a unittest.main()