Commit 7220a589 authored by qinsoon's avatar qinsoon

add more tests around rpython comparison, and list

parent f641adb9
......@@ -13,3 +13,4 @@ mod test_controlflow;
mod test_call;
mod test_mem_inst;
mod test_inline;
mod test_convop;
\ No newline at end of file
extern crate mu;
extern crate log;
extern crate libloading;
use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::ast::op::*;
use self::mu::vm::*;
use self::mu::testutil;
use mu::utils::LinkedHashMap;
use std::sync::RwLock;
#[test]
fn test_truncate_then_call() {
let lib = testutil::compile_fncs("truncate_then_call", vec!["truncate_then_call", "dummy_call"], &truncate_then_call);
unsafe {
let truncate_then_call : libloading::Symbol<unsafe extern fn(u64) -> u32> = lib.get(b"truncate_then_call").unwrap();
let res = truncate_then_call(1);
println!("truncate_then_call(1) = {}", res);
assert!(res == 1);
}
}
fn truncate_then_call() -> VM {
let vm = VM::new_with_opts("init_mu --disable-inline");
typedef! ((vm) u64 = mu_int(64));
typedef! ((vm) u32 = mu_int(32));
funcsig! ((vm) dummy_call_sig = (u32) -> (u32));
funcdecl!((vm) <dummy_call_sig> dummy_call);
{
// --- dummy call ---
funcdef! ((vm) <dummy_call_sig> dummy_call VERSION dummy_call_v1);
// entry
block! ((vm, dummy_call_v1) blk_entry);
ssa! ((vm, dummy_call_v1) <u32> x);
inst! ((vm, dummy_call_v1) ret:
RET (x)
);
define_block!((vm, dummy_call_v1) blk_entry(x) {
ret
});
define_func_ver!((vm) dummy_call_v1 (entry: blk_entry) {
blk_entry
});
}
{
// --- truncate_then_call ---
typedef! ((vm) funcref_to_dummy = mu_funcref(dummy_call_sig));
constdef!((vm) <funcref_to_dummy> funcref_dummy = Constant::FuncRef(dummy_call));
funcsig! ((vm) sig = (u64) -> (u32));
funcdecl!((vm) <sig> truncate_then_call);
funcdef! ((vm) <sig> truncate_then_call VERSION truncate_then_call_v1);
// entry
block!((vm, truncate_then_call_v1) blk_entry);
ssa! ((vm, truncate_then_call_v1) <u64> arg);
// %arg_u32 = TRUNC <u64 u32> arg
ssa! ((vm, truncate_then_call_v1) <u32> arg_u32);
inst!((vm, truncate_then_call_v1) blk_entry_truncate:
arg_u32 = CONVOP (ConvOp::TRUNC) <u64 u32> arg
);
// %ret = CALL dummy_call (arg_u32)
ssa! ((vm, truncate_then_call_v1) <u32> res);
consta! ((vm, truncate_then_call_v1) funcref_dummy_local = funcref_dummy);
inst! ((vm, truncate_then_call_v1) blk_entry_call:
res = EXPRCALL (CallConvention::Mu, is_abort: false) funcref_dummy_local (arg_u32)
);
inst!((vm, truncate_then_call_v1) blk_entry_ret:
RET (arg)
);
define_block!((vm, truncate_then_call_v1) blk_entry(arg) {
blk_entry_truncate,
blk_entry_call,
blk_entry_ret
});
define_func_ver!((vm) truncate_then_call_v1 (entry: blk_entry) {
blk_entry
});
}
vm
}
\ No newline at end of file
......@@ -1075,31 +1075,6 @@ def test_rpython_main():
assert res.returncode == 0, res.err
@pytest.mark.xfail(reason = "new test")
@may_spawn_proc
def test_rpython_list_append():
from rpython.translator.interactive import Translation
def main(argv):
a = list_append(5)
return a
def list_append(n):
l = []
for i in range(0, n):
l.append(i)
sum = 0
for i in l:
sum += i
return sum
res = run_boot_image(main, '/tmp/test_list_append')
print res.returncode
assert res.returncode == 10, res.err
@pytest.mark.skipif("True")
@may_spawn_proc
def test_rpytarget_sha1sum():
......
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib import rmu_fast as rmu
from rpython.translator.platform import platform
from util import fncptr_from_rpy_func, fncptr_from_py_script, may_spawn_proc
import ctypes, py, stat
import pytest
from test_rpython import run_boot_image
@may_spawn_proc
def test_rpython_int_cmp():
def int_cmp(a, b):
if a > b:
return 1
elif a == b:
return 0
else:
return -1
mu_int_cmp, _ = fncptr_from_rpy_func(int_cmp, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)
assert mu_int_cmp(1, 0) == 1
assert mu_int_cmp(0, 1) == -1
assert mu_int_cmp(0, 0) == 0
assert mu_int_cmp(1, 1) == 0
assert mu_int_cmp(1, -1) == 1
assert mu_int_cmp(-1, 1) == -1
assert mu_int_cmp(-1, -2) == 1
assert mu_int_cmp(-2, -1) == -1
assert mu_int_cmp(-1, -1) == 0
assert mu_int_cmp(9223372036854775807, -9223372036854775808) == 1
assert mu_int_cmp(18446744073709551615, 9223372036854775807) == -1
assert mu_int_cmp(18446744073709551615, -9223372036854775808) == 1
assert mu_int_cmp(18446744073709551615, -1) == 0
@may_spawn_proc
def test_rpython_int_cmp_zero():
def int_cmp_zero(a):
if a > 0:
return 1
elif a == 0:
return 0
else:
return -1
mu_int_cmp_zero, _ = fncptr_from_rpy_func(int_cmp_zero, [rffi.LONGLONG], rffi.LONGLONG)
assert mu_int_cmp_zero(1) == 1
assert mu_int_cmp_zero(9223372036854775807) == 1
assert mu_int_cmp_zero(18446744073709551615) == -1
assert mu_int_cmp_zero(0) == 0
assert mu_int_cmp_zero(-1) == -1
assert mu_int_cmp_zero(-9223372036854775808) == -1
@may_spawn_proc
def test_rpython_int_cmp_const():
# these may get optimized away by Rpython compiler
def int_cmp_zero_eq_zero():
if 0 == 0:
return 1
else:
return 0
def int_cmp_zero_ne_zero():
if 0 != 0:
return 0
else:
return 1
def int_cmp_zero_eq_one():
if 0 == 1:
return 0
else:
return 1
def int_cmp_zero_ne_one():
if 0 != 1:
return 1
else:
return 0
mu_int_cmp_zero_eq_zero, _ = fncptr_from_rpy_func(int_cmp_zero_eq_zero, [], rffi.LONGLONG)
mu_int_cmp_zero_ne_zero, _ = fncptr_from_rpy_func(int_cmp_zero_ne_zero, [], rffi.LONGLONG)
mu_int_cmp_zero_eq_one , _ = fncptr_from_rpy_func(int_cmp_zero_eq_one , [], rffi.LONGLONG)
mu_int_cmp_zero_ne_one , _ = fncptr_from_rpy_func(int_cmp_zero_ne_one , [], rffi.LONGLONG)
assert mu_int_cmp_zero_eq_zero() == 1
assert mu_int_cmp_zero_ne_zero() == 1
assert mu_int_cmp_zero_eq_one () == 1
assert mu_int_cmp_zero_ne_one () == 1
@may_spawn_proc
def test_rpython_int_gt_value():
def int_gt_value(a, b):
ret = a > b
return ret
mu_int_gt_value, _ = fncptr_from_rpy_func(int_gt_value, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)
assert mu_int_gt_value(1, 0) == 1
assert mu_int_gt_value(0, 1) == 0
assert mu_int_gt_value(1, 1) == 0
assert mu_int_gt_value(1, -1) == 1
assert mu_int_gt_value(-1, 1) == 0
assert mu_int_gt_value(-1, -1) == 0
assert mu_int_gt_value(9223372036854775807, -9223372036854775808) == 1
assert mu_int_gt_value(-9223372036854775808, 9223372036854775807) == 0
assert mu_int_gt_value(18446744073709551615, 9223372036854775807) == 0
assert mu_int_gt_value(18446744073709551615, -9223372036854775808) == 1
assert mu_int_gt_value(18446744073709551615, -1) == 0
@may_spawn_proc
def test_rpython_int_ge_value():
def int_ge_value(a, b):
ret = a >= b
return ret
mu_int_ge_value, _ = fncptr_from_rpy_func(int_ge_value, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)
assert mu_int_ge_value(1, 0) == 1
assert mu_int_ge_value(0, 1) == 0
assert mu_int_ge_value(1, 1) == 1
assert mu_int_ge_value(1, -1) == 1
assert mu_int_ge_value(-1, 1) == 0
assert mu_int_ge_value(-1, -1) == 1
assert mu_int_ge_value(9223372036854775807, -9223372036854775808) == 1
assert mu_int_ge_value(-9223372036854775808, 9223372036854775807) == 0
assert mu_int_ge_value(18446744073709551615, 9223372036854775807) == 0
assert mu_int_ge_value(18446744073709551615, -9223372036854775808) == 1
assert mu_int_ge_value(18446744073709551615, -1) == 1
@may_spawn_proc
def test_rpython_int_lt_value():
def int_lt_value(a, b):
ret = a < b
return ret
mu_int_lt_value, _ = fncptr_from_rpy_func(int_lt_value, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)
assert mu_int_lt_value(1, 0) == 0
assert mu_int_lt_value(0, 1) == 1
assert mu_int_lt_value(1, 1) == 0
assert mu_int_lt_value(1, -1) == 0
assert mu_int_lt_value(-1, 1) == 1
assert mu_int_lt_value(-1, -1) == 0
assert mu_int_lt_value(9223372036854775807, -9223372036854775808) == 0
assert mu_int_lt_value(-9223372036854775808, 9223372036854775807) == 1
assert mu_int_lt_value(18446744073709551615, 9223372036854775807) == 1
assert mu_int_lt_value(18446744073709551615, -9223372036854775808) == 0
assert mu_int_lt_value(18446744073709551615, -1) == 0
@may_spawn_proc
def test_rpython_int_le_value():
def int_le_value(a, b):
ret = a <= b
return ret
mu_int_le_value, _ = fncptr_from_rpy_func(int_le_value, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)
assert mu_int_le_value(1, 0) == 0
assert mu_int_le_value(0, 1) == 1
assert mu_int_le_value(1, 1) == 1
assert mu_int_le_value(1, -1) == 0
assert mu_int_le_value(-1, 1) == 1
assert mu_int_le_value(-1, -1) == 1
assert mu_int_le_value(9223372036854775807, -9223372036854775808) == 0
assert mu_int_le_value(-9223372036854775808, 9223372036854775807) == 1
assert mu_int_le_value(18446744073709551615, 9223372036854775807) == 1
assert mu_int_le_value(18446744073709551615, -9223372036854775808) == 0
assert mu_int_le_value(18446744073709551615, -1) == 1
@may_spawn_proc
def test_rpython_int_eq_value():
def int_eq_value(a, b):
ret = a == b
return ret
mu_int_eq_value, _ = fncptr_from_rpy_func(int_eq_value, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)
assert mu_int_eq_value(1, 0) == 0
assert mu_int_eq_value(0, 1) == 0
assert mu_int_eq_value(1, 1) == 1
assert mu_int_eq_value(1, -1) == 0
assert mu_int_eq_value(-1, 1) == 0
assert mu_int_eq_value(-1, -1) == 1
assert mu_int_eq_value(9223372036854775807, -9223372036854775808) == 0
assert mu_int_eq_value(-9223372036854775808, 9223372036854775807) == 0
assert mu_int_eq_value(18446744073709551615, 9223372036854775807) == 0
assert mu_int_eq_value(18446744073709551615, -9223372036854775808) == 0
assert mu_int_eq_value(18446744073709551615, -1) == 1
@may_spawn_proc
def test_rpython_int_ne_value():
def int_ne_value(a, b):
ret = a != b
return ret
mu_int_ne_value, _ = fncptr_from_rpy_func(int_ne_value, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)
assert mu_int_ne_value(1, 0) == 1
assert mu_int_ne_value(0, 1) == 1
assert mu_int_ne_value(1, 1) == 0
assert mu_int_ne_value(1, -1) == 1
assert mu_int_ne_value(-1, 1) == 1
assert mu_int_ne_value(-1, -1) == 0
assert mu_int_ne_value(9223372036854775807, -9223372036854775808) == 1
assert mu_int_ne_value(-9223372036854775808, 9223372036854775807) == 1
assert mu_int_ne_value(18446744073709551615, 9223372036854775807) == 1
assert mu_int_ne_value(18446744073709551615, -9223372036854775808) == 1
assert mu_int_ne_value(18446744073709551615, -1) == 0
\ No newline at end of file
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib import rmu_fast as rmu
from rpython.translator.platform import platform
from util import fncptr_from_rpy_func, fncptr_from_py_script, may_spawn_proc
import ctypes, py, stat
import pytest
from test_rpython import run_boot_image
c_exit = rffi.llexternal('exit', [rffi.INT], lltype.Void, _nowrapper=True)
@may_spawn_proc
def test_rpython_list_new_empty():
def main(argv):
a = []
c_exit(rffi.cast(rffi.INT, len(a)))
return 0
res = run_boot_image(main, '/tmp/test_rpython_list_new_empty')
assert res.returncode == 0, res.err
@may_spawn_proc
def test_rpython_list_new_5():
def main(argv):
a = [1, 2, 3, 4, 5]
c_exit(rffi.cast(rffi.INT, len(a)))
return 0
res = run_boot_image(main, '/tmp/test_rpython_list_new_5')
assert res.returncode == 5, res.err
@may_spawn_proc
def test_rpython_list_append():
def main(argv):
a = []
for i in range(0, 10):
a.append(i)
c_exit(rffi.cast(rffi.INT, len(a)))
return 0
res = run_boot_image(main, '/tmp/test_rpython_list_append')
assert res.returncode == 10, res.err
@may_spawn_proc
def test_rpython_list_iter():
def main(argv):
a = []
for i in range(0, 10):
a.append(i)
sum = 0
for n in a:
sum += n
c_exit(rffi.cast(rffi.INT, sum))
return 0
res = run_boot_image(main, '/tmp/test_rpython_list_iter')
assert res.returncode == 45, res.err
@pytest.mark.xfail(reason = "mem[1] is not 10? but mem[0] is 10, need to look into this")
@may_spawn_proc
def test_rpython_list_check_by_addr():
Int64Ptr = lltype.Ptr(lltype.Array(rffi.LONGLONG))
def check(a, b):
if a != b:
c_exit(rffi.cast(rffi.INT, 23))
def main(argv):
a = []
for i in range(0, 10):
a.append(i)
from rpython.rtyper.lltypesystem.llmemory import cast_ptr_to_adr
from rpython.rlib.objectmodel import keepalive_until_here
addr = cast_ptr_to_adr(a)
mem = rffi.cast(Int64Ptr, addr)
# ignore mem[0]
check(mem[1], 10)
keepalive_until_here(argv)
return 0
res = run_boot_image(main, '/tmp/test_rpython_list_check_by_addr')
assert res.returncode == 0, 'returncode = %d\n%s' % (res.returncode, res.err)
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