test_milestones.py 1.76 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 35 36

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

    # run
37 38
    p = subp.Popen([str(bin_path)], stdout=subp.PIPE, stderr=subp.PIPE,
                   cwd=str(bin_dir), env=os.environ)
39 40 41 42 43 44
    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)

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

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

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

56
def test_fibonacci():
57
    fn = get_fncptr("test_fib", "fib")
58
    assert fn(20) == 6765
59 60

def test_multifunc():
61
    fn = get_fncptr("test_multifunc", "entry")
62
    assert fn() == 6765