Skip to content

Commit

Permalink
tests: Add test of repeated adding of the same extension
Browse files Browse the repository at this point in the history
  • Loading branch information
croyzor committed Sep 18, 2023
1 parent e2213ba commit 87d9345
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion src/extension/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,14 +670,15 @@ mod test {
use super::*;
use crate::builder::test::closed_dfg_root_hugr;
use crate::builder::{BuildError, DFGBuilder, Dataflow, DataflowHugr};
use crate::extension::{ExtensionSet, EMPTY_REG};
use crate::extension::{ExtensionSet, EMPTY_REG, PRELUDE_REGISTRY};
use crate::hugr::{validate::ValidationError, Hugr, HugrMut, HugrView, NodeType};
use crate::macros::const_extension_ids;
use crate::ops::{self, dataflow::IOTrait, handle::NodeHandle, OpTrait};
use crate::type_row;
use crate::types::{FunctionType, Type};

use cool_asserts::assert_matches;
use itertools::Itertools;
use portgraph::NodeIndex;

const NAT: Type = crate::extension::prelude::USIZE_T;
Expand Down Expand Up @@ -1091,4 +1092,76 @@ mod test {
}
Ok(())
}

#[test]
fn extension_adding_sequence() -> Result<(), Box<dyn Error>> {
fn mknode(
hugr: &mut Hugr,
root: Node,
sig: FunctionType,
ext: ExtensionId,
) -> Result<Node, Box<dyn Error>> {
let [node, input, output] = create_with_io(
hugr,
root,
ops::DFG {
signature: sig
.clone()
.with_extension_delta(&ExtensionSet::singleton(&ext)),
},
)?;

let lift = hugr.add_node_with_parent(
node,
NodeType::open_extensions(ops::LeafOp::Lift {
type_row: type_row![NAT],
new_extension: ext,
}),
)?;

hugr.connect(input, 0, lift, 0)?;
hugr.connect(lift, 0, output, 0)?;

Ok(node)
}

let df_sig = FunctionType::new(type_row![NAT], type_row![NAT]);

let mut hugr = Hugr::new(NodeType::open_extensions(ops::DFG {
signature: df_sig
.clone()
.with_extension_delta(&ExtensionSet::from_iter([A, B])),
}));

let root = hugr.root();
let input = hugr.add_node_with_parent(
root,
NodeType::open_extensions(ops::Input {
types: type_row![NAT],
}),
)?;
let output = hugr.add_node_with_parent(
root,
NodeType::open_extensions(ops::Output {
types: type_row![NAT],
}),
)?;

let node0 = mknode(&mut hugr, root, df_sig.clone(), A)?;
let node1 = mknode(&mut hugr, root, df_sig.clone(), A)?;
let node2 = mknode(&mut hugr, root, df_sig.clone(), B)?;
let node3 = mknode(&mut hugr, root, df_sig.clone(), B)?;
let node4 = mknode(&mut hugr, root, df_sig.clone(), A)?;
let node5 = mknode(&mut hugr, root, df_sig.clone(), B)?;

// Connect nodes in order (0 -> 1 -> 2 ...)
let nodes = [input, node0, node1, node2, node3, node4, node5, output];
for (src, tgt) in nodes.into_iter().tuple_windows() {
hugr.connect(src, 0, tgt, 0)?;
}

hugr.infer_and_validate(&PRELUDE_REGISTRY)?;

Ok(())
}
}

0 comments on commit 87d9345

Please sign in to comment.