test_milestones.py 1.8 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

John Zhang's avatar
John Zhang committed
17 18 19
print 'proj_dir =', proj_dir
print 'bin_dir =', bin_dir

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

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

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

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

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

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

def test_multifunc():
63
    fn = get_fncptr("test_multifunc", "entry")
64
    assert fn() == 6765