1 | n/a | import unittest |
---|
2 | n/a | from ctypes import * |
---|
3 | n/a | from ctypes.test import need_symbol |
---|
4 | n/a | |
---|
5 | n/a | import _ctypes_test |
---|
6 | n/a | |
---|
7 | n/a | class SlicesTestCase(unittest.TestCase): |
---|
8 | n/a | def test_getslice_cint(self): |
---|
9 | n/a | a = (c_int * 100)(*range(1100, 1200)) |
---|
10 | n/a | b = list(range(1100, 1200)) |
---|
11 | n/a | self.assertEqual(a[0:2], b[0:2]) |
---|
12 | n/a | self.assertEqual(a[0:2:], b[0:2:]) |
---|
13 | n/a | self.assertEqual(len(a), len(b)) |
---|
14 | n/a | self.assertEqual(a[5:7], b[5:7]) |
---|
15 | n/a | self.assertEqual(a[5:7:], b[5:7:]) |
---|
16 | n/a | self.assertEqual(a[-1], b[-1]) |
---|
17 | n/a | self.assertEqual(a[:], b[:]) |
---|
18 | n/a | self.assertEqual(a[::], b[::]) |
---|
19 | n/a | self.assertEqual(a[10::-1], b[10::-1]) |
---|
20 | n/a | self.assertEqual(a[30:20:-1], b[30:20:-1]) |
---|
21 | n/a | self.assertEqual(a[:12:6], b[:12:6]) |
---|
22 | n/a | self.assertEqual(a[2:6:4], b[2:6:4]) |
---|
23 | n/a | |
---|
24 | n/a | a[0:5] = range(5, 10) |
---|
25 | n/a | self.assertEqual(a[0:5], list(range(5, 10))) |
---|
26 | n/a | self.assertEqual(a[0:5:], list(range(5, 10))) |
---|
27 | n/a | self.assertEqual(a[4::-1], list(range(9, 4, -1))) |
---|
28 | n/a | |
---|
29 | n/a | def test_setslice_cint(self): |
---|
30 | n/a | a = (c_int * 100)(*range(1100, 1200)) |
---|
31 | n/a | b = list(range(1100, 1200)) |
---|
32 | n/a | |
---|
33 | n/a | a[32:47] = list(range(32, 47)) |
---|
34 | n/a | self.assertEqual(a[32:47], list(range(32, 47))) |
---|
35 | n/a | a[32:47] = range(132, 147) |
---|
36 | n/a | self.assertEqual(a[32:47:], list(range(132, 147))) |
---|
37 | n/a | a[46:31:-1] = range(232, 247) |
---|
38 | n/a | self.assertEqual(a[32:47:1], list(range(246, 231, -1))) |
---|
39 | n/a | |
---|
40 | n/a | a[32:47] = range(1132, 1147) |
---|
41 | n/a | self.assertEqual(a[:], b) |
---|
42 | n/a | a[32:47:7] = range(3) |
---|
43 | n/a | b[32:47:7] = range(3) |
---|
44 | n/a | self.assertEqual(a[:], b) |
---|
45 | n/a | a[33::-3] = range(12) |
---|
46 | n/a | b[33::-3] = range(12) |
---|
47 | n/a | self.assertEqual(a[:], b) |
---|
48 | n/a | |
---|
49 | n/a | from operator import setitem |
---|
50 | n/a | |
---|
51 | n/a | # TypeError: int expected instead of str instance |
---|
52 | n/a | self.assertRaises(TypeError, setitem, a, slice(0, 5), "abcde") |
---|
53 | n/a | # TypeError: int expected instead of str instance |
---|
54 | n/a | self.assertRaises(TypeError, setitem, a, slice(0, 5), |
---|
55 | n/a | ["a", "b", "c", "d", "e"]) |
---|
56 | n/a | # TypeError: int expected instead of float instance |
---|
57 | n/a | self.assertRaises(TypeError, setitem, a, slice(0, 5), |
---|
58 | n/a | [1, 2, 3, 4, 3.14]) |
---|
59 | n/a | # ValueError: Can only assign sequence of same size |
---|
60 | n/a | self.assertRaises(ValueError, setitem, a, slice(0, 5), range(32)) |
---|
61 | n/a | |
---|
62 | n/a | def test_char_ptr(self): |
---|
63 | n/a | s = b"abcdefghijklmnopqrstuvwxyz" |
---|
64 | n/a | |
---|
65 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
66 | n/a | dll.my_strdup.restype = POINTER(c_char) |
---|
67 | n/a | dll.my_free.restype = None |
---|
68 | n/a | res = dll.my_strdup(s) |
---|
69 | n/a | self.assertEqual(res[:len(s)], s) |
---|
70 | n/a | self.assertEqual(res[:3], s[:3]) |
---|
71 | n/a | self.assertEqual(res[:len(s):], s) |
---|
72 | n/a | self.assertEqual(res[len(s)-1:-1:-1], s[::-1]) |
---|
73 | n/a | self.assertEqual(res[len(s)-1:5:-7], s[:5:-7]) |
---|
74 | n/a | self.assertEqual(res[0:-1:-1], s[0::-1]) |
---|
75 | n/a | |
---|
76 | n/a | import operator |
---|
77 | n/a | self.assertRaises(ValueError, operator.getitem, |
---|
78 | n/a | res, slice(None, None, None)) |
---|
79 | n/a | self.assertRaises(ValueError, operator.getitem, |
---|
80 | n/a | res, slice(0, None, None)) |
---|
81 | n/a | self.assertRaises(ValueError, operator.getitem, |
---|
82 | n/a | res, slice(None, 5, -1)) |
---|
83 | n/a | self.assertRaises(ValueError, operator.getitem, |
---|
84 | n/a | res, slice(-5, None, None)) |
---|
85 | n/a | |
---|
86 | n/a | self.assertRaises(TypeError, operator.setitem, |
---|
87 | n/a | res, slice(0, 5), "abcde") |
---|
88 | n/a | dll.my_free(res) |
---|
89 | n/a | |
---|
90 | n/a | dll.my_strdup.restype = POINTER(c_byte) |
---|
91 | n/a | res = dll.my_strdup(s) |
---|
92 | n/a | self.assertEqual(res[:len(s)], list(range(ord("a"), ord("z")+1))) |
---|
93 | n/a | self.assertEqual(res[:len(s):], list(range(ord("a"), ord("z")+1))) |
---|
94 | n/a | dll.my_free(res) |
---|
95 | n/a | |
---|
96 | n/a | def test_char_ptr_with_free(self): |
---|
97 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
98 | n/a | s = b"abcdefghijklmnopqrstuvwxyz" |
---|
99 | n/a | |
---|
100 | n/a | class allocated_c_char_p(c_char_p): |
---|
101 | n/a | pass |
---|
102 | n/a | |
---|
103 | n/a | dll.my_free.restype = None |
---|
104 | n/a | def errcheck(result, func, args): |
---|
105 | n/a | retval = result.value |
---|
106 | n/a | dll.my_free(result) |
---|
107 | n/a | return retval |
---|
108 | n/a | |
---|
109 | n/a | dll.my_strdup.restype = allocated_c_char_p |
---|
110 | n/a | dll.my_strdup.errcheck = errcheck |
---|
111 | n/a | try: |
---|
112 | n/a | res = dll.my_strdup(s) |
---|
113 | n/a | self.assertEqual(res, s) |
---|
114 | n/a | finally: |
---|
115 | n/a | del dll.my_strdup.errcheck |
---|
116 | n/a | |
---|
117 | n/a | |
---|
118 | n/a | def test_char_array(self): |
---|
119 | n/a | s = b"abcdefghijklmnopqrstuvwxyz\0" |
---|
120 | n/a | |
---|
121 | n/a | p = (c_char * 27)(*s) |
---|
122 | n/a | self.assertEqual(p[:], s) |
---|
123 | n/a | self.assertEqual(p[::], s) |
---|
124 | n/a | self.assertEqual(p[::-1], s[::-1]) |
---|
125 | n/a | self.assertEqual(p[5::-2], s[5::-2]) |
---|
126 | n/a | self.assertEqual(p[2:5:-3], s[2:5:-3]) |
---|
127 | n/a | |
---|
128 | n/a | |
---|
129 | n/a | @need_symbol('c_wchar') |
---|
130 | n/a | def test_wchar_ptr(self): |
---|
131 | n/a | s = "abcdefghijklmnopqrstuvwxyz\0" |
---|
132 | n/a | |
---|
133 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
134 | n/a | dll.my_wcsdup.restype = POINTER(c_wchar) |
---|
135 | n/a | dll.my_wcsdup.argtypes = POINTER(c_wchar), |
---|
136 | n/a | dll.my_free.restype = None |
---|
137 | n/a | res = dll.my_wcsdup(s) |
---|
138 | n/a | self.assertEqual(res[:len(s)], s) |
---|
139 | n/a | self.assertEqual(res[:len(s):], s) |
---|
140 | n/a | self.assertEqual(res[len(s)-1:-1:-1], s[::-1]) |
---|
141 | n/a | self.assertEqual(res[len(s)-1:5:-7], s[:5:-7]) |
---|
142 | n/a | |
---|
143 | n/a | import operator |
---|
144 | n/a | self.assertRaises(TypeError, operator.setitem, |
---|
145 | n/a | res, slice(0, 5), "abcde") |
---|
146 | n/a | dll.my_free(res) |
---|
147 | n/a | |
---|
148 | n/a | if sizeof(c_wchar) == sizeof(c_short): |
---|
149 | n/a | dll.my_wcsdup.restype = POINTER(c_short) |
---|
150 | n/a | elif sizeof(c_wchar) == sizeof(c_int): |
---|
151 | n/a | dll.my_wcsdup.restype = POINTER(c_int) |
---|
152 | n/a | elif sizeof(c_wchar) == sizeof(c_long): |
---|
153 | n/a | dll.my_wcsdup.restype = POINTER(c_long) |
---|
154 | n/a | else: |
---|
155 | n/a | self.skipTest('Pointers to c_wchar are not supported') |
---|
156 | n/a | res = dll.my_wcsdup(s) |
---|
157 | n/a | tmpl = list(range(ord("a"), ord("z")+1)) |
---|
158 | n/a | self.assertEqual(res[:len(s)-1], tmpl) |
---|
159 | n/a | self.assertEqual(res[:len(s)-1:], tmpl) |
---|
160 | n/a | self.assertEqual(res[len(s)-2:-1:-1], tmpl[::-1]) |
---|
161 | n/a | self.assertEqual(res[len(s)-2:5:-7], tmpl[:5:-7]) |
---|
162 | n/a | dll.my_free(res) |
---|
163 | n/a | |
---|
164 | n/a | ################################################################ |
---|
165 | n/a | |
---|
166 | n/a | if __name__ == "__main__": |
---|
167 | n/a | unittest.main() |
---|