diff --git a/src/sbpir/mod.rs b/src/sbpir/mod.rs index 22fdc227..b5a77029 100644 --- a/src/sbpir/mod.rs +++ b/src/sbpir/mod.rs @@ -119,6 +119,19 @@ impl SBPIR { pub fn expose(&mut self, signal: Queriable, offset: ExposeOffset) { match signal { Queriable::Forward(..) | Queriable::Shared(..) => { + let existing_forward_signal = self + .forward_signals + .clone() + .iter() + .any(|s| s.uuid() == signal.uuid()); + let existing_shared_signal = self + .shared_signals + .clone() + .iter() + .any(|s| s.uuid() == signal.uuid()); + if !existing_forward_signal && !existing_shared_signal { + panic!("Signal not found in forward signals."); + } self.exposed.push((signal, offset)); } _ => panic!("Can only expose forward and shared signals."), @@ -530,4 +543,37 @@ mod tests { let circuit: SBPIR = SBPIR::default(); assert!(circuit.q_enable); } + + #[test] + #[should_panic] + fn test_expose_non_existing_signal() { + let mut circuit: SBPIR = SBPIR::default(); + let signal = Queriable::Forward( + ForwardSignal::new_with_phase(0, "signal".to_string()), + false, + ); + let offset = ExposeOffset::First; + + circuit.expose(signal, offset); + } + + #[test] + fn test_expose_forward_signal() { + let mut circuit: SBPIR = SBPIR::default(); + let signal = circuit.add_forward("signal", 0); + let offset = ExposeOffset::Last; + assert_eq!(circuit.exposed.len(), 0); + circuit.expose(Queriable::Forward(signal, false), offset); + assert_eq!(circuit.exposed.len(), 1); + } + + #[test] + fn test_expose_shared_signal() { + let mut circuit: SBPIR = SBPIR::default(); + let signal = circuit.add_shared("signal", 0); + let offset = ExposeOffset::Last; + assert_eq!(circuit.exposed.len(), 0); + circuit.expose(Queriable::Shared(signal, 10), offset); + assert_eq!(circuit.exposed.len(), 1); + } }