Skip to content

Commit 61f1ea2

Browse files
committed
improve coverage
1 parent 14f969a commit 61f1ea2

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

hugr-core/src/hugr/patch/port_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub enum BoundaryPort<HostNode, P> {
2323
pub struct HostPort<N, P>(pub N, pub P);
2424

2525
/// A port in the replacement graph.
26-
#[derive(Debug, Clone, Copy, From)]
26+
#[derive(Debug, Clone, Copy, From, PartialEq, Eq, PartialOrd, Ord)]
2727
pub struct ReplacementPort<P>(pub Node, pub P);
2828

2929
impl<HostNode: Copy, P> BoundaryPort<HostNode, P> {

hugr-core/src/hugr/patch/simple_replace.rs

+66-2
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ pub(in crate::hugr::patch) mod test {
573573
DataflowSubContainer, HugrBuilder, ModuleBuilder,
574574
};
575575
use crate::extension::prelude::{bool_t, qb_t};
576-
use crate::hugr::patch::PatchVerification;
576+
use crate::hugr::patch::simple_replace::OutputBoundaryMap;
577+
use crate::hugr::patch::{PatchVerification, ReplacementPort};
577578
use crate::hugr::views::{HugrView, SiblingSubgraph};
578579
use crate::hugr::{Hugr, HugrMut, Patch};
579580
use crate::ops::dataflow::DataflowOpTrait;
@@ -584,7 +585,7 @@ pub(in crate::hugr::patch) mod test {
584585
use crate::std_extensions::logic::LogicOp;
585586
use crate::types::{Signature, Type};
586587
use crate::utils::test_quantum_extension::{cx_gate, h_gate};
587-
use crate::{IncomingPort, Node, OutgoingPort};
588+
use crate::{Direction, IncomingPort, Node, OutgoingPort, Port};
588589

589590
use super::SimpleReplacement;
590591

@@ -801,6 +802,17 @@ pub(in crate::hugr::patch) mod test {
801802
nu_inp,
802803
nu_out: nu_out.into(),
803804
};
805+
806+
// Check output boundary
807+
assert_eq!(
808+
r.map_host_output((h_outp_node, h_port_2)).unwrap(),
809+
ReplacementPort::from((r.get_replacement_io().unwrap()[1], n_port_2))
810+
);
811+
assert!(r
812+
.map_host_output((h_outp_node, OutgoingPort::from(0)))
813+
.is_none());
814+
815+
// Check invalidation set
804816
assert_eq!(
805817
HashSet::<_>::from_iter(r.invalidation_set()),
806818
HashSet::<_>::from_iter([h_node_cx, h_node_h0, h_node_h1, h_outp_node]),
@@ -1209,6 +1221,58 @@ pub(in crate::hugr::patch) mod test {
12091221
assert_eq!(h.validate(), Ok(()));
12101222
}
12111223

1224+
#[rstest]
1225+
fn test_output_boundary_map(dfg_hugr2: Hugr) {
1226+
let [inp, out] = dfg_hugr2.get_io(dfg_hugr2.root()).unwrap();
1227+
let map = [
1228+
((inp, OutgoingPort::from(0)), IncomingPort::from(0)),
1229+
((inp, OutgoingPort::from(1)), IncomingPort::from(1)),
1230+
]
1231+
.into_iter()
1232+
.collect();
1233+
let map = OutputBoundaryMap::ByOutgoing(map);
1234+
1235+
// Basic check: map as just defined
1236+
assert_eq!(
1237+
map.get(inp, OutgoingPort::from(0)),
1238+
Some(IncomingPort::from(0))
1239+
);
1240+
assert_eq!(
1241+
map.get(inp, OutgoingPort::from(1)),
1242+
Some(IncomingPort::from(1))
1243+
);
1244+
1245+
// Now check the map in terms of incoming ports
1246+
assert!(map.get(out, IncomingPort::from(0)).is_none());
1247+
assert_eq!(
1248+
map.get_as_incoming(out, IncomingPort::from(0), &dfg_hugr2),
1249+
Some(IncomingPort::from(0))
1250+
);
1251+
1252+
// Finally, check iterators
1253+
assert_eq!(
1254+
map.iter().collect::<HashSet<_>>(),
1255+
HashSet::from_iter([
1256+
(
1257+
(inp, Port::new(Direction::Outgoing, 0)),
1258+
IncomingPort::from(0)
1259+
),
1260+
(
1261+
(inp, Port::new(Direction::Outgoing, 1)),
1262+
IncomingPort::from(1)
1263+
),
1264+
])
1265+
);
1266+
let h_gate = dfg_hugr2.output_neighbours(inp).nth(1).unwrap();
1267+
assert_eq!(
1268+
map.iter_as_incoming(&dfg_hugr2).collect::<HashSet<_>>(),
1269+
HashSet::from_iter([
1270+
((out, IncomingPort::from(0)), IncomingPort::from(0)),
1271+
((h_gate, IncomingPort::from(0)), IncomingPort::from(1)),
1272+
])
1273+
);
1274+
}
1275+
12121276
use crate::hugr::patch::replace::Replacement;
12131277
fn to_replace(h: &impl HugrView<Node = Node>, s: SimpleReplacement) -> Replacement {
12141278
use crate::hugr::patch::replace::{NewEdgeKind, NewEdgeSpec};

0 commit comments

Comments
 (0)