Commit 801e9603 authored by John Zhang's avatar John Zhang

refactored tests

parent ef62712d
......@@ -178,4 +178,45 @@ mod aot {
link_dylib_internal(files, out_path)
}
}
mod testutil {
extern crate mu;
extern crate log;
extern crate simple_logger;
extern crate libloading as ll;
use test_ir::test_ir::sum;
use test_ir::test_ir::factorial;
use self::mu::compiler::*;
use self::mu::ast::ir::*;
use self::mu::vm::*;
use std::sync::Arc;
use aot;
pub fn compile_fnc<'a>(fnc_name: &'static str, build_fnc: &'a Fn() -> VM) -> ll::Library {
simple_logger::init_with_level(log::LogLevel::Trace).ok();
let vm = Arc::new(build_fnc());
let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
let func_id = vm.id_of(fnc_name);
{
let funcs = vm.funcs().read().unwrap();
let func = funcs.get(&func_id).unwrap().read().unwrap();
let func_vers = vm.func_vers().read().unwrap();
let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
compiler.compile(&mut func_ver);
}
backend::emit_context(&vm);
let libname = &format!("lib{}.dylib", fnc_name);
let dylib = aot::link_dylib(vec![Mu(fnc_name)], libname);
ll::Library::new(dylib.as_os_str()).unwrap()
}
}
\ No newline at end of file
extern crate mu;
extern crate log;
extern crate simple_logger;
extern crate libloading;
extern crate libloading as ll;
use test_ir::test_ir::sum;
use test_ir::test_ir::factorial;
......@@ -10,73 +10,24 @@ use self::mu::ast::ir::*;
use std::sync::Arc;
use aot;
use testutil;
#[test]
fn test_regalloc_fac() {
simple_logger::init_with_level(log::LogLevel::Trace).ok();
let vm = Arc::new(factorial());
let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
let func_id = vm.id_of("fac");
{
let funcs = vm.funcs().read().unwrap();
let func = funcs.get(&func_id).unwrap().read().unwrap();
let func_vers = vm.func_vers().read().unwrap();
let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
compiler.compile(&mut func_ver);
}
backend::emit_context(&vm);
let dylib = aot::link_dylib(vec![Mu("fac")], "libfac.dylib");
let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
fn test_factorial() {
let lib = testutil::compile_fnc("fac", &factorial);
unsafe {
let fac : libloading::Symbol<unsafe extern fn(u64) -> u64> = lib.get(b"fac").unwrap();
let fac5 = fac(5);
println!("fac(5) = {}", fac5);
assert!(fac5 == 120);
let fac: ll::Symbol<unsafe extern fn (u64) -> u64> = lib.get(b"fac").unwrap();
println!("fac(10) = {}", fac(10));
assert!(fac(10) == 3628800);
}
}
#[test]
fn test_regalloc_sum() {
simple_logger::init_with_level(log::LogLevel::Trace).ok();
let vm = Arc::new(sum());
let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
let func_id = vm.id_of("sum");
{
let funcs = vm.funcs().read().unwrap();
let func = funcs.get(&func_id).unwrap().read().unwrap();
let func_vers = vm.func_vers().read().unwrap();
let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
compiler.compile(&mut func_ver);
}
backend::emit_context(&vm);
let dylib = aot::link_dylib(vec![Mu("sum")], "libsum.dylib");
let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
fn test_sum() {
let lib = testutil::compile_fnc("sum", &sum);
unsafe {
let sum : libloading::Symbol<unsafe extern fn(u64) -> u64> = lib.get(b"sum").unwrap();
let sum5 = sum(5);
println!("sum(1..4) = {}", sum5);
assert!(sum5 == 10);
let sum10 = sum(10);
println!("sum(0..9) = {}", sum10);
assert!(sum10 == 45);
}
}
\ No newline at end of file
let sumptr: ll::Symbol<unsafe extern fn (u64) -> u64> = lib.get(b"sum").unwrap();
assert!(sumptr(5) == 10);
assert!(sumptr(10) == 45);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment