File: test__rawffi.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (56 lines) | stat: -rw-r--r-- 1,517 bytes parent folder | download | duplicates (8)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import unittest
from test import test_support

# xxx - forces a skip in the case we haven't built ctypes_test module in ant (which is not yet a task as of now)

try:
    import _rawffi
    _rawffi.CDLL("ctypes_test")
except:
    raise ImportError

class RawFFITestCase(unittest.TestCase):

    def setUp(self):
        self.libc_name = "c"
        self.lib_name = "ctypes_test"

    def test_libload(self):
        import _rawffi
        _rawffi.CDLL(self.libc_name)

    def test_libc_load(self):
        import _rawffi
        _rawffi.get_libc()

    def test_getattr(self):
        import _rawffi
        libc = _rawffi.get_libc()
        func = libc.ptr('rand', [], 'i')
        assert libc.ptr('rand', [], 'i') is func # caching
        assert libc.ptr('rand', [], 'l') is not func
        assert isinstance(func, _rawffi.FuncPtr)
        self.assertRaises(AttributeError, getattr, libc, "xxx")

    def test_short_addition(self):
        import _rawffi
        lib = _rawffi.CDLL(self.lib_name)
        short_add = lib.ptr('add_shorts', ['h', 'h'], 'H')
        A = _rawffi.Array('h')
        arg1 = A(1, autofree=True)
        arg2 = A(1, autofree=True)
        arg1[0] = 1
        arg2[0] = 2
        res = short_add(arg1, arg2)
        assert res[0] == 3
        # this does not apply to this version of memory allocation
        #arg1.free()
        #arg2.free()

def test_main():
    tests = [RawFFITestCase,
             ]
    test_support.run_unittest(*tests)

if __name__ == '__main__':
    test_main()