move tests on circuits (dag)
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
use circuit_cas::poly::flat;
|
||||
use circuit_cas::var;
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,6 @@
|
||||
pub mod dag;
|
||||
pub mod quotient;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
|
||||
53
src/circuit/tests.rs
Normal file
53
src/circuit/tests.rs
Normal 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);
|
||||
}
|
||||
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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> {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
use super::flat::{Mono, Poly};
|
||||
use super::var::StaticVar;
|
||||
|
||||
|
||||
@@ -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))
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user