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