Skip to content

Commit 1a8bb31

Browse files
committed
More targeted random tests + fix compilation without rand
1 parent b40ea3b commit 1a8bb31

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/lut.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,6 @@ impl fmt::Binary for Lut {
686686

687687
#[cfg(test)]
688688
mod tests {
689-
use rand::Rng;
690689

691690
use crate::{decomposition::DecompositionType, Lut};
692691

@@ -864,6 +863,7 @@ mod tests {
864863
#[cfg(feature = "rand")]
865864
/// Test that the permutation function works using random swaps
866865
fn test_permute() {
866+
use rand::Rng;
867867
let mut rng = rand::rng();
868868
for lut_size in 1..10 {
869869
let lut = Lut::random(lut_size);
@@ -915,16 +915,29 @@ mod tests {
915915
}
916916
}
917917

918+
#[cfg(feature = "rand")]
919+
fn gen_random(max_size: usize) -> Vec<Lut> {
920+
let mut ret = Vec::new();
921+
for lut_size in 1..=max_size {
922+
ret.push(Lut::zero(lut_size));
923+
ret.push(Lut::one(lut_size));
924+
for i in 0..lut_size {
925+
ret.push(Lut::nth_var(lut_size, i));
926+
}
927+
for _ in 0..10 {
928+
ret.push(Lut::random(lut_size));
929+
}
930+
}
931+
ret
932+
}
933+
918934
#[test]
919935
#[cfg(feature = "rand")]
920936
fn test_flip_n() {
921-
for lut_size in 2..=8 {
922-
for _ in 0..10 {
923-
let lut = Lut::random(lut_size);
924-
assert_eq!(!&lut, lut.flip_n(1 << lut_size));
925-
for i in 0..lut_size {
926-
assert_eq!(lut.flip(i), lut.flip_n(1 << i));
927-
}
937+
for lut in gen_random(8) {
938+
assert_eq!(!&lut, lut.flip_n(1 << lut.num_vars()));
939+
for i in 0..lut.num_vars() {
940+
assert_eq!(lut.flip(i), lut.flip_n(1 << i));
928941
}
929942
}
930943
}
@@ -976,40 +989,31 @@ mod tests {
976989
#[test]
977990
#[cfg(feature = "rand")]
978991
fn test_n_canonization() {
979-
for i in 0..=8 {
980-
for _ in 0..10 {
981-
let lut = Lut::random(i);
982-
let (canon, flip) = lut.n_canonization();
983-
assert_eq!(canon.flip_n(flip), lut);
984-
assert_eq!(lut.flip_n(flip), canon);
985-
assert_eq!(canon == lut, lut.is_n_canonical());
986-
}
992+
for lut in gen_random(8) {
993+
let (canon, flip) = lut.n_canonization();
994+
assert_eq!(canon.flip_n(flip), lut);
995+
assert_eq!(lut.flip_n(flip), canon);
996+
assert_eq!(canon == lut, lut.is_n_canonical());
987997
}
988998
}
989999

9901000
#[test]
9911001
#[cfg(feature = "rand")]
9921002
fn test_p_canonization() {
993-
for i in 0..=7 {
994-
for _ in 0..10 {
995-
let lut = Lut::random(i);
996-
let (canon, perm) = lut.p_canonization();
997-
assert_eq!(lut.permute(&perm), canon);
998-
assert_eq!(canon == lut, lut.is_p_canonical());
999-
}
1003+
for lut in gen_random(7) {
1004+
let (canon, perm) = lut.p_canonization();
1005+
assert_eq!(lut.permute(&perm), canon);
1006+
assert_eq!(canon == lut, lut.is_p_canonical());
10001007
}
10011008
}
10021009

10031010
#[test]
10041011
#[cfg(feature = "rand")]
10051012
fn test_npn_canonization() {
1006-
for i in 0..=5 {
1007-
for _ in 0..10 {
1008-
let lut = Lut::random(i);
1009-
let (canon, perm, flip) = lut.npn_canonization();
1010-
assert_eq!(lut.permute(&perm).flip_n(flip), canon);
1011-
assert_eq!(canon == lut, lut.is_npn_canonical());
1012-
}
1013+
for lut in gen_random(5) {
1014+
let (canon, perm, flip) = lut.npn_canonization();
1015+
assert_eq!(lut.permute(&perm).flip_n(flip), canon);
1016+
assert_eq!(canon == lut, lut.is_npn_canonical());
10131017
}
10141018
}
10151019

0 commit comments

Comments
 (0)