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

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

#countcontent
1n/aimport unittest
2n/afrom ctypes import *
3n/afrom ctypes.test import need_symbol
4n/a
5n/aimport _ctypes_test
6n/a
7n/aclass SlicesTestCase(unittest.TestCase):
8n/a def test_getslice_cint(self):
9n/a a = (c_int * 100)(*range(1100, 1200))
10n/a b = list(range(1100, 1200))
11n/a self.assertEqual(a[0:2], b[0:2])
12n/a self.assertEqual(a[0:2:], b[0:2:])
13n/a self.assertEqual(len(a), len(b))
14n/a self.assertEqual(a[5:7], b[5:7])
15n/a self.assertEqual(a[5:7:], b[5:7:])
16n/a self.assertEqual(a[-1], b[-1])
17n/a self.assertEqual(a[:], b[:])
18n/a self.assertEqual(a[::], b[::])
19n/a self.assertEqual(a[10::-1], b[10::-1])
20n/a self.assertEqual(a[30:20:-1], b[30:20:-1])
21n/a self.assertEqual(a[:12:6], b[:12:6])
22n/a self.assertEqual(a[2:6:4], b[2:6:4])
23n/a
24n/a a[0:5] = range(5, 10)
25n/a self.assertEqual(a[0:5], list(range(5, 10)))
26n/a self.assertEqual(a[0:5:], list(range(5, 10)))
27n/a self.assertEqual(a[4::-1], list(range(9, 4, -1)))
28n/a
29n/a def test_setslice_cint(self):
30n/a a = (c_int * 100)(*range(1100, 1200))
31n/a b = list(range(1100, 1200))
32n/a
33n/a a[32:47] = list(range(32, 47))
34n/a self.assertEqual(a[32:47], list(range(32, 47)))
35n/a a[32:47] = range(132, 147)
36n/a self.assertEqual(a[32:47:], list(range(132, 147)))
37n/a a[46:31:-1] = range(232, 247)
38n/a self.assertEqual(a[32:47:1], list(range(246, 231, -1)))
39n/a
40n/a a[32:47] = range(1132, 1147)
41n/a self.assertEqual(a[:], b)
42n/a a[32:47:7] = range(3)
43n/a b[32:47:7] = range(3)
44n/a self.assertEqual(a[:], b)
45n/a a[33::-3] = range(12)
46n/a b[33::-3] = range(12)
47n/a self.assertEqual(a[:], b)
48n/a
49n/a from operator import setitem
50n/a
51n/a # TypeError: int expected instead of str instance
52n/a self.assertRaises(TypeError, setitem, a, slice(0, 5), "abcde")
53n/a # TypeError: int expected instead of str instance
54n/a self.assertRaises(TypeError, setitem, a, slice(0, 5),
55n/a ["a", "b", "c", "d", "e"])
56n/a # TypeError: int expected instead of float instance
57n/a self.assertRaises(TypeError, setitem, a, slice(0, 5),
58n/a [1, 2, 3, 4, 3.14])
59n/a # ValueError: Can only assign sequence of same size
60n/a self.assertRaises(ValueError, setitem, a, slice(0, 5), range(32))
61n/a
62n/a def test_char_ptr(self):
63n/a s = b"abcdefghijklmnopqrstuvwxyz"
64n/a
65n/a dll = CDLL(_ctypes_test.__file__)
66n/a dll.my_strdup.restype = POINTER(c_char)
67n/a dll.my_free.restype = None
68n/a res = dll.my_strdup(s)
69n/a self.assertEqual(res[:len(s)], s)
70n/a self.assertEqual(res[:3], s[:3])
71n/a self.assertEqual(res[:len(s):], s)
72n/a self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
73n/a self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
74n/a self.assertEqual(res[0:-1:-1], s[0::-1])
75n/a
76n/a import operator
77n/a self.assertRaises(ValueError, operator.getitem,
78n/a res, slice(None, None, None))
79n/a self.assertRaises(ValueError, operator.getitem,
80n/a res, slice(0, None, None))
81n/a self.assertRaises(ValueError, operator.getitem,
82n/a res, slice(None, 5, -1))
83n/a self.assertRaises(ValueError, operator.getitem,
84n/a res, slice(-5, None, None))
85n/a
86n/a self.assertRaises(TypeError, operator.setitem,
87n/a res, slice(0, 5), "abcde")
88n/a dll.my_free(res)
89n/a
90n/a dll.my_strdup.restype = POINTER(c_byte)
91n/a res = dll.my_strdup(s)
92n/a self.assertEqual(res[:len(s)], list(range(ord("a"), ord("z")+1)))
93n/a self.assertEqual(res[:len(s):], list(range(ord("a"), ord("z")+1)))
94n/a dll.my_free(res)
95n/a
96n/a def test_char_ptr_with_free(self):
97n/a dll = CDLL(_ctypes_test.__file__)
98n/a s = b"abcdefghijklmnopqrstuvwxyz"
99n/a
100n/a class allocated_c_char_p(c_char_p):
101n/a pass
102n/a
103n/a dll.my_free.restype = None
104n/a def errcheck(result, func, args):
105n/a retval = result.value
106n/a dll.my_free(result)
107n/a return retval
108n/a
109n/a dll.my_strdup.restype = allocated_c_char_p
110n/a dll.my_strdup.errcheck = errcheck
111n/a try:
112n/a res = dll.my_strdup(s)
113n/a self.assertEqual(res, s)
114n/a finally:
115n/a del dll.my_strdup.errcheck
116n/a
117n/a
118n/a def test_char_array(self):
119n/a s = b"abcdefghijklmnopqrstuvwxyz\0"
120n/a
121n/a p = (c_char * 27)(*s)
122n/a self.assertEqual(p[:], s)
123n/a self.assertEqual(p[::], s)
124n/a self.assertEqual(p[::-1], s[::-1])
125n/a self.assertEqual(p[5::-2], s[5::-2])
126n/a self.assertEqual(p[2:5:-3], s[2:5:-3])
127n/a
128n/a
129n/a @need_symbol('c_wchar')
130n/a def test_wchar_ptr(self):
131n/a s = "abcdefghijklmnopqrstuvwxyz\0"
132n/a
133n/a dll = CDLL(_ctypes_test.__file__)
134n/a dll.my_wcsdup.restype = POINTER(c_wchar)
135n/a dll.my_wcsdup.argtypes = POINTER(c_wchar),
136n/a dll.my_free.restype = None
137n/a res = dll.my_wcsdup(s)
138n/a self.assertEqual(res[:len(s)], s)
139n/a self.assertEqual(res[:len(s):], s)
140n/a self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
141n/a self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
142n/a
143n/a import operator
144n/a self.assertRaises(TypeError, operator.setitem,
145n/a res, slice(0, 5), "abcde")
146n/a dll.my_free(res)
147n/a
148n/a if sizeof(c_wchar) == sizeof(c_short):
149n/a dll.my_wcsdup.restype = POINTER(c_short)
150n/a elif sizeof(c_wchar) == sizeof(c_int):
151n/a dll.my_wcsdup.restype = POINTER(c_int)
152n/a elif sizeof(c_wchar) == sizeof(c_long):
153n/a dll.my_wcsdup.restype = POINTER(c_long)
154n/a else:
155n/a self.skipTest('Pointers to c_wchar are not supported')
156n/a res = dll.my_wcsdup(s)
157n/a tmpl = list(range(ord("a"), ord("z")+1))
158n/a self.assertEqual(res[:len(s)-1], tmpl)
159n/a self.assertEqual(res[:len(s)-1:], tmpl)
160n/a self.assertEqual(res[len(s)-2:-1:-1], tmpl[::-1])
161n/a self.assertEqual(res[len(s)-2:5:-7], tmpl[:5:-7])
162n/a dll.my_free(res)
163n/a
164n/a################################################################
165n/a
166n/aif __name__ == "__main__":
167n/a unittest.main()