From 19b243478996d658ca2491a8948146824188d0a6 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Thu, 28 Nov 2024 11:01:08 +0100 Subject: [PATCH] Added tests for combination --- src/combination.rs | 136 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/src/combination.rs b/src/combination.rs index 0c38967..b4ed3e1 100644 --- a/src/combination.rs +++ b/src/combination.rs @@ -106,3 +106,139 @@ impl Iterator for Combinator { 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 = (0..100).collect(); + assert_eq!( + nth_lex(big.clone(), 100, 1).unwrap(), + (0..100).collect::>() + ); + assert!(nth_lex(big, 100, 2).is_err()); + } + + #[test] + fn nth_lex_some_big() { + let big: Vec = (0..100).collect(); + assert_eq!( + nth_lex(big.clone(), 50, 1).unwrap(), + (0..50).collect::>() + ); + } + + #[test] + fn nth_lex_one_big() { + let big: Vec = (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()); + //} +}