Make more tests quickcheck-compatible, remove unused code
This commit is contained in:
@@ -322,9 +322,7 @@ pub fn current_block_state(ctx: &Context) -> BlockState {
|
||||
ctx.block_state.clone()
|
||||
}
|
||||
|
||||
pub fn return_from_block(ctx: &mut Context, new_depth: StackDepth) {
|
||||
let diff = ((ctx.block_state.depth.0 - new_depth.0) * WORD_SIZE) as i32;
|
||||
|
||||
pub fn return_from_block(ctx: &mut Context) {
|
||||
if let Some(loc) = ctx.block_state.stack.last().unwrap().location(&ctx.locals) {
|
||||
match loc {
|
||||
ValueLocation::Reg(r) => {
|
||||
|
||||
@@ -148,7 +148,7 @@ pub fn translate(
|
||||
..
|
||||
}) => {
|
||||
if ty != Type::EmptyBlockType {
|
||||
return_from_block(&mut ctx, block_state.depth);
|
||||
return_from_block(&mut ctx);
|
||||
}
|
||||
|
||||
// Finalize if..else block by jumping to the `end_label`.
|
||||
@@ -179,7 +179,7 @@ pub fn translate(
|
||||
let control_frame = control_frames.pop().expect("control stack is never empty");
|
||||
|
||||
if control_frame.ty != Type::EmptyBlockType && !control_frames.is_empty() {
|
||||
return_from_block(&mut ctx, control_frame.block_state.depth);
|
||||
return_from_block(&mut ctx);
|
||||
}
|
||||
|
||||
if !control_frame.kind.is_loop() {
|
||||
|
||||
55
src/tests.rs
55
src/tests.rs
@@ -19,7 +19,7 @@ fn empty() {
|
||||
}
|
||||
|
||||
macro_rules! binop_test {
|
||||
($op:ident, $func:path) => {
|
||||
($op:ident, $func:expr) => {
|
||||
quickcheck! {
|
||||
fn $op(a: u32, b: u32) -> bool {
|
||||
static CODE: &str = concat!(
|
||||
@@ -45,40 +45,27 @@ binop_test!(or, std::ops::BitOr::bitor);
|
||||
binop_test!(xor, std::ops::BitXor::bitxor);
|
||||
binop_test!(mul, u32::wrapping_mul);
|
||||
|
||||
#[test]
|
||||
fn relop_eq() {
|
||||
const CASES: &[(u32, u32, u32)] = &[
|
||||
(0, 0, 1),
|
||||
(0, 1, 0),
|
||||
(1, 0, 0),
|
||||
(1, 1, 1),
|
||||
(1312, 1, 0),
|
||||
(1312, 1312, 1),
|
||||
];
|
||||
|
||||
let code = r#"
|
||||
(module
|
||||
quickcheck! {
|
||||
fn relop_eq(a: u32, b: u32) -> bool{
|
||||
static CODE: &str = r#"
|
||||
(module
|
||||
(func (param i32) (param i32) (result i32) (i32.eq (get_local 0) (get_local 1)))
|
||||
)
|
||||
)
|
||||
"#;
|
||||
|
||||
for (a, b, expected) in CASES {
|
||||
assert_eq!(execute_wat(code, *a, *b), *expected);
|
||||
lazy_static! {
|
||||
static ref TRANSLATED: TranslatedModule = translate_wat(CODE);
|
||||
}
|
||||
|
||||
let out = unsafe { TRANSLATED.execute_func::<(u32, u32), u32>(0, (a, b)) };
|
||||
|
||||
(a == b) == (out == 1)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_then_else() {
|
||||
const CASES: &[(u32, u32, u32)] = &[
|
||||
(0, 1, 1),
|
||||
(0, 0, 0),
|
||||
(1, 0, 0),
|
||||
(1, 1, 1),
|
||||
(1312, 1, 1),
|
||||
(1312, 1312, 1312),
|
||||
];
|
||||
|
||||
let code = r#"
|
||||
quickcheck! {
|
||||
fn if_then_else(a: u32, b: u32) -> bool {
|
||||
const CODE: &str = r#"
|
||||
(module
|
||||
(func (param i32) (param i32) (result i32)
|
||||
(if (result i32)
|
||||
@@ -93,11 +80,15 @@ fn if_then_else() {
|
||||
)
|
||||
"#;
|
||||
|
||||
for (a, b, expected) in CASES {
|
||||
assert_eq!(execute_wat(code, *a, *b), *expected, "{}, {}", a, b);
|
||||
lazy_static! {
|
||||
static ref TRANSLATED: TranslatedModule = translate_wat(CODE);
|
||||
}
|
||||
|
||||
let out = unsafe { TRANSLATED.execute_func::<(u32, u32), u32>(0, (a, b)) };
|
||||
|
||||
out == (if a == b { a } else { b })
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_without_result() {
|
||||
let code = r#"
|
||||
|
||||
Reference in New Issue
Block a user