PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/ctypes/test/test_varsize_struct.py

http://unladen-swallow.googlecode.com/
Python | 50 lines | 37 code | 8 blank | 5 comment | 1 complexity | bb322516d3bcde9a4ef228684bc8e256 MD5 | raw file
 1from ctypes import *
 2import unittest
 3
 4class VarSizeTest(unittest.TestCase):
 5    def test_resize(self):
 6        class X(Structure):
 7            _fields_ = [("item", c_int),
 8                        ("array", c_int * 1)]
 9
10        self.failUnlessEqual(sizeof(X), sizeof(c_int) * 2)
11        x = X()
12        x.item = 42
13        x.array[0] = 100
14        self.failUnlessEqual(sizeof(x), sizeof(c_int) * 2)
15
16        # make room for one additional item
17        new_size = sizeof(X) + sizeof(c_int) * 1
18        resize(x, new_size)
19        self.failUnlessEqual(sizeof(x), new_size)
20        self.failUnlessEqual((x.item, x.array[0]), (42, 100))
21
22        # make room for 10 additional items
23        new_size = sizeof(X) + sizeof(c_int) * 9
24        resize(x, new_size)
25        self.failUnlessEqual(sizeof(x), new_size)
26        self.failUnlessEqual((x.item, x.array[0]), (42, 100))
27
28        # make room for one additional item
29        new_size = sizeof(X) + sizeof(c_int) * 1
30        resize(x, new_size)
31        self.failUnlessEqual(sizeof(x), new_size)
32        self.failUnlessEqual((x.item, x.array[0]), (42, 100))
33
34    def test_array_invalid_length(self):
35        # cannot create arrays with non-positive size
36        self.failUnlessRaises(ValueError, lambda: c_int * -1)
37        self.failUnlessRaises(ValueError, lambda: c_int * -3)
38
39    def test_zerosized_array(self):
40        array = (c_int * 0)()
41        # accessing elements of zero-sized arrays raise IndexError
42        self.failUnlessRaises(IndexError, array.__setitem__, 0, None)
43        self.failUnlessRaises(IndexError, array.__getitem__, 0)
44        self.failUnlessRaises(IndexError, array.__setitem__, 1, None)
45        self.failUnlessRaises(IndexError, array.__getitem__, 1)
46        self.failUnlessRaises(IndexError, array.__setitem__, -1, None)
47        self.failUnlessRaises(IndexError, array.__getitem__, -1)
48
49if __name__ == "__main__":
50    unittest.main()