PageRenderTime 16ms CodeModel.GetById 7ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/ctypes/test/test_buffers.py

http://unladen-swallow.googlecode.com/
Python | 70 lines | 62 code | 8 blank | 0 comment | 1 complexity | 0a0d11fd23473a2090ae375e04cbe2ce MD5 | raw file
 1from ctypes import *
 2import unittest
 3
 4class StringBufferTestCase(unittest.TestCase):
 5
 6    def test_buffer(self):
 7        b = create_string_buffer(32)
 8        self.failUnlessEqual(len(b), 32)
 9        self.failUnlessEqual(sizeof(b), 32 * sizeof(c_char))
10        self.failUnless(type(b[0]) is str)
11
12        b = create_string_buffer("abc")
13        self.failUnlessEqual(len(b), 4) # trailing nul char
14        self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char))
15        self.failUnless(type(b[0]) is str)
16        self.failUnlessEqual(b[0], "a")
17        self.failUnlessEqual(b[:], "abc\0")
18        self.failUnlessEqual(b[::], "abc\0")
19        self.failUnlessEqual(b[::-1], "\0cba")
20        self.failUnlessEqual(b[::2], "ac")
21        self.failUnlessEqual(b[::5], "a")
22
23    def test_string_conversion(self):
24        b = create_string_buffer(u"abc")
25        self.failUnlessEqual(len(b), 4) # trailing nul char
26        self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char))
27        self.failUnless(type(b[0]) is str)
28        self.failUnlessEqual(b[0], "a")
29        self.failUnlessEqual(b[:], "abc\0")
30        self.failUnlessEqual(b[::], "abc\0")
31        self.failUnlessEqual(b[::-1], "\0cba")
32        self.failUnlessEqual(b[::2], "ac")
33        self.failUnlessEqual(b[::5], "a")
34
35    try:
36        c_wchar
37    except NameError:
38        pass
39    else:
40        def test_unicode_buffer(self):
41            b = create_unicode_buffer(32)
42            self.failUnlessEqual(len(b), 32)
43            self.failUnlessEqual(sizeof(b), 32 * sizeof(c_wchar))
44            self.failUnless(type(b[0]) is unicode)
45
46            b = create_unicode_buffer(u"abc")
47            self.failUnlessEqual(len(b), 4) # trailing nul char
48            self.failUnlessEqual(sizeof(b), 4 * sizeof(c_wchar))
49            self.failUnless(type(b[0]) is unicode)
50            self.failUnlessEqual(b[0], u"a")
51            self.failUnlessEqual(b[:], "abc\0")
52            self.failUnlessEqual(b[::], "abc\0")
53            self.failUnlessEqual(b[::-1], "\0cba")
54            self.failUnlessEqual(b[::2], "ac")
55            self.failUnlessEqual(b[::5], "a")
56
57        def test_unicode_conversion(self):
58            b = create_unicode_buffer("abc")
59            self.failUnlessEqual(len(b), 4) # trailing nul char
60            self.failUnlessEqual(sizeof(b), 4 * sizeof(c_wchar))
61            self.failUnless(type(b[0]) is unicode)
62            self.failUnlessEqual(b[0], u"a")
63            self.failUnlessEqual(b[:], "abc\0")
64            self.failUnlessEqual(b[::], "abc\0")
65            self.failUnlessEqual(b[::-1], "\0cba")
66            self.failUnlessEqual(b[::2], "ac")
67            self.failUnlessEqual(b[::5], "a")
68
69if __name__ == "__main__":
70    unittest.main()