Skip to content

Commit

Permalink
Add test for NonEmpty block pass
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Oct 13, 2024
1 parent 77f3fda commit 52acbf8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/verifier/src/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub trait VerificationPass {
fn run(&mut self, ctx: &mut VerificationCtx) -> VerificationResult;
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum VerificationResult {
Pass,
Fail,
Expand Down
79 changes: 79 additions & 0 deletions crates/verifier/src/passes/block/non_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,82 @@ impl VerificationPass for NonEmpty {
VerificationResult::Pass
}
}

#[cfg(test)]
mod tests {
use std::fmt::Write;

use sonatina_ir::{
builder::test_util::test_func_builder,
inst::control_flow::{Jump, Return},
isa::Isa,
Type,
};

use super::*;

#[test]
fn non_empty_block() {
let (evm, mut builder) = test_func_builder(&[], Type::Unit);
let is = evm.inst_set();

let b0 = builder.append_block();
let _b1 = builder.append_block(); // empty
let b2 = builder.append_block();
let b3 = builder.append_block();
let _b4 = builder.append_block(); // empty
let _b5 = builder.append_block(); // empty
let b6 = builder.append_block();

builder.switch_to_block(b0);
builder.insert_inst_no_result_with(|| Jump::new(is, b2));

builder.switch_to_block(b2);
builder.insert_inst_no_result_with(|| Jump::new(is, b3));

builder.switch_to_block(b3);
builder.insert_inst_no_result_with(|| Jump::new(is, b6));

builder.switch_to_block(b6);
builder.insert_inst_no_result_with(|| Return::new(is, None));

builder.seal_all();

let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &module.funcs[func_ref];

let mut ctx = VerificationCtx::new(func_ref, func);
let res = NonEmpty.run(&mut ctx);
assert_eq!(res, VerificationResult::Fail);

let mut err_msgs = String::new();

let errs = ctx
.error_stack
.into_errs_iter(func, func_ref)
.into_iter()
.collect::<Vec<_>>();

for e in errs {
write!(&mut err_msgs, "{}\n", e).unwrap();
}

assert_eq!(
"empty block, block1
trace_info:
0: block1
1: func public %test_func() -> unit
empty block, block4
trace_info:
0: block4
1: func public %test_func() -> unit
empty block, block5
trace_info:
0: block5
1: func public %test_func() -> unit
",
err_msgs
);
}
}

0 comments on commit 52acbf8

Please sign in to comment.