PageRenderTime 50ms CodeModel.GetById 14ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/ctypes/test/test_cast.py

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