test_milestones.py 2.01 KB
Newer Older
1 2 3 4 5 6
"""
Harness JIT tests using py.test framework
"""
import subprocess as subp
import os, sys
import ctypes
7
import py
8 9

CC = 'clang'
John Zhang's avatar
John Zhang committed
10 11 12 13 14 15
proj_dir = py.path.local(__file__).join('..', '..', '..')
test_jit_dir = proj_dir.join('tests', 'test_jit')
testsuite_dir = test_jit_dir.join('suite')
bin_dir = test_jit_dir.join('temp')
if not bin_dir.exists():
    bin_dir.mkdir()
16 17 18 19

def compile_lib(testname):
    src_c = testsuite_dir.join(testname + '.c')
    bin_path = bin_dir.join(testname)
John Zhang's avatar
John Zhang committed
20 21 22 23 24 25
    CFLAGS = [
        "-std=c99",
        "-I%(proj_dir)s/src/vm/api" % globals(),
        "-L%(proj_dir)s/target/debug" % globals(),
        "-lmu",
    ]
26
    cmd = [CC] + CFLAGS + ['-o', str(bin_path)] + [str(src_c)]
27 28 29 30 31 32 33 34

    # compile
    p = subp.Popen(cmd, stdout=subp.PIPE, stderr=subp.PIPE, env=os.environ)
    out, err = p.communicate()
    if p.returncode != 0:  # failed
        sys.stdout.write(out + '\n')
        sys.stderr.write(err + '\n')
        raise subp.CalledProcessError(p.returncode, cmd)
35 36 37
     
    os.environ['LD_LIBRARY_PATH'] = "%s:%s" % ("%(proj_dir)s/target/debug" % globals(),
                                               os.environ['LD_LIBRARY_PATH'])
38
    # run
39
    p = subp.Popen([str(bin_path)], stdout=subp.PIPE, stderr=subp.PIPE, cwd=str(bin_dir), env=os.environ)
40 41 42 43 44 45
    out, err = p.communicate()
    if p.returncode != 0:  # failed
        sys.stdout.write(out + '\n')
        sys.stderr.write(err + '\n')
        raise subp.CalledProcessError(p.returncode, bin_path)

46
    return str(bin_dir.join('emit', testname + '.dylib'))
47

48 49 50
def get_fncptr(testname, entry_fnc):
    lib_path = compile_lib(testname)
    lib = ctypes.CDLL(lib_path)
John Zhang's avatar
John Zhang committed
51
    return getattr(lib, entry_fnc)
52

John Zhang's avatar
John Zhang committed
53
def test_constant_function():
54
    fn = get_fncptr("test_constfunc", 'test_fnc')
55 56
    assert fn() == 0

57 58 59 60
def test_milsum():
    fn = get_fncptr("test_milsum", "milsum")
    assert fn(1000000) == 500000500000

61
def test_fibonacci():
62
    fn = get_fncptr("test_fib", "fib")
63
    assert fn(20) == 6765
64 65

def test_multifunc():
66
    fn = get_fncptr("test_multifunc", "entry")
67
    assert fn() == 6765