Back to index

python3.2  3.2.2
Public Member Functions
ctypes.test.test_cast.Test Class Reference

List of all members.

Public Member Functions

def test_array2pointer
def test_address2pointer
def test_p2a_objects
def test_other
def test_char_p
def test_wchar_p

Detailed Description

Definition at line 5 of file test_cast.py.


Member Function Documentation

Definition at line 23 of file test_cast.py.

00023 
00024     def test_address2pointer(self):
00025         array = (c_int * 3)(42, 17, 2)
00026 
00027         address = addressof(array)
00028         ptr = cast(c_void_p(address), POINTER(c_int))
00029         self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
00030 
00031         ptr = cast(address, POINTER(c_int))
00032         self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])

Here is the call graph for this function:

Definition at line 7 of file test_cast.py.

00007 
00008     def test_array2pointer(self):
00009         array = (c_int * 3)(42, 17, 2)
00010 
00011         # casting an array to a pointer works.
00012         ptr = cast(array, POINTER(c_int))
00013         self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
00014 
00015         if 2*sizeof(c_short) == sizeof(c_int):
00016             ptr = cast(array, POINTER(c_short))
00017             if sys.byteorder == "little":
00018                 self.assertEqual([ptr[i] for i in range(6)],
00019                                      [42, 0, 17, 0, 2, 0])
00020             else:
00021                 self.assertEqual([ptr[i] for i in range(6)],
00022                                      [0, 42, 0, 17, 0, 2])

Here is the call graph for this function:

Definition at line 72 of file test_cast.py.

00072 
00073     def test_char_p(self):
00074         # This didn't work: bad argument to internal function
00075         s = c_char_p(b"hiho")
00076         self.assertEqual(cast(cast(s, c_void_p), c_char_p).value,
00077                              b"hiho")

Here is the call graph for this function:

Definition at line 50 of file test_cast.py.

00050 
00051     def test_other(self):
00052         p = cast((c_int * 4)(1, 2, 3, 4), POINTER(c_int))
00053         self.assertEqual(p[:4], [1,2, 3, 4])
00054         self.assertEqual(p[:4:], [1, 2, 3, 4])
00055         self.assertEqual(p[3:-1:-1], [4, 3, 2, 1])
00056         self.assertEqual(p[:4:3], [1, 4])
00057         c_int()
00058         self.assertEqual(p[:4], [1, 2, 3, 4])
00059         self.assertEqual(p[:4:], [1, 2, 3, 4])
00060         self.assertEqual(p[3:-1:-1], [4, 3, 2, 1])
00061         self.assertEqual(p[:4:3], [1, 4])
00062         p[2] = 96
00063         self.assertEqual(p[:4], [1, 2, 96, 4])
00064         self.assertEqual(p[:4:], [1, 2, 96, 4])
00065         self.assertEqual(p[3:-1:-1], [4, 96, 2, 1])
00066         self.assertEqual(p[:4:3], [1, 4])
00067         c_int()
00068         self.assertEqual(p[:4], [1, 2, 96, 4])
00069         self.assertEqual(p[:4:], [1, 2, 96, 4])
00070         self.assertEqual(p[3:-1:-1], [4, 96, 2, 1])
00071         self.assertEqual(p[:4:3], [1, 4])

Here is the call graph for this function:

Definition at line 33 of file test_cast.py.

00033 
00034     def test_p2a_objects(self):
00035         array = (c_char_p * 5)()
00036         self.assertEqual(array._objects, None)
00037         array[0] = b"foo bar"
00038         self.assertEqual(array._objects, {'0': b"foo bar"})
00039 
00040         p = cast(array, POINTER(c_char_p))
00041         # array and p share a common _objects attribute
00042         self.assertTrue(p._objects is array._objects)
00043         self.assertEqual(array._objects, {'0': b"foo bar", id(array): array})
00044         p[0] = b"spam spam"
00045         self.assertEqual(p._objects, {'0': b"spam spam", id(array): array})
00046         self.assertTrue(array._objects is p._objects)
00047         p[1] = b"foo bar"
00048         self.assertEqual(p._objects, {'1': b'foo bar', '0': b"spam spam", id(array): array})
00049         self.assertTrue(array._objects is p._objects)

Here is the call graph for this function:

Definition at line 83 of file test_cast.py.

00083 
00084         def test_wchar_p(self):
00085             s = c_wchar_p("hiho")
00086             self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
00087                                  "hiho")

Here is the call graph for this function:


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