test_rpython.py 2.17 KB
Newer Older
John Zhang's avatar
John Zhang committed
1 2 3 4 5
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.translator.interactive import Translation
import ctypes


John Zhang's avatar
John Zhang committed
6 7
def getfncptr(entry_fnc, argtypes):
    t = Translation(entry_fnc, argtypes,
John Zhang's avatar
John Zhang committed
8 9 10 11
                    backend='mu', muimpl='fast', mucodegen='api', mutestjit=True)
    db, bdlgen, fnc_name = t.compile_mu()
    bdlgen.mu.compile_to_sharedlib('libtesting.dylib', [])
    lib = ctypes.CDLL('emit/libtesting.dylib')
John Zhang's avatar
John Zhang committed
12 13 14 15 16 17 18 19 20 21
    fnp = getattr(lib, fnc_name)
    return fnp


def test_add():
    def add(a, b):
        return a + b

    fn = getfncptr(add, [rffi.LONGLONG, rffi.LONGLONG])
    assert fn(1, 2) == 3
John Zhang's avatar
John Zhang committed
22 23 24 25 26 27 28 29 30 31 32


def test_find_min():
    def find_min(xs, sz):
        m = xs[0]
        for i in range(1, sz):
            x = xs[i]
            if x < m:
                m = x
        return m

John Zhang's avatar
John Zhang committed
33
    fnc = getfncptr(find_min, [rffi.CArrayPtr(rffi.LONGLONG), rffi.UINTPTR_T])
John Zhang's avatar
John Zhang committed
34

John Zhang's avatar
John Zhang committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    arr = (ctypes.c_longlong * 5)(23, 100, 0, 78, -5)
    assert fnc(ctypes.byref(arr), 5) == -5


def test_quicksort():
    # algorithm taken from Wikipedia
    def swap(arr, i, j):
        t = arr[i]
        arr[i] = arr[j]
        arr[j] = t

    def partition(arr, idx_low, idx_high):
        pivot = arr[idx_high]
        i = idx_low
        for j in range(idx_low, idx_high):
            if arr[j] < pivot:
                swap(arr, i, j)
                i += 1
        swap(arr, i, idx_high)
        return i

    def quicksort(arr, start, end):
        if start < end:
            p = partition(arr, start, end)
            quicksort(arr, start, p - 1)
            quicksort(arr, p + 1, end)
John Zhang's avatar
John Zhang committed
61

John Zhang's avatar
John Zhang committed
62
    fnc = getfncptr(quicksort, [rffi.CArrayPtr(rffi.LONGLONG), rffi.UINTPTR_T, rffi.UINTPTR_T])
John Zhang's avatar
John Zhang committed
63

John Zhang's avatar
John Zhang committed
64 65 66 67 68 69 70 71 72
    from random import getrandbits
    from struct import pack, unpack
    n = 20
    lst = [unpack('i', pack('I', getrandbits(32)))[0] for i in range(n)]
    arr = (ctypes.c_longlong * n)(*lst)
    fnc(ctypes.byref(arr), 0, n - 1)    # inplace sort
    lst_s = sorted(lst)
    for i in range(n):
        assert lst_s[i] == arr[i]
John Zhang's avatar
John Zhang committed
73 74 75 76 77 78 79 80

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('testfnc', help="Test function name")
    opts = parser.parse_args()

    globals()[opts.testfnc]()