tests: Add testcase for ffi callbacks.
This commit is contained in:
parent
b62371e8fb
commit
7a4765dbeb
23
tests/unix/ffi_callback.py
Normal file
23
tests/unix/ffi_callback.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import sys
|
||||||
|
try:
|
||||||
|
import ffi
|
||||||
|
except ImportError:
|
||||||
|
print("SKIP")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
libc = ffi.open("libc.so.6")
|
||||||
|
|
||||||
|
qsort = libc.func("v", "qsort", "piip")
|
||||||
|
|
||||||
|
def cmp(pa, pb):
|
||||||
|
a = ffi.as_bytearray(pa, 1)
|
||||||
|
b = ffi.as_bytearray(pb, 1)
|
||||||
|
#print("cmp:", a, b)
|
||||||
|
return a[0] - b[0]
|
||||||
|
|
||||||
|
cmp_c = ffi.callback("i", cmp, "pp")
|
||||||
|
|
||||||
|
s = bytearray(b"foobar")
|
||||||
|
print("org string:", s)
|
||||||
|
qsort(s, len(s), 1, cmp_c)
|
||||||
|
print("qsort'ed:", s)
|
2
tests/unix/ffi_callback.py.exp
Normal file
2
tests/unix/ffi_callback.py.exp
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
org string: bytearray(b'foobar')
|
||||||
|
qsort'ed: bytearray(b'abfoor')
|
Loading…
Reference in New Issue
Block a user