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

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

#countcontent
1n/afrom ctypes import *
2n/aimport unittest
3n/a
4n/aclass VarSizeTest(unittest.TestCase):
5n/a def test_resize(self):
6n/a class X(Structure):
7n/a _fields_ = [("item", c_int),
8n/a ("array", c_int * 1)]
9n/a
10n/a self.assertEqual(sizeof(X), sizeof(c_int) * 2)
11n/a x = X()
12n/a x.item = 42
13n/a x.array[0] = 100
14n/a self.assertEqual(sizeof(x), sizeof(c_int) * 2)
15n/a
16n/a # make room for one additional item
17n/a new_size = sizeof(X) + sizeof(c_int) * 1
18n/a resize(x, new_size)
19n/a self.assertEqual(sizeof(x), new_size)
20n/a self.assertEqual((x.item, x.array[0]), (42, 100))
21n/a
22n/a # make room for 10 additional items
23n/a new_size = sizeof(X) + sizeof(c_int) * 9
24n/a resize(x, new_size)
25n/a self.assertEqual(sizeof(x), new_size)
26n/a self.assertEqual((x.item, x.array[0]), (42, 100))
27n/a
28n/a # make room for one additional item
29n/a new_size = sizeof(X) + sizeof(c_int) * 1
30n/a resize(x, new_size)
31n/a self.assertEqual(sizeof(x), new_size)
32n/a self.assertEqual((x.item, x.array[0]), (42, 100))
33n/a
34n/a def test_array_invalid_length(self):
35n/a # cannot create arrays with non-positive size
36n/a self.assertRaises(ValueError, lambda: c_int * -1)
37n/a self.assertRaises(ValueError, lambda: c_int * -3)
38n/a
39n/a def test_zerosized_array(self):
40n/a array = (c_int * 0)()
41n/a # accessing elements of zero-sized arrays raise IndexError
42n/a self.assertRaises(IndexError, array.__setitem__, 0, None)
43n/a self.assertRaises(IndexError, array.__getitem__, 0)
44n/a self.assertRaises(IndexError, array.__setitem__, 1, None)
45n/a self.assertRaises(IndexError, array.__getitem__, 1)
46n/a self.assertRaises(IndexError, array.__setitem__, -1, None)
47n/a self.assertRaises(IndexError, array.__getitem__, -1)
48n/a
49n/aif __name__ == "__main__":
50n/a unittest.main()