def test_qsort_callback(self): TP = rffi.CArrayPtr(rffi.INT) a = lltype.malloc(TP.TO, 5, flavor='raw') a[0] = rffi.r_int(5) a[1] = rffi.r_int(3) a[2] = rffi.r_int(2) a[3] = rffi.r_int(1) a[4] = rffi.r_int(4) def compare(a, b): if a[0] > b[0]: return rffi.r_int(1) else: return rffi.r_int(-1) CALLBACK = rffi.CCallback([rffi.VOIDP, rffi.VOIDP], rffi.INT) qsort = rffi.llexternal('qsort', [rffi.VOIDP, rffi.SIZE_T, rffi.SIZE_T, CALLBACK], lltype.Void) qsort(rffi.cast(rffi.VOIDP, a), 5, rffi.sizeof(rffi.INT), compare) for i in range(5): assert a[i] == i + 1
def test_qsort_callback(self): TP = rffi.CArrayPtr(rffi.INT) a = lltype.malloc(TP.TO, 5, flavor='raw') a[0] = rffi.r_int(5) a[1] = rffi.r_int(3) a[2] = rffi.r_int(2) a[3] = rffi.r_int(1) a[4] = rffi.r_int(4) def compare(a, b): if a[0] > b[0]: return rffi.r_int(1) else: return rffi.r_int(-1) CALLBACK = rffi.CCallback([rffi.VOIDP, rffi.VOIDP], rffi.INT) qsort = rffi.llexternal( 'qsort', [rffi.VOIDP, rffi.SIZE_T, rffi.SIZE_T, CALLBACK], lltype.Void) qsort(rffi.cast(rffi.VOIDP, a), 5, rffi.sizeof(rffi.INT), compare) for i in range(5): assert a[i] == i + 1
def compare(a, b): if a[0] > b[0]: return rffi.r_int(1) else: return rffi.r_int(-1)