tests: Add testcase for ffi callbacks.

This commit is contained in:
Paul Sokolovsky 2014-12-15 02:18:12 +02:00
parent b62371e8fb
commit 7a4765dbeb
2 changed files with 25 additions and 0 deletions

View 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)

View File

@ -0,0 +1,2 @@
org string: bytearray(b'foobar')
qsort'ed: bytearray(b'abfoor')