PageRenderTime 34ms CodeModel.GetById 12ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/ctypes/test/test_slicing.py

http://unladen-swallow.googlecode.com/
Python | 178 lines | 146 code | 27 blank | 5 comment | 2 complexity | 418ea0284f4646cee053e7ba0bdf1c59 MD5 | raw file
  1import unittest
  2from ctypes import *
  3
  4import _ctypes_test
  5
  6class SlicesTestCase(unittest.TestCase):
  7    def test_getslice_cint(self):
  8        a = (c_int * 100)(*xrange(1100, 1200))
  9        b = range(1100, 1200)
 10        self.failUnlessEqual(a[0:2], b[0:2])
 11        self.failUnlessEqual(a[0:2:], b[0:2:])
 12        self.failUnlessEqual(len(a), len(b))
 13        self.failUnlessEqual(a[5:7], b[5:7])
 14        self.failUnlessEqual(a[5:7:], b[5:7:])
 15        self.failUnlessEqual(a[-1], b[-1])
 16        self.failUnlessEqual(a[:], b[:])
 17        self.failUnlessEqual(a[::], b[::])
 18        self.failUnlessEqual(a[10::-1], b[10::-1])
 19        self.failUnlessEqual(a[30:20:-1], b[30:20:-1])
 20        self.failUnlessEqual(a[:12:6], b[:12:6])
 21        self.failUnlessEqual(a[2:6:4], b[2:6:4])
 22
 23        a[0:5] = range(5, 10)
 24        self.failUnlessEqual(a[0:5], range(5, 10))
 25        self.failUnlessEqual(a[0:5:], range(5, 10))
 26        self.failUnlessEqual(a[4::-1], range(9, 4, -1))
 27
 28    def test_setslice_cint(self):
 29        a = (c_int * 100)(*xrange(1100, 1200))
 30        b = range(1100, 1200)
 31
 32        a[32:47] = range(32, 47)
 33        self.failUnlessEqual(a[32:47], range(32, 47))
 34        a[32:47] = range(132, 147)
 35        self.failUnlessEqual(a[32:47:], range(132, 147))
 36        a[46:31:-1] = range(232, 247)
 37        self.failUnlessEqual(a[32:47:1], range(246, 231, -1))
 38
 39        a[32:47] = range(1132, 1147)
 40        self.failUnlessEqual(a[:], b)
 41        a[32:47:7] = range(3)
 42        b[32:47:7] = range(3)
 43        self.failUnlessEqual(a[:], b)
 44        a[33::-3] = range(12)
 45        b[33::-3] = range(12)
 46        self.failUnlessEqual(a[:], b)
 47
 48        from operator import setslice, setitem
 49
 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),
 60                          [1, 2, 3, 4, 3.14])
 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))
 64
 65    def test_char_ptr(self):
 66        s = "abcdefghijklmnopqrstuvwxyz"
 67
 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.failUnlessEqual(res[:len(s)], s)
 73        self.failUnlessEqual(res[:3], s[:3])
 74        self.failUnlessEqual(res[:len(s):], s)
 75        self.failUnlessEqual(res[len(s)-1:-1:-1], s[::-1])
 76        self.failUnlessEqual(res[len(s)-1:5:-7], s[:5:-7])
 77        self.failUnlessEqual(res[0:-1:-1], s[0::-1])
 78
 79        import operator
 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))
 88
 89        self.assertRaises(TypeError, operator.setslice,
 90                          res, 0, 5, u"abcde")
 91        self.assertRaises(TypeError, operator.setitem,
 92                          res, slice(0, 5), u"abcde")
 93        dll.my_free(res)
 94
 95        dll.my_strdup.restype = POINTER(c_byte)
 96        res = dll.my_strdup(s)
 97        self.failUnlessEqual(res[:len(s)], range(ord("a"), ord("z")+1))
 98        self.failUnlessEqual(res[:len(s):], range(ord("a"), ord("z")+1))
 99        dll.my_free(res)
100
101    def test_char_ptr_with_free(self):
102        dll = CDLL(_ctypes_test.__file__)
103        s = "abcdefghijklmnopqrstuvwxyz"
104
105        class allocated_c_char_p(c_char_p):
106            pass
107
108        dll.my_free.restype = None
109        def errcheck(result, func, args):
110            retval = result.value
111            dll.my_free(result)
112            return retval
113
114        dll.my_strdup.restype = allocated_c_char_p
115        dll.my_strdup.errcheck = errcheck
116        try:
117            res = dll.my_strdup(s)
118            self.failUnlessEqual(res, s)
119        finally:
120            del dll.my_strdup.errcheck
121
122
123    def test_char_array(self):
124        s = "abcdefghijklmnopqrstuvwxyz\0"
125
126        p = (c_char * 27)(*s)
127        self.failUnlessEqual(p[:], s)
128        self.failUnlessEqual(p[::], s)
129        self.failUnlessEqual(p[::-1], s[::-1])
130        self.failUnlessEqual(p[5::-2], s[5::-2])
131        self.failUnlessEqual(p[2:5:-3], s[2:5:-3])
132
133
134    try:
135        c_wchar
136    except NameError:
137        pass
138    else:
139        def test_wchar_ptr(self):
140            s = u"abcdefghijklmnopqrstuvwxyz\0"
141
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.failUnlessEqual(res[:len(s)], s)
148            self.failUnlessEqual(res[:len(s):], s)
149            self.failUnlessEqual(res[len(s)-1:-1:-1], s[::-1])
150            self.failUnlessEqual(res[len(s)-1:5:-7], s[:5:-7])
151
152            import operator
153            self.assertRaises(TypeError, operator.setslice,
154                              res, 0, 5, u"abcde")
155            self.assertRaises(TypeError, operator.setitem,
156                              res, slice(0, 5), u"abcde")
157            dll.my_free(res)
158
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)
165            else:
166                return
167            res = dll.my_wcsdup(s)
168            tmpl = range(ord("a"), ord("z")+1)
169            self.failUnlessEqual(res[:len(s)-1], tmpl)
170            self.failUnlessEqual(res[:len(s)-1:], tmpl)
171            self.failUnlessEqual(res[len(s)-2:-1:-1], tmpl[::-1])
172            self.failUnlessEqual(res[len(s)-2:5:-7], tmpl[:5:-7])
173            dll.my_free(res)
174
175################################################################
176
177if __name__ == "__main__":
178    unittest.main()