Exemplo n.º 1
0
def test_function_pretty_signature():
    """Test building the pretty signature of a function.

    """
    library = CLibrary(os.path.basename(_ctypes_test.__file__),
                       ['ctypes_test.h'])
    library.my_strdup.pretty_signature()
Exemplo n.º 2
0
class TestCTypesCLibrary(object):
    """Test the ctypes wrapper functionality.

    """
    def setup(self):
        self.library = CLibrary(_ctypes_test.__file__, ['ctypes_test.h'])

    def test_call(self):
        point_cls = self.library('structs', 'tagpoint')
        point_cls(x=1, y=2)

        with raises(KeyError):
            self.library('r', 't')

    def test_getattr(self):
        assert self.library.an_integer == 42

    def test_getitem(self):
        assert self.library['values']['an_integer'] == 42

    def test_make_struct(self):
        self.library.BITS

    def test_function_call1(self):
        # Test calling a function taking no arguments.
        res = self.library.get_an_integer()
        assert res() == 42

    def test_function_call2(self):
        # Test calling a function without pointers.
        _, (res, ) = self.library.getSPAMANDEGGS()
        egg = res[0]  # we get a pointer of pointer.
        # Needed because this is a byte array in python 3
        assert egg.name.decode('utf8') == 'first egg'
        assert egg.num_spams == 1
        assert egg.spams[0].name.decode('utf8') == 'name1'
        assert egg.spams[0].value.decode('utf8') == 'value1'
        assert egg.spams[1].name.decode('utf8') == 'name2'
        assert egg.spams[1].value.decode('utf8') == 'value2'

    def test_function_call3(self):
        # Test calling a function with an argument and a missing pointer.
        arg = self.library.point(x=1, y=2)
        res, (_, test_point) = self.library._testfunc_byval(arg)
        assert res == 3
        assert test_point.x == arg.x
        assert test_point.y == arg.y
Exemplo n.º 3
0
class TestCTypesCLibrary(object):
    """Test the ctypes wrapper functionality.

    """
    def setup(self):
        self.library = CLibrary(_ctypes_test.__file__, ['ctypes_test.h'])

    def test_call(self):
        point_cls = self.library('structs', 'tagpoint')
        point_cls(x=1, y=2)

        with raises(KeyError):
            self.library('r', 't')

    def test_getattr(self):
        assert self.library.an_integer == 42

    def test_getitem(self):
        assert self.library['values']['an_integer'] == 42

    def test_make_struct(self):
        self.library.BITS

    def test_function_call1(self):
        # Test calling a function taking no arguments.
        res = self.library.get_an_integer()
        assert res() == 42

    def test_function_call2(self):
        # Test calling a function without pointers.
        _, (res,) = self.library.getSPAMANDEGGS()
        egg = res[0]  # we get a pointer of pointer.
        # Needed because this is a byte array in python 3
        assert egg.name.decode('utf8') == 'first egg'
        assert egg.num_spams == 1
        assert egg.spams[0].name.decode('utf8') == 'name1'
        assert egg.spams[0].value.decode('utf8') == 'value1'
        assert egg.spams[1].name.decode('utf8') == 'name2'
        assert egg.spams[1].value.decode('utf8') == 'value2'

    def test_function_call3(self):
        # Test calling a function with an argument and a missing pointer.
        arg = self.library.point(x=1, y=2)
        res, (_, test_point) = self.library._testfunc_byval(arg)
        assert res == 3
        assert test_point.x == arg.x
        assert test_point.y == arg.y
Exemplo n.º 4
0
 def setup(self):
     self.library = CLibrary(_ctypes_test.__file__, ['ctypes_test.h'])
Exemplo n.º 5
0
class TestCTypesCLibrary(object):
    """Test the ctypes wrapper functionality.

    """
    def setup(self):
        self.library = CLibrary(_ctypes_test.__file__, ['ctypes_test.h'])

    def test_call(self):
        point_cls = self.library('structs', 'tagpoint')
        point_cls(x=1, y=2)

        with raises(KeyError):
            self.library('r', 't')

    def test_getattr(self):
        assert self.library.an_integer == 42

    def test_getitem(self):
        assert self.library['values']['an_integer'] == 42

    def test_make_struct(self):
        self.library.BITS

    def test_function_call1(self):
        # Test calling a function taking no arguments.
        res = self.library.get_an_integer()
        assert res() == 42

    def test_function_call2(self):
        # Test calling a function without pointers.
        _, (res, ) = self.library.getSPAMANDEGGS()
        egg = res[0]  # we get a pointer of pointer.
        # Needed because this is a byte array in python 3
        assert egg.name.decode('utf8') == 'first egg'
        assert egg.num_spams == 1
        assert egg.spams[0].name.decode('utf8') == 'name1'
        assert egg.spams[0].value.decode('utf8') == 'value1'
        assert egg.spams[1].name.decode('utf8') == 'name2'
        assert egg.spams[1].value.decode('utf8') == 'value2'

    def test_function_call3(self):
        # Test calling a function with an argument and a missing pointer.
        arg = self.library.point(x=1, y=2)
        res, (_, test_point) = self.library._testfunc_byval(arg)
        assert res == 3
        assert test_point.x == arg.x
        assert test_point.y == arg.y

    def test_function_call4(self):
        """Test calling a funcùtion returning a string

        Will fail if restype is not properly set.

        """
        test_str = 'Test'.encode('utf-8')
        copy = self.library.my_strdup(test_str)()
        assert copy == test_str
        assert copy is not test_str

    def test_cast_to(self):
        """Test casting.

        """
        a = 10
        assert (cast_to(self.library, a, ctypes.c_void_p).value == ctypes.cast(
            a, ctypes.c_void_p).value)

    def test_build_array(self):
        """Test array building.

        """
        pyc_array = build_array(self.library, ctypes.c_void_p, 2, [1, 2])
        c_array = (ctypes.c_void_p * 2)(1, 2)
        assert type(pyc_array) == type(c_array)
        assert pyc_array[0] == c_array[0]
Exemplo n.º 6
0
 def setup(self):
     path = os.path.join(os.path.dirname(__file__), 'test.pyclibcache')
     parser = CParser(['ctypes_test.h'])
     parser.write_cache(path)
     self.library = CLibrary(_ctypes_test.__file__, ['ctypes_test.h'],
                             cache=path)
Exemplo n.º 7
0
 def setup(self):
     self.library = CLibrary(_ctypes_test.__file__, ['ctypes_test.h'])
Exemplo n.º 8
0
class TestCTypesCLibrary(object):
    """Test the ctypes wrapper functionality.

    """
    def setup(self):
        self.library = CLibrary(_ctypes_test.__file__, ['ctypes_test.h'])

    def test_call(self):
        point_cls = self.library('structs', 'tagpoint')
        point_cls(x=1, y=2)

        with raises(KeyError):
            self.library('r', 't')

    def test_getattr(self):
        assert self.library.an_integer == 42

    def test_getitem(self):
        assert self.library['values']['an_integer'] == 42

    def test_make_struct(self):
        self.library.BITS

    def test_function_call1(self):
        # Test calling a function taking no arguments.
        res = self.library.get_an_integer()
        assert res() == 42

    def test_function_call2(self):
        # Test calling a function without pointers.
        _, (res,) = self.library.getSPAMANDEGGS()
        egg = res[0]  # we get a pointer of pointer.
        # Needed because this is a byte array in python 3
        assert egg.name.decode('utf8') == 'first egg'
        assert egg.num_spams == 1
        assert egg.spams[0].name.decode('utf8') == 'name1'
        assert egg.spams[0].value.decode('utf8') == 'value1'
        assert egg.spams[1].name.decode('utf8') == 'name2'
        assert egg.spams[1].value.decode('utf8') == 'value2'

    def test_function_call3(self):
        # Test calling a function with an argument and a missing pointer.
        arg = self.library.point(x=1, y=2)
        res, (_, test_point) = self.library._testfunc_byval(arg)
        assert res == 3
        assert test_point.x == arg.x
        assert test_point.y == arg.y

    def test_function_call4(self):
        """Test calling a funcùtion returning a string

        Will fail if restype is not properly set.

        """
        test_str = 'Test'.encode('utf-8')
        copy = self.library.my_strdup(test_str)()
        assert copy == test_str
        assert copy is not test_str

    def test_cast_to(self):
        """Test casting.

        """
        a = 10
        assert (cast_to(self.library, a, ctypes.c_void_p).value ==
                ctypes.cast(a, ctypes.c_void_p).value)

    def test_build_array(self):
        """Test array building.

        """
        pyc_array = build_array(self.library, ctypes.c_void_p, 2, [1, 2])
        c_array = (ctypes.c_void_p*2)(1, 2)
        assert type(pyc_array) == type(c_array)
        assert pyc_array[0] == c_array[0]
Exemplo n.º 9
0
    def test_already_opened_library(self):

        lib = ctypes.CDLL(_ctypes_test.__file__)
        library = CLibrary(lib, ['ctypes_test.h'])
        assert library is CLibrary(_ctypes_test.__file__, ['ctypes_test.h'])
Exemplo n.º 10
0
    def test_accessing_library_by_object(self):

        lib = ctypes.CDLL(_ctypes_test.__file__)
        library = CLibrary(lib, ['ctypes_test.h'])
        assert library._lib_ is lib
Exemplo n.º 11
0
    def test_accessing_library_by_path(self):

        library = CLibrary(_ctypes_test.__file__, ['ctypes_test.h'])
        assert library._lib_
Exemplo n.º 12
0
    def test_accessing_library_by_name(self, library_location_fixture):

        library = CLibrary(os.path.basename(_ctypes_test.__file__),
                           ['ctypes_test.h'])
        assert library._lib_