From dee53e6339feee88728471a87a0445a1e30f96f2 Mon Sep 17 00:00:00 2001 From: asteri Date: Wed, 22 Apr 2026 16:27:18 +0200 Subject: [PATCH] move tests on circuits (dag) --- examples/flat.rs | 1 - src/circuit/dag.rs | 53 -------------------------------------------- src/circuit/mod.rs | 4 ++++ src/circuit/tests.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/poly/flat.rs | 2 -- src/poly/fmt.rs | 2 -- src/poly/mod.rs | 5 ++--- src/poly/tests.rs | 1 - src/poly/var.rs | 2 -- 9 files changed, 59 insertions(+), 64 deletions(-) create mode 100644 src/circuit/tests.rs diff --git a/examples/flat.rs b/examples/flat.rs index 75605c2..ed8c7ba 100644 --- a/examples/flat.rs +++ b/examples/flat.rs @@ -1,4 +1,3 @@ -use circuit_cas::poly::flat; use circuit_cas::var; fn main() { diff --git a/src/circuit/dag.rs b/src/circuit/dag.rs index e28f2a6..107e1f0 100644 --- a/src/circuit/dag.rs +++ b/src/circuit/dag.rs @@ -135,56 +135,3 @@ impl Mul for CircuitNode { } } } - -#[cfg(test)] -mod tests { - use super::*; - use crate::poly::var::StaticVar; - - #[test] - fn test_deduplication() { - let circuit = Rc::new(RefCell::new(Circuit::new())); - - // Same leaf constructed twice returns the same NodeId - let x1 = circuit.leaf(StaticVar::from("x")); - let x2 = circuit.leaf(StaticVar::from("x")); - assert_eq!(x1.id, x2.id); - assert_eq!(circuit.borrow().len(), 1); - - // Same sum constructed twice returns the same NodeId - let _y = circuit.leaf(StaticVar::from("y")); - let sum1 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); - let sum2 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); - assert_eq!(sum1.id, sum2.id); - assert_eq!(circuit.borrow().len(), 3); // x, y, x+y - - // Shared subexpression: (x + y) * (x + y) reuses the x+y node - let xy = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); - let xy2 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); - let _sq = xy * xy2; - assert_eq!(circuit.borrow().len(), 4); // x, y, x+y, (x+y)*(x+y) - - // Commutativity: x+y and y+x are the same node - let xy = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); - let yx = circuit.leaf(StaticVar::from("y")) + circuit.leaf(StaticVar::from("x")); - assert_eq!(xy.id, yx.id); - - // Associativity: (x+y)+z and x+(y+z) are distinct nodes - let _z = circuit.leaf(StaticVar::from("z")); - let xy_z = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"))) - + circuit.leaf(StaticVar::from("z")); - let x_yz = circuit.leaf(StaticVar::from("x")) - + (circuit.leaf(StaticVar::from("y")) + circuit.leaf(StaticVar::from("z"))); - assert_ne!(xy_z.id, x_yz.id); - - // Deep shared structure: (x+y)*z appears twice in ((x+y)*z) + ((x+y)*z) - let xyz1 = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"))) - * circuit.leaf(StaticVar::from("z")); - let xyz2 = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"))) - * circuit.leaf(StaticVar::from("z")); - assert_eq!(xyz1.id, xyz2.id); - let _sum = xyz1 + xyz2; - // x, y, z, x+y(==y+x), (x+y)*z, (x+y)+z, y+z, x+(y+z), (x+y)*z+(x+y)*z, sq - assert_eq!(circuit.borrow().len(), 10); - } -} diff --git a/src/circuit/mod.rs b/src/circuit/mod.rs index 8d8ef0b..9c994c2 100644 --- a/src/circuit/mod.rs +++ b/src/circuit/mod.rs @@ -1,2 +1,6 @@ pub mod dag; pub mod quotient; + +#[cfg(test)] +mod tests; + diff --git a/src/circuit/tests.rs b/src/circuit/tests.rs new file mode 100644 index 0000000..58c1184 --- /dev/null +++ b/src/circuit/tests.rs @@ -0,0 +1,53 @@ + +use std::cell::RefCell; +use std::rc::Rc; + +use super::dag::{Circuit, CircuitExt}; +use crate::poly::var::StaticVar; + +#[test] +fn test_deduplication() { + let circuit = Rc::new(RefCell::new(Circuit::new())); + + // Same leaf constructed twice returns the same NodeId + let x1 = circuit.leaf(StaticVar::from("x")); + let x2 = circuit.leaf(StaticVar::from("x")); + assert_eq!(x1.id, x2.id); + assert_eq!(circuit.borrow().len(), 1); + + // Same sum constructed twice returns the same NodeId + let _y = circuit.leaf(StaticVar::from("y")); + let sum1 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); + let sum2 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); + assert_eq!(sum1.id, sum2.id); + assert_eq!(circuit.borrow().len(), 3); // x, y, x+y + + // Shared subexpression: (x + y) * (x + y) reuses the x+y node + let xy = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); + let xy2 = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); + let _sq = xy * xy2; + assert_eq!(circuit.borrow().len(), 4); // x, y, x+y, (x+y)*(x+y) + + // Commutativity: x+y and y+x are the same node + let xy = circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y")); + let yx = circuit.leaf(StaticVar::from("y")) + circuit.leaf(StaticVar::from("x")); + assert_eq!(xy.id, yx.id); + + // Associativity: (x+y)+z and x+(y+z) are distinct nodes + let _z = circuit.leaf(StaticVar::from("z")); + let xy_z = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"))) + + circuit.leaf(StaticVar::from("z")); + let x_yz = circuit.leaf(StaticVar::from("x")) + + (circuit.leaf(StaticVar::from("y")) + circuit.leaf(StaticVar::from("z"))); + assert_ne!(xy_z.id, x_yz.id); + + // Deep shared structure: (x+y)*z appears twice in ((x+y)*z) + ((x+y)*z) + let xyz1 = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"))) + * circuit.leaf(StaticVar::from("z")); + let xyz2 = (circuit.leaf(StaticVar::from("x")) + circuit.leaf(StaticVar::from("y"))) + * circuit.leaf(StaticVar::from("z")); + assert_eq!(xyz1.id, xyz2.id); + let _sum = xyz1 + xyz2; + // x, y, z, x+y(==y+x), (x+y)*z, (x+y)+z, y+z, x+(y+z), (x+y)*z+(x+y)*z, sq + assert_eq!(circuit.borrow().len(), 10); +} diff --git a/src/poly/flat.rs b/src/poly/flat.rs index 818efb9..ceedea7 100644 --- a/src/poly/flat.rs +++ b/src/poly/flat.rs @@ -15,7 +15,6 @@ impl Default for Poly { } } - impl From for Poly where Poly: FromIterator<::Item>, @@ -92,4 +91,3 @@ impl> FromIterator<(U, u32)> for Mono { Mono { term } } } - diff --git a/src/poly/fmt.rs b/src/poly/fmt.rs index e16742f..40e7cf5 100644 --- a/src/poly/fmt.rs +++ b/src/poly/fmt.rs @@ -40,7 +40,6 @@ impl Display for Poly { } } - impl Display for Mono { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!( @@ -56,4 +55,3 @@ impl Display for Mono { ) } } - diff --git a/src/poly/mod.rs b/src/poly/mod.rs index 8b38f99..51fd3b1 100644 --- a/src/poly/mod.rs +++ b/src/poly/mod.rs @@ -1,8 +1,7 @@ pub mod flat; -pub mod var; -pub mod ops; pub mod fmt; +pub mod ops; +pub mod var; #[cfg(test)] mod tests; - diff --git a/src/poly/tests.rs b/src/poly/tests.rs index 7a5c841..870ebde 100644 --- a/src/poly/tests.rs +++ b/src/poly/tests.rs @@ -1,4 +1,3 @@ - use super::flat::{Mono, Poly}; use super::var::StaticVar; diff --git a/src/poly/var.rs b/src/poly/var.rs index b2daf72..a3fe71a 100644 --- a/src/poly/var.rs +++ b/src/poly/var.rs @@ -9,7 +9,6 @@ pub struct StaticVar { pub trait Var: PartialEq + Eq + PartialOrd + Ord + Clone + Hash + Debug + Display {} - impl Var for StaticVar {} impl From<&'static str> for StaticVar { @@ -51,4 +50,3 @@ macro_rules! var { ::circuit_cas::poly::var::StaticVar::from(($name, $idx1, $idx2)) }; } -