1 | n/a | from ctypes import * |
---|
2 | n/a | from ctypes.test import need_symbol |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | class StringBufferTestCase(unittest.TestCase): |
---|
6 | n/a | |
---|
7 | n/a | def test_buffer(self): |
---|
8 | n/a | b = create_string_buffer(32) |
---|
9 | n/a | self.assertEqual(len(b), 32) |
---|
10 | n/a | self.assertEqual(sizeof(b), 32 * sizeof(c_char)) |
---|
11 | n/a | self.assertIs(type(b[0]), bytes) |
---|
12 | n/a | |
---|
13 | n/a | b = create_string_buffer(b"abc") |
---|
14 | n/a | self.assertEqual(len(b), 4) # trailing nul char |
---|
15 | n/a | self.assertEqual(sizeof(b), 4 * sizeof(c_char)) |
---|
16 | n/a | self.assertIs(type(b[0]), bytes) |
---|
17 | n/a | self.assertEqual(b[0], b"a") |
---|
18 | n/a | self.assertEqual(b[:], b"abc\0") |
---|
19 | n/a | self.assertEqual(b[::], b"abc\0") |
---|
20 | n/a | self.assertEqual(b[::-1], b"\0cba") |
---|
21 | n/a | self.assertEqual(b[::2], b"ac") |
---|
22 | n/a | self.assertEqual(b[::5], b"a") |
---|
23 | n/a | |
---|
24 | n/a | self.assertRaises(TypeError, create_string_buffer, "abc") |
---|
25 | n/a | |
---|
26 | n/a | def test_buffer_interface(self): |
---|
27 | n/a | self.assertEqual(len(bytearray(create_string_buffer(0))), 0) |
---|
28 | n/a | self.assertEqual(len(bytearray(create_string_buffer(1))), 1) |
---|
29 | n/a | |
---|
30 | n/a | @need_symbol('c_wchar') |
---|
31 | n/a | def test_unicode_buffer(self): |
---|
32 | n/a | b = create_unicode_buffer(32) |
---|
33 | n/a | self.assertEqual(len(b), 32) |
---|
34 | n/a | self.assertEqual(sizeof(b), 32 * sizeof(c_wchar)) |
---|
35 | n/a | self.assertIs(type(b[0]), str) |
---|
36 | n/a | |
---|
37 | n/a | b = create_unicode_buffer("abc") |
---|
38 | n/a | self.assertEqual(len(b), 4) # trailing nul char |
---|
39 | n/a | self.assertEqual(sizeof(b), 4 * sizeof(c_wchar)) |
---|
40 | n/a | self.assertIs(type(b[0]), str) |
---|
41 | n/a | self.assertEqual(b[0], "a") |
---|
42 | n/a | self.assertEqual(b[:], "abc\0") |
---|
43 | n/a | self.assertEqual(b[::], "abc\0") |
---|
44 | n/a | self.assertEqual(b[::-1], "\0cba") |
---|
45 | n/a | self.assertEqual(b[::2], "ac") |
---|
46 | n/a | self.assertEqual(b[::5], "a") |
---|
47 | n/a | |
---|
48 | n/a | self.assertRaises(TypeError, create_unicode_buffer, b"abc") |
---|
49 | n/a | |
---|
50 | n/a | @need_symbol('c_wchar') |
---|
51 | n/a | def test_unicode_conversion(self): |
---|
52 | n/a | b = create_unicode_buffer("abc") |
---|
53 | n/a | self.assertEqual(len(b), 4) # trailing nul char |
---|
54 | n/a | self.assertEqual(sizeof(b), 4 * sizeof(c_wchar)) |
---|
55 | n/a | self.assertIs(type(b[0]), str) |
---|
56 | n/a | self.assertEqual(b[0], "a") |
---|
57 | n/a | self.assertEqual(b[:], "abc\0") |
---|
58 | n/a | self.assertEqual(b[::], "abc\0") |
---|
59 | n/a | self.assertEqual(b[::-1], "\0cba") |
---|
60 | n/a | self.assertEqual(b[::2], "ac") |
---|
61 | n/a | self.assertEqual(b[::5], "a") |
---|
62 | n/a | |
---|
63 | n/a | if __name__ == "__main__": |
---|
64 | n/a | unittest.main() |
---|