~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Lib/ctypes/test/test_unicode.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

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