move tests on circuits (dag)

This commit is contained in:
2026-04-22 16:27:18 +02:00
parent 8c646fd920
commit dee53e6339
9 changed files with 59 additions and 64 deletions

View File

@@ -1,4 +1,3 @@
use circuit_cas::poly::flat;
use circuit_cas::var;
fn main() {

View File

@@ -135,56 +135,3 @@ impl<V: Var> Mul for CircuitNode<V> {
}
}
}
#[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);
}
}

View File

@@ -1,2 +1,6 @@
pub mod dag;
pub mod quotient;
#[cfg(test)]
mod tests;

53
src/circuit/tests.rs Normal file
View File

@@ -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);
}

View File

@@ -15,7 +15,6 @@ impl<V: Var> Default for Poly<V> {
}
}
impl<V: Var, T: IntoIterator> From<T> for Poly<V>
where
Poly<V>: FromIterator<<T as IntoIterator>::Item>,
@@ -92,4 +91,3 @@ impl<V: Var, U: Into<V>> FromIterator<(U, u32)> for Mono<V> {
Mono { term }
}
}

View File

@@ -40,7 +40,6 @@ impl<V: Var> Display for Poly<V> {
}
}
impl<V: Var> Display for Mono<V> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(
@@ -56,4 +55,3 @@ impl<V: Var> Display for Mono<V> {
)
}
}

View File

@@ -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;

View File

@@ -1,4 +1,3 @@
use super::flat::{Mono, Poly};
use super::var::StaticVar;

View File

@@ -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))
};
}