Back to index

python3.2  3.2.2
Public Member Functions
ctypes.test.test_slicing.SlicesTestCase Class Reference

List of all members.

Public Member Functions

def test_getslice_cint
def test_setslice_cint
def test_char_ptr
def test_char_ptr_with_free
def test_char_array
def test_wchar_ptr

Detailed Description

Definition at line 6 of file test_slicing.py.


Member Function Documentation

Definition at line 117 of file test_slicing.py.

00117 
00118     def test_char_array(self):
00119         s = b"abcdefghijklmnopqrstuvwxyz\0"
00120 
00121         p = (c_char * 27)(*s)
00122         self.assertEqual(p[:], s)
00123         self.assertEqual(p[::], s)
00124         self.assertEqual(p[::-1], s[::-1])
00125         self.assertEqual(p[5::-2], s[5::-2])
00126         self.assertEqual(p[2:5:-3], s[2:5:-3])
00127 

Here is the call graph for this function:

Definition at line 61 of file test_slicing.py.

00061 
00062     def test_char_ptr(self):
00063         s = b"abcdefghijklmnopqrstuvwxyz"
00064 
00065         dll = CDLL(_ctypes_test.__file__)
00066         dll.my_strdup.restype = POINTER(c_char)
00067         dll.my_free.restype = None
00068         res = dll.my_strdup(s)
00069         self.assertEqual(res[:len(s)], s)
00070         self.assertEqual(res[:3], s[:3])
00071         self.assertEqual(res[:len(s):], s)
00072         self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
00073         self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
00074         self.assertEqual(res[0:-1:-1], s[0::-1])
00075 
00076         import operator
00077         self.assertRaises(ValueError, operator.getitem,
00078                           res, slice(None, None, None))
00079         self.assertRaises(ValueError, operator.getitem,
00080                           res, slice(0, None, None))
00081         self.assertRaises(ValueError, operator.getitem,
00082                           res, slice(None, 5, -1))
00083         self.assertRaises(ValueError, operator.getitem,
00084                           res, slice(-5, None, None))
00085 
00086         self.assertRaises(TypeError, operator.setitem,
00087                           res, slice(0, 5), "abcde")
00088         dll.my_free(res)
00089 
00090         dll.my_strdup.restype = POINTER(c_byte)
00091         res = dll.my_strdup(s)
00092         self.assertEqual(res[:len(s)], list(range(ord("a"), ord("z")+1)))
00093         self.assertEqual(res[:len(s):], list(range(ord("a"), ord("z")+1)))
00094         dll.my_free(res)

Here is the call graph for this function:

Definition at line 95 of file test_slicing.py.

00095 
00096     def test_char_ptr_with_free(self):
00097         dll = CDLL(_ctypes_test.__file__)
00098         s = b"abcdefghijklmnopqrstuvwxyz"
00099 
00100         class allocated_c_char_p(c_char_p):
00101             pass
00102 
00103         dll.my_free.restype = None
00104         def errcheck(result, func, args):
00105             retval = result.value
00106             dll.my_free(result)
00107             return retval
00108 
00109         dll.my_strdup.restype = allocated_c_char_p
00110         dll.my_strdup.errcheck = errcheck
00111         try:
00112             res = dll.my_strdup(s)
00113             self.assertEqual(res, s)
00114         finally:
00115             del dll.my_strdup.errcheck
00116 

Definition at line 7 of file test_slicing.py.

00007 
00008     def test_getslice_cint(self):
00009         a = (c_int * 100)(*range(1100, 1200))
00010         b = list(range(1100, 1200))
00011         self.assertEqual(a[0:2], b[0:2])
00012         self.assertEqual(a[0:2:], b[0:2:])
00013         self.assertEqual(len(a), len(b))
00014         self.assertEqual(a[5:7], b[5:7])
00015         self.assertEqual(a[5:7:], b[5:7:])
00016         self.assertEqual(a[-1], b[-1])
00017         self.assertEqual(a[:], b[:])
00018         self.assertEqual(a[::], b[::])
00019         self.assertEqual(a[10::-1], b[10::-1])
00020         self.assertEqual(a[30:20:-1], b[30:20:-1])
00021         self.assertEqual(a[:12:6], b[:12:6])
00022         self.assertEqual(a[2:6:4], b[2:6:4])
00023 
00024         a[0:5] = range(5, 10)
00025         self.assertEqual(a[0:5], list(range(5, 10)))
00026         self.assertEqual(a[0:5:], list(range(5, 10)))
00027         self.assertEqual(a[4::-1], list(range(9, 4, -1)))

Here is the call graph for this function:

Definition at line 28 of file test_slicing.py.

00028 
00029     def test_setslice_cint(self):
00030         a = (c_int * 100)(*range(1100, 1200))
00031         b = list(range(1100, 1200))
00032 
00033         a[32:47] = list(range(32, 47))
00034         self.assertEqual(a[32:47], list(range(32, 47)))
00035         a[32:47] = range(132, 147)
00036         self.assertEqual(a[32:47:], list(range(132, 147)))
00037         a[46:31:-1] = range(232, 247)
00038         self.assertEqual(a[32:47:1], list(range(246, 231, -1)))
00039 
00040         a[32:47] = range(1132, 1147)
00041         self.assertEqual(a[:], b)
00042         a[32:47:7] = range(3)
00043         b[32:47:7] = range(3)
00044         self.assertEqual(a[:], b)
00045         a[33::-3] = range(12)
00046         b[33::-3] = range(12)
00047         self.assertEqual(a[:], b)
00048 
00049         from operator import setitem
00050 
00051         # TypeError: int expected instead of str instance
00052         self.assertRaises(TypeError, setitem, a, slice(0, 5), "abcde")
00053         # TypeError: int expected instead of str instance
00054         self.assertRaises(TypeError, setitem, a, slice(0, 5),
00055                           ["a", "b", "c", "d", "e"])
00056         # TypeError: int expected instead of float instance
00057         self.assertRaises(TypeError, setitem, a, slice(0, 5),
00058                           [1, 2, 3, 4, 3.14])
00059         # ValueError: Can only assign sequence of same size
00060         self.assertRaises(ValueError, setitem, a, slice(0, 5), range(32))

Here is the call graph for this function:

Definition at line 133 of file test_slicing.py.

00133 
00134         def test_wchar_ptr(self):
00135             s = "abcdefghijklmnopqrstuvwxyz\0"
00136 
00137             dll = CDLL(_ctypes_test.__file__)
00138             dll.my_wcsdup.restype = POINTER(c_wchar)
00139             dll.my_wcsdup.argtypes = POINTER(c_wchar),
00140             dll.my_free.restype = None
00141             res = dll.my_wcsdup(s)
00142             self.assertEqual(res[:len(s)], s)
00143             self.assertEqual(res[:len(s):], s)
00144             self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
00145             self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
00146 
00147             import operator
00148             self.assertRaises(TypeError, operator.setitem,
00149                               res, slice(0, 5), "abcde")
00150             dll.my_free(res)
00151 
00152             if sizeof(c_wchar) == sizeof(c_short):
00153                 dll.my_wcsdup.restype = POINTER(c_short)
00154             elif sizeof(c_wchar) == sizeof(c_int):
00155                 dll.my_wcsdup.restype = POINTER(c_int)
00156             elif sizeof(c_wchar) == sizeof(c_long):
00157                 dll.my_wcsdup.restype = POINTER(c_long)
00158             else:
00159                 return
00160             res = dll.my_wcsdup(s)
00161             tmpl = list(range(ord("a"), ord("z")+1))
00162             self.assertEqual(res[:len(s)-1], tmpl)
00163             self.assertEqual(res[:len(s)-1:], tmpl)
00164             self.assertEqual(res[len(s)-2:-1:-1], tmpl[::-1])
00165             self.assertEqual(res[len(s)-2:5:-7], tmpl[:5:-7])
00166             dll.my_free(res)

Here is the call graph for this function:


The documentation for this class was generated from the following file: