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

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

#countcontent
1n/aimport unittest
2n/aimport ctypes
3n/afrom ctypes.test import need_symbol
4n/a
5n/aimport _ctypes_test
6n/a
7n/a@need_symbol('c_wchar')
8n/aclass UnicodeTestCase(unittest.TestCase):
9n/a def test_wcslen(self):
10n/a dll = ctypes.CDLL(_ctypes_test.__file__)
11n/a wcslen = dll.my_wcslen
12n/a wcslen.argtypes = [ctypes.c_wchar_p]
13n/a
14n/a self.assertEqual(wcslen("abc"), 3)
15n/a self.assertEqual(wcslen("ab\u2070"), 3)
16n/a self.assertRaises(ctypes.ArgumentError, wcslen, b"ab\xe4")
17n/a
18n/a def test_buffers(self):
19n/a buf = ctypes.create_unicode_buffer("abc")
20n/a self.assertEqual(len(buf), 3+1)
21n/a
22n/a buf = ctypes.create_unicode_buffer("ab\xe4\xf6\xfc")
23n/a self.assertEqual(buf[:], "ab\xe4\xf6\xfc\0")
24n/a self.assertEqual(buf[::], "ab\xe4\xf6\xfc\0")
25n/a self.assertEqual(buf[::-1], '\x00\xfc\xf6\xe4ba')
26n/a self.assertEqual(buf[::2], 'a\xe4\xfc')
27n/a self.assertEqual(buf[6:5:-1], "")
28n/a
29n/afunc = ctypes.CDLL(_ctypes_test.__file__)._testfunc_p_p
30n/a
31n/aclass StringTestCase(UnicodeTestCase):
32n/a def setUp(self):
33n/a func.argtypes = [ctypes.c_char_p]
34n/a func.restype = ctypes.c_char_p
35n/a
36n/a def tearDown(self):
37n/a func.argtypes = None
38n/a func.restype = ctypes.c_int
39n/a
40n/a def test_func(self):
41n/a self.assertEqual(func(b"abc\xe4"), b"abc\xe4")
42n/a
43n/a def test_buffers(self):
44n/a buf = ctypes.create_string_buffer(b"abc")
45n/a self.assertEqual(len(buf), 3+1)
46n/a
47n/a buf = ctypes.create_string_buffer(b"ab\xe4\xf6\xfc")
48n/a self.assertEqual(buf[:], b"ab\xe4\xf6\xfc\0")
49n/a self.assertEqual(buf[::], b"ab\xe4\xf6\xfc\0")
50n/a self.assertEqual(buf[::-1], b'\x00\xfc\xf6\xe4ba')
51n/a self.assertEqual(buf[::2], b'a\xe4\xfc')
52n/a self.assertEqual(buf[6:5:-1], b"")
53n/a
54n/a
55n/aif __name__ == '__main__':
56n/a unittest.main()