6 class SlicesTestCase(unittest
.TestCase
):
7 def test_getslice_cint(self
):
8 a
= (c_int
* 100)(*xrange(1100, 1200))
10 self
.assertEqual(a
[0:2], b
[0:2])
11 self
.assertEqual(a
[0:2:], b
[0:2:])
12 self
.assertEqual(len(a
), len(b
))
13 self
.assertEqual(a
[5:7], b
[5:7])
14 self
.assertEqual(a
[5:7:], b
[5:7:])
15 self
.assertEqual(a
[-1], b
[-1])
16 self
.assertEqual(a
[:], b
[:])
17 self
.assertEqual(a
[::], b
[::])
18 self
.assertEqual(a
[10::-1], b
[10::-1])
19 self
.assertEqual(a
[30:20:-1], b
[30:20:-1])
20 self
.assertEqual(a
[:12:6], b
[:12:6])
21 self
.assertEqual(a
[2:6:4], b
[2:6:4])
24 self
.assertEqual(a
[0:5], range(5, 10))
25 self
.assertEqual(a
[0:5:], range(5, 10))
26 self
.assertEqual(a
[4::-1], range(9, 4, -1))
28 def test_setslice_cint(self
):
29 a
= (c_int
* 100)(*xrange(1100, 1200))
32 a
[32:47] = range(32, 47)
33 self
.assertEqual(a
[32:47], range(32, 47))
34 a
[32:47] = range(132, 147)
35 self
.assertEqual(a
[32:47:], range(132, 147))
36 a
[46:31:-1] = range(232, 247)
37 self
.assertEqual(a
[32:47:1], range(246, 231, -1))
39 a
[32:47] = range(1132, 1147)
40 self
.assertEqual(a
[:], b
)
43 self
.assertEqual(a
[:], b
)
46 self
.assertEqual(a
[:], b
)
48 from operator
import setslice
, setitem
50 # TypeError: int expected instead of str instance
51 self
.assertRaises(TypeError, setslice
, a
, 0, 5, "abcde")
52 self
.assertRaises(TypeError, setitem
, a
, slice(0, 5), "abcde")
53 # TypeError: int expected instead of str instance
54 self
.assertRaises(TypeError, setslice
, a
, 0, 5, ["a", "b", "c", "d", "e"])
55 self
.assertRaises(TypeError, setitem
, a
, slice(0, 5),
56 ["a", "b", "c", "d", "e"])
57 # TypeError: int expected instead of float instance
58 self
.assertRaises(TypeError, setslice
, a
, 0, 5, [1, 2, 3, 4, 3.14])
59 self
.assertRaises(TypeError, setitem
, a
, slice(0, 5),
61 # ValueError: Can only assign sequence of same size
62 self
.assertRaises(ValueError, setslice
, a
, 0, 5, range(32))
63 self
.assertRaises(ValueError, setitem
, a
, slice(0, 5), range(32))
65 def test_char_ptr(self
):
66 s
= "abcdefghijklmnopqrstuvwxyz"
68 dll
= CDLL(_ctypes_test
.__file
__)
69 dll
.my_strdup
.restype
= POINTER(c_char
)
70 dll
.my_free
.restype
= None
71 res
= dll
.my_strdup(s
)
72 self
.assertEqual(res
[:len(s
)], s
)
73 self
.assertEqual(res
[:3], s
[:3])
74 self
.assertEqual(res
[:len(s
):], s
)
75 self
.assertEqual(res
[len(s
)-1:-1:-1], s
[::-1])
76 self
.assertEqual(res
[len(s
)-1:5:-7], s
[:5:-7])
77 self
.assertEqual(res
[0:-1:-1], s
[0::-1])
80 self
.assertRaises(ValueError, operator
.getitem
,
81 res
, slice(None, None, None))
82 self
.assertRaises(ValueError, operator
.getitem
,
83 res
, slice(0, None, None))
84 self
.assertRaises(ValueError, operator
.getitem
,
85 res
, slice(None, 5, -1))
86 self
.assertRaises(ValueError, operator
.getitem
,
87 res
, slice(-5, None, None))
89 self
.assertRaises(TypeError, operator
.setslice
,
91 self
.assertRaises(TypeError, operator
.setitem
,
92 res
, slice(0, 5), u
"abcde")
95 dll
.my_strdup
.restype
= POINTER(c_byte
)
96 res
= dll
.my_strdup(s
)
97 self
.assertEqual(res
[:len(s
)], range(ord("a"), ord("z")+1))
98 self
.assertEqual(res
[:len(s
):], range(ord("a"), ord("z")+1))
101 def test_char_ptr_with_free(self
):
102 dll
= CDLL(_ctypes_test
.__file
__)
103 s
= "abcdefghijklmnopqrstuvwxyz"
105 class allocated_c_char_p(c_char_p
):
108 dll
.my_free
.restype
= None
109 def errcheck(result
, func
, args
):
110 retval
= result
.value
114 dll
.my_strdup
.restype
= allocated_c_char_p
115 dll
.my_strdup
.errcheck
= errcheck
117 res
= dll
.my_strdup(s
)
118 self
.assertEqual(res
, s
)
120 del dll
.my_strdup
.errcheck
123 def test_char_array(self
):
124 s
= "abcdefghijklmnopqrstuvwxyz\0"
126 p
= (c_char
* 27)(*s
)
127 self
.assertEqual(p
[:], s
)
128 self
.assertEqual(p
[::], s
)
129 self
.assertEqual(p
[::-1], s
[::-1])
130 self
.assertEqual(p
[5::-2], s
[5::-2])
131 self
.assertEqual(p
[2:5:-3], s
[2:5:-3])
139 def test_wchar_ptr(self
):
140 s
= u
"abcdefghijklmnopqrstuvwxyz\0"
142 dll
= CDLL(_ctypes_test
.__file
__)
143 dll
.my_wcsdup
.restype
= POINTER(c_wchar
)
144 dll
.my_wcsdup
.argtypes
= POINTER(c_wchar
),
145 dll
.my_free
.restype
= None
146 res
= dll
.my_wcsdup(s
)
147 self
.assertEqual(res
[:len(s
)], s
)
148 self
.assertEqual(res
[:len(s
):], s
)
149 self
.assertEqual(res
[len(s
)-1:-1:-1], s
[::-1])
150 self
.assertEqual(res
[len(s
)-1:5:-7], s
[:5:-7])
153 self
.assertRaises(TypeError, operator
.setslice
,
155 self
.assertRaises(TypeError, operator
.setitem
,
156 res
, slice(0, 5), u
"abcde")
159 if sizeof(c_wchar
) == sizeof(c_short
):
160 dll
.my_wcsdup
.restype
= POINTER(c_short
)
161 elif sizeof(c_wchar
) == sizeof(c_int
):
162 dll
.my_wcsdup
.restype
= POINTER(c_int
)
163 elif sizeof(c_wchar
) == sizeof(c_long
):
164 dll
.my_wcsdup
.restype
= POINTER(c_long
)
167 res
= dll
.my_wcsdup(s
)
168 tmpl
= range(ord("a"), ord("z")+1)
169 self
.assertEqual(res
[:len(s
)-1], tmpl
)
170 self
.assertEqual(res
[:len(s
)-1:], tmpl
)
171 self
.assertEqual(res
[len(s
)-2:-1:-1], tmpl
[::-1])
172 self
.assertEqual(res
[len(s
)-2:5:-7], tmpl
[:5:-7])
175 ################################################################
177 if __name__
== "__main__":