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