Added tests for combination

This commit is contained in:
Fabian Schmidt 2024-11-28 11:01:08 +01:00
parent 9f10cee3e0
commit 19b2434789

View File

@ -106,3 +106,139 @@ impl<T: Clone + Ord> Iterator for Combinator<T> {
Some(self.current.clone()) Some(self.current.clone())
} }
} }
#[cfg(test)]
mod test {
use crate::combination::nth_lex;
const SMALL: [i8; 5] = [1, 2, 3, 4, 5];
// Nth Lex
#[test]
fn nth_lex_zero_small() {
let small = SMALL.to_vec();
assert!(nth_lex(small, 0, 1).is_err());
}
#[test]
fn nth_lex_more_small() {
let small = SMALL.to_vec();
assert!(nth_lex(small, 6, 1).is_err());
}
#[test]
fn nth_lex_all_small() {
let small = SMALL.to_vec();
assert!(nth_lex(small.clone(), 5, 1).unwrap() == vec![1, 2, 3, 4, 5]);
assert!(nth_lex(small, 5, 2).is_err());
}
#[test]
fn nth_lex_some_small() {
let small = SMALL.to_vec();
assert_eq!(nth_lex(small.clone(), 2, 1).unwrap(), vec![1, 2]);
assert_eq!(nth_lex(small.clone(), 2, 2).unwrap(), vec![1, 3]);
assert_eq!(nth_lex(small.clone(), 2, 3).unwrap(), vec![1, 4]);
assert_eq!(nth_lex(small.clone(), 2, 4).unwrap(), vec![1, 5]);
assert_eq!(nth_lex(small.clone(), 2, 5).unwrap(), vec![2, 3]);
assert_eq!(nth_lex(small.clone(), 2, 6).unwrap(), vec![2, 4]);
assert_eq!(nth_lex(small.clone(), 2, 7).unwrap(), vec![2, 5]);
assert_eq!(nth_lex(small.clone(), 2, 8).unwrap(), vec![3, 4]);
assert_eq!(nth_lex(small.clone(), 2, 9).unwrap(), vec![3, 5]);
assert_eq!(nth_lex(small.clone(), 2, 10).unwrap(), vec![4, 5]);
assert!(nth_lex(small, 2, 11).is_err());
}
#[test]
fn nth_lex_one_small() {
let small = SMALL.to_vec();
assert_eq!(nth_lex(small.clone(), 1, 1).unwrap(), vec![1]);
assert_eq!(nth_lex(small.clone(), 1, 2).unwrap(), vec![2]);
assert_eq!(nth_lex(small.clone(), 1, 3).unwrap(), vec![3]);
assert_eq!(nth_lex(small.clone(), 1, 4).unwrap(), vec![4]);
assert_eq!(nth_lex(small.clone(), 1, 5).unwrap(), vec![5]);
assert!(nth_lex(small, 1, 6).is_err());
}
#[test]
fn nth_lex_all_big() {
let big: Vec<i32> = (0..100).collect();
assert_eq!(
nth_lex(big.clone(), 100, 1).unwrap(),
(0..100).collect::<Vec<i32>>()
);
assert!(nth_lex(big, 100, 2).is_err());
}
#[test]
fn nth_lex_some_big() {
let big: Vec<i32> = (0..100).collect();
assert_eq!(
nth_lex(big.clone(), 50, 1).unwrap(),
(0..50).collect::<Vec<i32>>()
);
}
#[test]
fn nth_lex_one_big() {
let big: Vec<i32> = (0..100).collect();
assert_eq!(nth_lex(big.clone(), 1, 1).unwrap(), vec![0]);
assert_eq!(nth_lex(big.clone(), 1, 2).unwrap(), vec![1]);
assert_eq!(nth_lex(big.clone(), 1, 3).unwrap(), vec![2]);
assert_eq!(nth_lex(big.clone(), 1, 4).unwrap(), vec![3]);
}
// Combinator. Needs fixing
//#[test]
//fn comb_zero_small() {
// let small = SMALL.to_vec();
// let comb = Combinator::new(small, 0);
// assert!(comb.is_err());
//}
//#[test]
//fn comb_more_small() {
// let small = SMALL.to_vec();
// let comb = Combinator::new(small, 6);
// assert!(comb.is_err());
//}
//#[test]
//fn comb_all_small() {
// let small = SMALL.to_vec();
// let mut comb = Combinator::new(small, 5).unwrap();
// assert!(comb.next() == Some(vec![1, 2, 3, 4, 5]));
// assert!(comb.next().is_none());
//}
//#[test]
//fn comb_some_small() {
// let small = SMALL.to_vec();
// let mut comb = Combinator::new(small, 2).unwrap();
// assert_eq!(comb.next(), Some(vec![1, 2]));
// assert_eq!(comb.next(), Some(vec![1, 3]));
// assert_eq!(comb.next(), Some(vec![1, 4]));
// assert_eq!(comb.next(), Some(vec![1, 5]));
// assert_eq!(comb.next(), Some(vec![2, 3]));
// assert_eq!(comb.next(), Some(vec![2, 4]));
// assert_eq!(comb.next(), Some(vec![2, 5]));
// assert_eq!(comb.next(), Some(vec![3, 4]));
// assert_eq!(comb.next(), Some(vec![3, 5]));
// assert_eq!(comb.next(), Some(vec![4, 5]));
// assert!(comb.next().is_none());
//}
//#[test]
//fn comb_one_small() {
// let small = SMALL.to_vec();
// let mut comb = Combinator::new(small, 1).unwrap();
// assert_eq!(comb.next(), Some(vec![1]));
// assert_eq!(comb.next(), Some(vec![2]));
// assert_eq!(comb.next(), Some(vec![3]));
// assert_eq!(comb.next(), Some(vec![4]));
// assert_eq!(comb.next(), Some(vec![5]));
// assert!(comb.next().is_none());
//}
}