Skip to content

Commit

Permalink
test: use insta snapshots to add dot_string coverage (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 authored Nov 13, 2023
1 parent 40dfcf3 commit d32d033
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "--cfg=ci_run"
MIRIFLAGS: '-Zmiri-permissive-provenance' # Required due to warnings in bitvec 1.0.1
CI: true # insta snapshots behave differently on ci

jobs:
check:
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ webbrowser = "0.8.10"
urlencoding = "2.1.2"
cool_asserts = "2.0.3"
paste = "1.0"
insta = { version = "1.34.0", features = ["yaml"] }

[[bench]]
name = "bench_main"
harness = false


[profile.dev.package]
insta.opt-level = 3
similar.opt-level = 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/hugr/views/tests.rs
expression: h.dot_string()
---
"digraph {\n0 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"1\">(0) DFG</td></tr></table>>]\n1 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"2\">(1) Input</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: qubit([])</td><td port=\"out1\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >1: qubit([])</td></tr></table>>]\n1:out0 -> 3:in0 [style=\"\"]\n1:out1 -> 3:in1 [style=\"\"]\n2 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: qubit([])</td><td port=\"in1\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >1: qubit([])</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"2\">(2) Output</td></tr></table>>]\n3 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"3\" cellpadding=\"1\" >0: qubit([])</td><td port=\"in1\" align=\"text\" colspan=\"3\" cellpadding=\"1\" >1: qubit([])</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"6\">(3) test.quantum.CX</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"2\" cellpadding=\"1\" >0: qubit([])</td><td port=\"out1\" align=\"text\" colspan=\"2\" cellpadding=\"1\" >1: qubit([])</td><td port=\"out2\" align=\"text\" colspan=\"2\" cellpadding=\"1\" border=\"0\"></td></tr></table>>]\n3:out0 -> 4:in1 [style=\"\"]\n3:out1 -> 4:in0 [style=\"\"]\n3:out2 -> 4:in2 [style=\"dotted\"]\n4 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"2\" cellpadding=\"1\" >0: qubit([])</td><td port=\"in1\" align=\"text\" colspan=\"2\" cellpadding=\"1\" >1: qubit([])</td><td port=\"in2\" align=\"text\" colspan=\"2\" cellpadding=\"1\" border=\"0\"></td></tr><tr><td align=\"text\" border=\"0\" colspan=\"6\">(4) test.quantum.CX</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"3\" cellpadding=\"1\" >0: qubit([])</td><td port=\"out1\" align=\"text\" colspan=\"3\" cellpadding=\"1\" >1: qubit([])</td></tr></table>>]\n4:out0 -> 2:in0 [style=\"\"]\n4:out1 -> 2:in1 [style=\"\"]\nhier0 [shape=plain label=\"0\"]\nhier0 -> hier1 [style = \"dashed\"] \nhier0 -> hier2 [style = \"dashed\"] \nhier0 -> hier3 [style = \"dashed\"] \nhier0 -> hier4 [style = \"dashed\"] \nhier1 [shape=plain label=\"1\"]\nhier2 [shape=plain label=\"2\"]\nhier3 [shape=plain label=\"3\"]\nhier4 [shape=plain label=\"4\"]\n}\n"

43 changes: 34 additions & 9 deletions src/hugr/views/tests.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
use portgraph::PortOffset;
use rstest::{fixture, rstest};

use crate::{
builder::{BuildError, DFGBuilder, Dataflow, DataflowHugr},
builder::{BuildError, BuildHandle, Container, DFGBuilder, Dataflow, DataflowHugr},
extension::prelude::QB_T,
ops::handle::NodeHandle,
ops::handle::{DataflowOpID, NodeHandle},
type_row,
types::FunctionType,
utils::test_quantum_extension::cx_gate,
HugrView,
Hugr, HugrView,
};

#[test]
fn node_connections() -> Result<(), BuildError> {
#[fixture]
fn sample_hugr() -> (Hugr, BuildHandle<DataflowOpID>, BuildHandle<DataflowOpID>) {
let mut dfg = DFGBuilder::new(FunctionType::new(
type_row![QB_T, QB_T],
type_row![QB_T, QB_T],
))?;
))
.unwrap();

let [q1, q2] = dfg.input_wires_arr();

let n1 = dfg.add_dataflow_op(cx_gate(), [q1, q2])?;
let n1 = dfg.add_dataflow_op(cx_gate(), [q1, q2]).unwrap();
let [q1, q2] = n1.outputs_arr();
let n2 = dfg.add_dataflow_op(cx_gate(), [q2, q1])?;
let n2 = dfg.add_dataflow_op(cx_gate(), [q2, q1]).unwrap();
dfg.add_other_wire(n1.node(), n2.node()).unwrap();

let h = dfg.finish_prelude_hugr_with_outputs(n2.outputs())?;
(
dfg.finish_prelude_hugr_with_outputs(n2.outputs()).unwrap(),
n1,
n2,
)
}

#[rstest]
fn node_connections(
sample_hugr: (Hugr, BuildHandle<DataflowOpID>, BuildHandle<DataflowOpID>),
) -> Result<(), BuildError> {
let (h, n1, n2) = sample_hugr;
let connections: Vec<_> = h.node_connections(n1.node(), n2.node()).collect();

assert_eq!(
Expand All @@ -38,7 +51,19 @@ fn node_connections() -> Result<(), BuildError> {
PortOffset::new_outgoing(1).into(),
PortOffset::new_incoming(0).into()
],
// order edge
[
PortOffset::new_outgoing(2).into(),
PortOffset::new_incoming(2).into()
],
]
);
Ok(())
}

#[rstest]
fn dot_string(sample_hugr: (Hugr, BuildHandle<DataflowOpID>, BuildHandle<DataflowOpID>)) {
let (h, _, _) = sample_hugr;

insta::assert_yaml_snapshot!(h.dot_string());
}

0 comments on commit d32d033

Please sign in to comment.