Skip to content

Commit

Permalink
add test for non-dominating edge validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Nov 6, 2023
1 parent fc3ffdf commit e9a864b
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ mod test {
use crate::builder::build_traits::HugrBuilder;
use crate::builder::{DataflowSubContainer, ModuleBuilder};

use crate::hugr::validate::InterGraphEdgeError;
use crate::hugr::ValidationError;
use crate::{builder::test::NAT, type_row};
use cool_asserts::assert_matches;

Expand Down Expand Up @@ -408,17 +410,52 @@ mod test {

entry_b.finish_with_outputs(sum, [])?
};
let exit = cfg_builder.exit_block();
let mut middle_b =
cfg_builder.simple_block_builder(FunctionType::new(type_row![], type_row![NAT]), 1)?;
let middle = {
let c = middle_b.load_const(&sum_tuple_const)?;
middle_b.finish_with_outputs(c, [inw])?
};
let exit = cfg_builder.exit_block();
cfg_builder.branch(&entry, 0, &middle)?;
cfg_builder.branch(&middle, 0, &exit)?;
assert_matches!(cfg_builder.finish_prelude_hugr(), Ok(_));

Ok(())
}

#[test]
fn test_non_dom_edge() -> Result<(), BuildError> {
let mut cfg_builder = CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT]))?;
let sum_tuple_const =
cfg_builder.add_constant(ops::Const::unary_unit_sum(), ExtensionSet::new())?;
let sum_variants = vec![type_row![]];
let mut middle_b = cfg_builder
.simple_block_builder(FunctionType::new(type_row![NAT], type_row![NAT]), 1)?;
let [inw] = middle_b.input_wires_arr();
let middle = {
let c = middle_b.load_const(&sum_tuple_const)?;
middle_b.finish_with_outputs(c, [inw])?
};

let mut entry_b =
cfg_builder.entry_builder(sum_variants.clone(), type_row![NAT], ExtensionSet::new())?;
let entry = {
let sum = entry_b.load_const(&sum_tuple_const)?;
// entry block uses wire from middle block even though middle block
// does not dominate entry
entry_b.finish_with_outputs(sum, [inw])?
};
let exit = cfg_builder.exit_block();
cfg_builder.branch(&entry, 0, &middle)?;
cfg_builder.branch(&middle, 0, &exit)?;
assert_matches!(
cfg_builder.finish_prelude_hugr(),
Err(ValidationError::InterGraphEdgeError(
InterGraphEdgeError::NonDominatedAncestor { .. }
))
);

Ok(())
}
}

0 comments on commit e9a864b

Please sign in to comment.