Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Sep 12, 2023
1 parent aa88f75 commit 487c795
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,13 @@ where
Root: NodeHandle,
{
fn new(hugr: &'a impl HugrView, root: Node) -> Result<Self, HugrError> {
println!("ALAN checking node {:?} against {}", root, Root::TAG);
hugr.valid_node(root)?;
if !Root::TAG.is_superset(hugr.get_optype(root).tag()) {
println!(
"ALAN check failed, optype was {}",
hugr.get_optype(root).tag()
);
return Err(HugrError::InvalidNode(root));
}
let hugr = hugr.base_hugr();
Expand Down Expand Up @@ -217,6 +222,13 @@ where

#[cfg(test)]
mod test {
use crate::builder::{Container, Dataflow, DataflowSubContainer, HugrBuilder, ModuleBuilder};
use crate::extension::PRELUDE_REGISTRY;
use crate::ops::handle::{DfgID, FuncID, ModuleRootID};
use crate::ops::{dataflow::IOTrait, Input, Output};
use crate::type_row;
use crate::types::{FunctionType, Type};

use super::super::descendants::test::make_module_hgr;
use super::*;

Expand All @@ -234,4 +246,45 @@ mod test {

Ok(())
}

const NAT: Type = crate::extension::prelude::USIZE_T;
#[test]
fn nested_flat() -> Result<(), Box<dyn std::error::Error>> {
let mut module_builder = ModuleBuilder::new();
let fty = FunctionType::new(type_row![NAT], type_row![NAT]);
let mut fbuild = module_builder.define_function("main", fty.clone().pure())?;
let dfg = fbuild.dfg_builder(fty, None, fbuild.input_wires())?;
let ins = dfg.input_wires();
let sub_dfg = dfg.finish_with_outputs(ins)?;
let fun = fbuild.finish_with_outputs(sub_dfg.outputs())?;
let h = module_builder.finish_hugr(&PRELUDE_REGISTRY)?;
let sub_dfg = sub_dfg.node();
// Can create a view from a child or grandchild of a hugr:
let dfg_view: SiblingGraph<'_, DfgID> = SiblingGraph::new(&h, sub_dfg)?;
let fun_view: SiblingGraph<'_, FuncID<true>> = SiblingGraph::new(&h, fun.node())?;
assert_eq!(fun_view.children(sub_dfg).len(), 0);
// And can create a view from a child of another SiblingGraph
let nested_dfg_view: SiblingGraph<'_, DfgID> = SiblingGraph::new(&fun_view, sub_dfg)?;

// Both ways work:
let just_io = vec![
Input::new(type_row![NAT]).into(),
Output::new(type_row![NAT]).into(),
];
for d in [dfg_view, nested_dfg_view] {
assert_eq!(
d.children(sub_dfg).map(|n| d.get_optype(n)).collect_vec(),
just_io.iter().collect_vec()
);
}

// But cannot create a view directly as a grandchild of another SiblingGraph
let root_view: SiblingGraph<'_, ModuleRootID> = SiblingGraph::new(&h, h.root()).unwrap();
assert_eq!(
SiblingGraph::<'_, DfgID>::new(&root_view, sub_dfg.node()).err(),
Some(HugrError::InvalidNode(sub_dfg.node()))
);

Ok(())
}
}

0 comments on commit 487c795

Please sign in to comment.