Skip to content

Commit 21223a0

Browse files
authored
Add a clean test (#20)
1 parent 73c4e46 commit 21223a0

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/netlist.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,12 +1450,17 @@ where
14501450
}
14511451

14521452
/// Greedly removes unused nodes from the netlist, until it stops changing.
1453-
pub fn clean(&self) -> Result<(), String> {
1454-
let mut changed = true;
1455-
while changed {
1456-
changed = self.clean_once()?;
1453+
/// Returns true if the netlist was changed.
1454+
pub fn clean(&self) -> Result<bool, String> {
1455+
if !self.clean_once()? {
1456+
Ok(false)
1457+
} else {
1458+
let mut changed = true;
1459+
while changed {
1460+
changed = self.clean_once()?;
1461+
}
1462+
Ok(true)
14571463
}
1458-
Ok(())
14591464
}
14601465

14611466
/// Returns `true` if all the nets are uniquely named

tests/edits.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use safety_net::netlist::Gate;
2+
use safety_net::netlist::GateNetlist;
3+
use safety_net::netlist::Netlist;
4+
use std::rc::Rc;
5+
6+
fn and_gate() -> Gate {
7+
Gate::new_logical("AND".into(), vec!["A".into(), "B".into()], "Y".into())
8+
}
9+
10+
fn get_simple_example() -> Rc<GateNetlist> {
11+
let netlist = Netlist::new("example".to_string());
12+
13+
let a = netlist.insert_input("a".into());
14+
let b = netlist.insert_input("b".into());
15+
16+
let instance = netlist
17+
.insert_gate(and_gate(), "inst_0".into(), &[a, b])
18+
.unwrap();
19+
20+
instance.expose_with_name("y".into());
21+
22+
netlist
23+
}
24+
25+
#[test]
26+
fn test_clean() {
27+
let netlist = get_simple_example();
28+
assert!(netlist.verify().is_ok());
29+
assert!(!netlist.clean().unwrap());
30+
let inputs: Vec<_> = netlist.inputs().collect();
31+
assert_eq!(inputs.len(), 2);
32+
let _new_cell = netlist
33+
.insert_gate(and_gate(), "inst_1".into(), &inputs)
34+
.unwrap();
35+
assert!(netlist.verify().is_ok());
36+
assert_eq!(netlist.objects().count(), 4);
37+
assert!(netlist.clean().unwrap());
38+
assert_eq!(netlist.objects().count(), 3);
39+
assert!(!netlist.clean().unwrap());
40+
}

0 commit comments

Comments
 (0)