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

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

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