File: test_byref.py

package info (click to toggle)
python-ctypeslib 0.0.0%2Bsvn20100125-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 344 kB
  • ctags: 398
  • sloc: python: 2,845; makefile: 38
file content (41 lines) | stat: -rw-r--r-- 1,434 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import unittest
from ctypeslib.util import byref_at

from ctypes import *

import _ctypes_test

dll = CDLL(_ctypes_test.__file__)

# This test function will accept any ctypes pointer or pointer like
# object, and will return the pointer value as integer.
testfunc = dll._testfunc_p_p
testfunc.argtypes = [c_void_p]
testfunc.restype = c_void_p

class ByrefTest(unittest.TestCase):
    def test_byref_array(self):
        array = (c_int * 8)()
        # Passing an array is the same as passing a byref or pointer
        # to the array
        self.failUnlessEqual(addressof(array), testfunc(array))
        self.failUnlessEqual(addressof(array), testfunc(byref(array)))
        self.failUnlessEqual(addressof(array), testfunc(pointer(array)))

    def test_byref_fundamental(self):
        obj = c_int()
        self.failUnlessEqual(addressof(obj), testfunc(byref(obj)))
        self.failUnlessEqual(addressof(obj), testfunc(pointer(obj)))

    def test_byref_at(self):
        array = (c_int * 8)()
        self.failUnlessEqual(addressof(array) + 0,
                             testfunc(byref_at(array, 0)))
        self.failUnlessEqual(addressof(array) + 1,
                             testfunc(byref_at(array, 1)))
        for ofs in range(8 * sizeof(c_int)):
            self.failUnlessEqual(addressof(array) + ofs,
                                 testfunc(byref_at(array, ofs)))

if __name__ == "__main__":
    unittest.main()