test_milestones.py 1.71 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
MU_RUST = os.environ["MU_RUST"]
11 12
CFLAGS = [
    "-std=c99",
John Zhang's avatar
John Zhang committed
13 14
    "-I%(MU_RUST)s/src/vm/api" % globals(),
    "-L%(MU_RUST)s/target/debug" % globals(),
15 16 17 18
    "-lmu",
]
os.environ['RUST_BACKTRACE'] = '1'

19 20 21 22
testsuite_dir = py.path.local('suite')

def compile_lib(testname):
    src_c = testsuite_dir.join(testname + '.c')
John Zhang's avatar
John Zhang committed
23
    bin_dir = py.path.local('temp')
24 25 26 27 28
    if not bin_dir.exists():
        bin_dir.mkdir()

    bin_path = bin_dir.join(testname)
    cmd = [CC] + CFLAGS + ['-o', str(bin_path)] + [str(src_c)]
29 30 31 32 33 34 35 36 37 38

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