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

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

#countcontent
1n/aimport unittest
2n/afrom ctypes import *
3n/a
4n/afrom ctypes.test import need_symbol
5n/a
6n/aformats = "bBhHiIlLqQfd"
7n/a
8n/aformats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \
9n/a c_long, c_ulonglong, c_float, c_double, c_longdouble
10n/a
11n/aclass ArrayTestCase(unittest.TestCase):
12n/a def test_simple(self):
13n/a # create classes holding simple numeric types, and check
14n/a # various properties.
15n/a
16n/a init = list(range(15, 25))
17n/a
18n/a for fmt in formats:
19n/a alen = len(init)
20n/a int_array = ARRAY(fmt, alen)
21n/a
22n/a ia = int_array(*init)
23n/a # length of instance ok?
24n/a self.assertEqual(len(ia), alen)
25n/a
26n/a # slot values ok?
27n/a values = [ia[i] for i in range(alen)]
28n/a self.assertEqual(values, init)
29n/a
30n/a # out-of-bounds accesses should be caught
31n/a with self.assertRaises(IndexError): ia[alen]
32n/a with self.assertRaises(IndexError): ia[-alen-1]
33n/a
34n/a # change the items
35n/a from operator import setitem
36n/a new_values = list(range(42, 42+alen))
37n/a [setitem(ia, n, new_values[n]) for n in range(alen)]
38n/a values = [ia[i] for i in range(alen)]
39n/a self.assertEqual(values, new_values)
40n/a
41n/a # are the items initialized to 0?
42n/a ia = int_array()
43n/a values = [ia[i] for i in range(alen)]
44n/a self.assertEqual(values, [0] * alen)
45n/a
46n/a # Too many initializers should be caught
47n/a self.assertRaises(IndexError, int_array, *range(alen*2))
48n/a
49n/a CharArray = ARRAY(c_char, 3)
50n/a
51n/a ca = CharArray(b"a", b"b", b"c")
52n/a
53n/a # Should this work? It doesn't:
54n/a # CharArray("abc")
55n/a self.assertRaises(TypeError, CharArray, "abc")
56n/a
57n/a self.assertEqual(ca[0], b"a")
58n/a self.assertEqual(ca[1], b"b")
59n/a self.assertEqual(ca[2], b"c")
60n/a self.assertEqual(ca[-3], b"a")
61n/a self.assertEqual(ca[-2], b"b")
62n/a self.assertEqual(ca[-1], b"c")
63n/a
64n/a self.assertEqual(len(ca), 3)
65n/a
66n/a # cannot delete items
67n/a from operator import delitem
68n/a self.assertRaises(TypeError, delitem, ca, 0)
69n/a
70n/a def test_numeric_arrays(self):
71n/a
72n/a alen = 5
73n/a
74n/a numarray = ARRAY(c_int, alen)
75n/a
76n/a na = numarray()
77n/a values = [na[i] for i in range(alen)]
78n/a self.assertEqual(values, [0] * alen)
79n/a
80n/a na = numarray(*[c_int()] * alen)
81n/a values = [na[i] for i in range(alen)]
82n/a self.assertEqual(values, [0]*alen)
83n/a
84n/a na = numarray(1, 2, 3, 4, 5)
85n/a values = [i for i in na]
86n/a self.assertEqual(values, [1, 2, 3, 4, 5])
87n/a
88n/a na = numarray(*map(c_int, (1, 2, 3, 4, 5)))
89n/a values = [i for i in na]
90n/a self.assertEqual(values, [1, 2, 3, 4, 5])
91n/a
92n/a def test_classcache(self):
93n/a self.assertIsNot(ARRAY(c_int, 3), ARRAY(c_int, 4))
94n/a self.assertIs(ARRAY(c_int, 3), ARRAY(c_int, 3))
95n/a
96n/a def test_from_address(self):
97n/a # Failed with 0.9.8, reported by JUrner
98n/a p = create_string_buffer(b"foo")
99n/a sz = (c_char * 3).from_address(addressof(p))
100n/a self.assertEqual(sz[:], b"foo")
101n/a self.assertEqual(sz[::], b"foo")
102n/a self.assertEqual(sz[::-1], b"oof")
103n/a self.assertEqual(sz[::3], b"f")
104n/a self.assertEqual(sz[1:4:2], b"o")
105n/a self.assertEqual(sz.value, b"foo")
106n/a
107n/a @need_symbol('create_unicode_buffer')
108n/a def test_from_addressW(self):
109n/a p = create_unicode_buffer("foo")
110n/a sz = (c_wchar * 3).from_address(addressof(p))
111n/a self.assertEqual(sz[:], "foo")
112n/a self.assertEqual(sz[::], "foo")
113n/a self.assertEqual(sz[::-1], "oof")
114n/a self.assertEqual(sz[::3], "f")
115n/a self.assertEqual(sz[1:4:2], "o")
116n/a self.assertEqual(sz.value, "foo")
117n/a
118n/a def test_cache(self):
119n/a # Array types are cached internally in the _ctypes extension,
120n/a # in a WeakValueDictionary. Make sure the array type is
121n/a # removed from the cache when the itemtype goes away. This
122n/a # test will not fail, but will show a leak in the testsuite.
123n/a
124n/a # Create a new type:
125n/a class my_int(c_int):
126n/a pass
127n/a # Create a new array type based on it:
128n/a t1 = my_int * 1
129n/a t2 = my_int * 1
130n/a self.assertIs(t1, t2)
131n/a
132n/a def test_subclass(self):
133n/a class T(Array):
134n/a _type_ = c_int
135n/a _length_ = 13
136n/a class U(T):
137n/a pass
138n/a class V(U):
139n/a pass
140n/a class W(V):
141n/a pass
142n/a class X(T):
143n/a _type_ = c_short
144n/a class Y(T):
145n/a _length_ = 187
146n/a
147n/a for c in [T, U, V, W]:
148n/a self.assertEqual(c._type_, c_int)
149n/a self.assertEqual(c._length_, 13)
150n/a self.assertEqual(c()._type_, c_int)
151n/a self.assertEqual(c()._length_, 13)
152n/a
153n/a self.assertEqual(X._type_, c_short)
154n/a self.assertEqual(X._length_, 13)
155n/a self.assertEqual(X()._type_, c_short)
156n/a self.assertEqual(X()._length_, 13)
157n/a
158n/a self.assertEqual(Y._type_, c_int)
159n/a self.assertEqual(Y._length_, 187)
160n/a self.assertEqual(Y()._type_, c_int)
161n/a self.assertEqual(Y()._length_, 187)
162n/a
163n/a def test_bad_subclass(self):
164n/a import sys
165n/a
166n/a with self.assertRaises(AttributeError):
167n/a class T(Array):
168n/a pass
169n/a with self.assertRaises(AttributeError):
170n/a class T(Array):
171n/a _type_ = c_int
172n/a with self.assertRaises(AttributeError):
173n/a class T(Array):
174n/a _length_ = 13
175n/a with self.assertRaises(OverflowError):
176n/a class T(Array):
177n/a _type_ = c_int
178n/a _length_ = sys.maxsize * 2
179n/a with self.assertRaises(AttributeError):
180n/a class T(Array):
181n/a _type_ = c_int
182n/a _length_ = 1.87
183n/a
184n/aif __name__ == '__main__':
185n/a unittest.main()