Skip to content

Commit 7a4765d

Browse files
committed
tests: Add testcase for ffi callbacks.
1 parent b62371e commit 7a4765d

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

tests/unix/ffi_callback.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
try:
3+
import ffi
4+
except ImportError:
5+
print("SKIP")
6+
sys.exit()
7+
8+
libc = ffi.open("libc.so.6")
9+
10+
qsort = libc.func("v", "qsort", "piip")
11+
12+
def cmp(pa, pb):
13+
a = ffi.as_bytearray(pa, 1)
14+
b = ffi.as_bytearray(pb, 1)
15+
#print("cmp:", a, b)
16+
return a[0] - b[0]
17+
18+
cmp_c = ffi.callback("i", cmp, "pp")
19+
20+
s = bytearray(b"foobar")
21+
print("org string:", s)
22+
qsort(s, len(s), 1, cmp_c)
23+
print("qsort'ed:", s)

tests/unix/ffi_callback.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org string: bytearray(b'foobar')
2+
qsort'ed: bytearray(b'abfoor')

0 commit comments

Comments
 (0)