From abfb342e4e702ace88813aa759a07656f58e27d9 Mon Sep 17 00:00:00 2001 From: Martin Ockajak Date: Thu, 18 Jul 2024 18:06:45 +0200 Subject: [PATCH] . --- src/extensions/collectible.rs | 16 +-- src/extensions/traversable.rs | 6 +- tests/extensions/collectible.rs | 191 ++++++++++++++++++++++++-------- tests/extensions/list.rs | 6 +- tests/extensions/traversable.rs | 3 +- 5 files changed, 160 insertions(+), 62 deletions(-) diff --git a/src/extensions/collectible.rs b/src/extensions/collectible.rs index 3f556ea..0dc3d8e 100644 --- a/src/extensions/collectible.rs +++ b/src/extensions/collectible.rs @@ -968,7 +968,7 @@ pub trait Collectible: IntoIterator { /// /// let a = vec![1, 2, 3]; /// - /// let (even, odd) = a.partition(|n| n % 2 == 0); + /// let (even, odd) = a.partition(|&x| x % 2 == 0); /// /// assert_eq!(even, vec![2]); /// assert_eq!(odd, vec![1, 3]); @@ -998,7 +998,7 @@ pub trait Collectible: IntoIterator { /// /// let a = vec![1, 2, 3]; /// - /// let (even, odd) = a.partition_map(|n| if n % 2 == 0 { Ok(n + 3) } else { Err(*n) }); + /// let (even, odd) = a.partition_map(|&x| if x % 2 == 0 { Ok(x + 3) } else { Err(x) }); /// /// assert_eq!(even, vec![5]); /// assert_eq!(odd, vec![1, 3]); @@ -1025,7 +1025,7 @@ pub trait Collectible: IntoIterator { /// /// let a = vec![1, 2, 3]; /// - /// let (even, odd) = a.partition_map_to(|n| if n % 2 == 0 { Ok(n + 3) } else { Err(n) }); + /// let (even, odd) = a.partition_map_to(|x| if x % 2 == 0 { Ok(x + 3) } else { Err(x) }); /// /// assert_eq!(even, vec![5]); /// assert_eq!(odd, vec![1, 3]); @@ -1095,12 +1095,12 @@ pub trait Collectible: IntoIterator { /// ``` /// use cantrip::*; /// - /// let a = vec![2, 3, 4]; + /// let a = vec![1, 2, 3]; /// let e = Vec::::new(); /// /// let product = a.product(); /// - /// assert_eq!(product, 24); + /// assert_eq!(product, 6); /// assert_eq!(e.product(), 1); /// ``` #[inline] @@ -1136,15 +1136,15 @@ pub trait Collectible: IntoIterator { /// # let a_source = vec![1, 2, 3]; /// let a = vec![1, 2, 3]; /// - /// let reduced = a.reduce_to(|acc, e| acc + e).unwrap(); + /// let reduced = a.reduce_to(|acc, e| acc + e); /// - /// assert_eq!(reduced, 6); + /// assert_eq!(reduced, Some(6)); /// /// // Which is equivalent to doing it with `fold`: /// # let a = a_source.clone(); /// let folded = a.fold_to(0, |acc, e| acc + e); /// - /// assert_eq!(reduced, folded); + /// assert_eq!(reduced.unwrap(), folded); /// ``` #[inline] fn reduce_to(self, function: impl FnMut(Item, Item) -> Item) -> Option diff --git a/src/extensions/traversable.rs b/src/extensions/traversable.rs index 09057a5..787ec7e 100644 --- a/src/extensions/traversable.rs +++ b/src/extensions/traversable.rs @@ -607,15 +607,15 @@ pub trait Traversable { /// # let a_source = vec![1, 2, 3]; /// let a = vec![1, 2, 3]; /// - /// let reduced = a.reduce(|&acc, &e| acc + e).unwrap(); + /// let reduced = a.reduce(|&acc, &e| acc + e); /// - /// assert_eq!(reduced, 6); + /// assert_eq!(reduced, Some(6)); /// /// // Which is equivalent to doing it with `fold`: /// # let a = a_source.clone(); /// let folded = a.fold(0, |acc, &e| acc + e); /// - /// assert_eq!(reduced, folded); + /// assert_eq!(reduced.unwrap(), folded); /// ``` fn reduce(&self, function: impl FnMut(&Item, &Item) -> Item) -> Option; diff --git a/tests/extensions/collectible.rs b/tests/extensions/collectible.rs index 5b9cf51..b1be995 100644 --- a/tests/extensions/collectible.rs +++ b/tests/extensions/collectible.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use std::fmt::Debug; use cantrip::{Collectible, Iterable}; - use crate::extensions::util::{assert_map_equal, assert_map_vec_equivalent, assert_seq_equal, assert_set_equal, assert_vec_seq_equivalent, Equal}; pub(crate) fn test_collectible<'a, C>(sequence: bool) @@ -22,41 +21,41 @@ where let a_source = C::from_iter(vec![1, 2, 3]); let b_source = C::from_iter(vec![1, 2, 2, 3]); let e_source = C::from_iter(vec![]); - let a = a_source.clone(); - let b = b_source.clone(); - let e = e_source.clone(); // add + let a = a_source.clone(); + let e = e_source.clone(); if sequence { assert_seq_equal(a.add(3), vec![1, 2, 3, 3]); } else { assert_set_equal(a.add(3), vec![1, 2, 3]); } assert_seq_equal(e.add(1), vec![1]); - let a = a_source.clone(); - let e = e_source.clone(); // add_multi + let a = a_source.clone(); + let e = e_source.clone(); if sequence { assert_seq_equal(a.add_multi(vec![3, 4]), vec![1, 2, 3, 3, 4]); } else { assert_set_equal(a.add_multi(vec![3, 4]), vec![1, 2, 3, 4]); } assert_seq_equal(e.add(1), vec![1]); - let a = a_source.clone(); - let e = e_source.clone(); // combinations + let a = a_source.clone(); + let e = e_source.clone(); assert_vec_seq_equivalent(a.combinations(0), vec![vec![]]); assert_vec_seq_equivalent(a.combinations(1), vec![vec![1], vec![2], vec![3]]); assert_vec_seq_equivalent(a.combinations(2), vec![vec![1, 2], vec![1, 3], vec![2, 3]]); assert_vec_seq_equivalent(a.combinations(3), vec![vec![1, 2, 3]]); assert_vec_seq_equivalent(a.combinations(4), vec![]); assert_vec_seq_equivalent(e.combinations(2), vec![]); - let a = a_source.clone(); - let e = e_source.clone(); // delete + let a = a_source.clone(); + let b = b_source.clone(); + let e = e_source.clone(); if sequence { assert_seq_equal(b.delete(&2), vec![1, 2, 3]); let b = b_source.clone(); @@ -67,11 +66,11 @@ where assert_set_equal(a.delete(&4), vec![1, 2, 3]); } assert_seq_equal(e.delete(&2), vec![]); + + // delete_multi let a = a_source.clone(); let b = b_source.clone(); let e = e_source.clone(); - - // delete_multi if sequence { assert_seq_equal(b.delete_multi(&vec![1, 2]), vec![2, 3]); let b = b_source.clone(); @@ -82,11 +81,9 @@ where assert_seq_equal(a.delete_multi(&vec![4]), vec![1, 2, 3]); } assert_seq_equal(e.delete_multi(&vec![1]), vec![]); - let a = a_source.clone(); - let b = b_source.clone(); - let e = e_source.clone(); // fill_with + let b = b_source.clone(); if sequence { assert_seq_equal(C::fill_with(|| 1, 2), vec![1, 1]); } else { @@ -95,55 +92,57 @@ where assert_seq_equal(C::fill_with(|| 1, 0), vec![]); // filter - assert_seq_equal(a.filter(|&x| x > 1), vec![2, 3]); - assert_seq_equal(e.filter(|&x| x > 1), vec![]); let a = a_source.clone(); let e = e_source.clone(); + assert_seq_equal(a.filter(|&x| x > 1), vec![2, 3]); + assert_seq_equal(e.filter(|&x| x > 1), vec![]); // filter_map - assert_seq_equal(a.filter_map(|&x| if x % 2 == 0 { Some(x + 1) } else { None }), vec![3]); - assert_seq_equal(e.filter_map(|&x| if x % 2 == 0 { Some(x + 1) } else { None }), vec![]); let a = a_source.clone(); let e = e_source.clone(); + assert_seq_equal(a.filter_map(|&x| if x % 2 == 0 { Some(x + 1) } else { None }), vec![3]); + assert_seq_equal(e.filter_map(|&x| if x % 2 == 0 { Some(x + 1) } else { None }), vec![]); // filter_map_to - assert_seq_equal(a.filter_map_to(|x| if x % 2 == 0 { Some(x + 1) } else { None }), vec![3]); - assert_seq_equal(e.filter_map_to(|x| if x % 2 == 0 { Some(x + 1) } else { None }), vec![]); let a = a_source.clone(); let e = e_source.clone(); + assert_seq_equal(a.filter_map_to(|x| if x % 2 == 0 { Some(x + 1) } else { None }), vec![3]); + assert_seq_equal(e.filter_map_to(|x| if x % 2 == 0 { Some(x + 1) } else { None }), vec![]); // find_map_to - assert_eq!(a.find_map_to(|x| if x % 2 == 0 { Some(x) } else { None }), Some(2)); - assert_eq!(e.find_map_to(|x| if x % 2 == 0 { Some(x) } else { None }), None); let a = a_source.clone(); let e = e_source.clone(); + assert_eq!(a.find_map_to(|x| if x % 2 == 0 { Some(x) } else { None }), Some(2)); + assert_eq!(e.find_map_to(|x| if x % 2 == 0 { Some(x) } else { None }), None); // flat - FIXME - implement test + // let a = a_source.clone(); + // let e = e_source.clone(); // let c = D::from_iter(vec![C::from_iter(vec![1, 2]), C::from_iter(vec![3])]); // assert_equal(c.flat(), vec![1, 2, 3]); // assert_equal(e.map(|&x| C::from_iter(vec![x, -x])).flat(), vec![]); - // let a = a_source.clone(); - // let e = e_source.clone(); // flat_map - FIXME - implement test - // assert_equal(a.flat_map(|&x| vec![x, -x]), vec![1, -1, 2, -2, 3, -3]); - // assert_equal(e.flat_map(|&x| vec![x, -x]), vec![]); // let a = a_source.clone(); // let e = e_source.clone(); + // assert_equal(a.flat_map(|&x| vec![x, -x]), vec![1, -1, 2, -2, 3, -3]); + // assert_equal(e.flat_map(|&x| vec![x, -x]), vec![]); // flat_map_to - FIXME - implement test - // assert_equal(a.flat_map_to(|x| vec![x, -x]), vec![1, -1, 2, -2, 3, -3]); - // assert_equal(e.flat_map_to(|x| vec![x, -x]), vec![]); // let a = a_source.clone(); // let e = e_source.clone(); + // assert_equal(a.flat_map_to(|x| vec![x, -x]), vec![1, -1, 2, -2, 3, -3]); + // assert_equal(e.flat_map_to(|x| vec![x, -x]), vec![]); // fold_to - assert_eq!(a.fold_to(0, |acc, x| acc + x), 6); - assert_eq!(e.fold_to(0, |acc, x| acc + x), 0); let a = a_source.clone(); let e = e_source.clone(); + assert_eq!(a.fold_to(0, |acc, x| acc + x), 6); + assert_eq!(e.fold_to(0, |acc, x| acc + x), 0); // group_by + let a = a_source.clone(); + let e = e_source.clone(); if sequence { // FIXME - fix the test case // let b = a_source.clone(); @@ -155,73 +154,169 @@ where ])); } assert_map_vec_equivalent(e.group_by(|x| x % 2), HashMap::new()); - let a = a_source.clone(); - let e = e_source.clone(); // group_fold_to + let a = a_source.clone(); + let e = e_source.clone(); assert_map_equal(a.group_fold_to(|x| x % 2, 0, |acc, x| acc + x), HashMap::from([ (0, 2), (1, 4) ])); assert_map_equal(e.group_fold_to(|x| x % 2, 0, |acc, x| acc + x), HashMap::new()); - let a = a_source.clone(); - let e = e_source.clone(); // group_reduce_to + let a = a_source.clone(); + let e = e_source.clone(); assert_eq!(a.group_reduce_to(|x| x % 2, |acc, x| acc + x), HashMap::from([ (0, 2), (1, 4), ])); assert_eq!(e.group_reduce_to(|x| x % 2, |acc, x| acc + x), HashMap::new()); - let a = a_source.clone(); - let e = e_source.clone(); // intersect + let a = a_source.clone(); + let e = e_source.clone(); if sequence { assert_seq_equal(b.intersect(&vec![4, 3, 2, 2, 5]), vec![2, 2, 3]); } else { assert_seq_equal(a.intersect(&vec![4, 3, 2, 2, 5]), vec![2, 3]); } assert_seq_equal(e.intersect(&vec![1]), vec![]); - let a = a_source.clone(); - let e = e_source.clone(); // map - assert_seq_equal(a.map(|&x| x + 1), vec![2, 3, 4]); - assert_seq_equal(e.map(|&x| x + 1), vec![]); let a = a_source.clone(); let e = e_source.clone(); + assert_seq_equal(a.map(|&x| x + 1), vec![2, 3, 4]); + assert_seq_equal(e.map(|&x| x + 1), vec![]); // map_to assert_seq_equal(a.map(|x| x + 1), vec![2, 3, 4]); assert_seq_equal(e.map(|x| x + 1), vec![]); - let a = a_source.clone(); - let e = e_source.clone(); // largest + let a = a_source.clone(); + let e = e_source.clone(); assert_seq_equal(a.largest(2), vec![3, 2]); let a = a_source.clone(); assert_seq_equal(a.largest(4), vec![3, 2, 1]); assert_seq_equal(e.largest(3), vec![]); - let a = a_source.clone(); - let e = e_source.clone(); // partition + let a = a_source.clone(); + let e = e_source.clone(); let (a_even, a_odd) = a.partition(|n| n % 2 == 0); assert_seq_equal(a_even, vec![2]); assert_seq_equal(a_odd, vec![1, 3]); let (e_even, e_odd) = e.partition(|n| n % 2 == 0); assert_seq_equal(e_even, vec![]); assert_seq_equal(e_odd, vec![]); + + // partition_map let a = a_source.clone(); let e = e_source.clone(); + let (a_even, a_odd) = a.partition_map(|&x| if x % 2 == 0 { Ok(x + 3) } else { Err(x) }); + assert_seq_equal(a_even, vec![5]); + assert_seq_equal(a_odd, vec![1, 3]); + let (e_even, e_odd) = e.partition_map(|&x| if x % 2 == 0 { Ok(x + 3) } else { Err(x) }); + assert_seq_equal(e_even, vec![]); + assert_seq_equal(e_odd, vec![]); - // partition_map - let (a_even, a_odd) = a.partition_map(|n| if n % 2 == 0 { Ok(n + 3) } else { Err(*n) }); + // partition_map_to + let a = a_source.clone(); + let e = e_source.clone(); + let (a_even, a_odd) = a.partition_map_to(|n| if n % 2 == 0 { Ok(n + 3) } else { Err(n) }); assert_seq_equal(a_even, vec![5]); assert_seq_equal(a_odd, vec![1, 3]); - let (e_even, e_odd) = e.partition_map(|n| if n % 2 == 0 { Ok(n + 3) } else { Err(*n) }); + let (e_even, e_odd) = e.partition_map_to(|x| if x % 2 == 0 { Ok(x + 3) } else { Err(x) }); assert_seq_equal(e_even, vec![]); assert_seq_equal(e_odd, vec![]); + // powerset + let a = a_source.clone(); + let e = e_source.clone(); + assert_vec_seq_equivalent(a.powerset(), vec![ + vec![], + vec![1], vec![2], vec![3], + vec![1, 2], vec![1, 3], vec![2, 3], + vec![1, 2, 3]] + ); + assert_vec_seq_equivalent(e.powerset(), vec![vec![]]); + + // product + let a = a_source.clone(); + let b = b_source.clone(); + let e = e_source.clone(); + if sequence { + assert_eq!(b.product(), 12); + } else { + assert_eq!(a.product(), 6); + } + assert_eq!(e.product(), 1); + + // reduce_to + let a = a_source.clone(); + let e = e_source.clone(); + assert_eq!(a.reduce_to(|acc, e| acc + e), Some(6)); + assert_eq!(e.reduce_to(|acc, e| acc + e), None); + + // smallest + let a = a_source.clone(); + let e = e_source.clone(); + assert_seq_equal(a.smallest(2), vec![1, 2]); + let a = a_source.clone(); + assert_seq_equal(a.smallest(4), vec![1, 2, 3]); + assert_seq_equal(e.smallest(3), vec![]); + let a = a_source.clone(); + let b = b_source.clone(); + let e = e_source.clone(); + + // substitute + if sequence { + assert_seq_equal(b.substitute(&2, 4), vec![1, 4, 2, 3]); + let b = b_source.clone(); + assert_set_equal(b.substitute(&4, 5), vec![1, 2, 2, 3]); + } else { + assert_set_equal(a.substitute(&2, 4), vec![1, 4, 3]); + let a = a_source.clone(); + assert_seq_equal(a.substitute(&4, 5), vec![1, 2, 3]); + } + assert_seq_equal(e.substitute(&1, 2), vec![]); + + // substitute_multi + let a = a_source.clone(); + let b = b_source.clone(); + // let e = e_source.clone(); + if sequence { + assert_seq_equal(b.substitute_multi(&vec![2, 3], vec![4, 5]), vec![1, 4, 2, 5]); + let b = b_source.clone(); + assert_seq_equal(b.substitute_multi(&vec![2, 2], vec![4, 5]), vec![1, 4, 5, 3]); + let b = b_source.clone(); + assert_seq_equal(b.substitute_multi(&vec![2, 4], vec![4, 5]), vec![1, 4, 2, 3]); + let b = b_source.clone(); + assert_seq_equal(b.substitute_multi(&vec![4, 5], vec![1, 1]), vec![1, 2, 2, 3]); + } else { + assert_set_equal(a.substitute_multi(&vec![2, 3], vec![4, 5]), vec![1, 4, 5]); + // FIXME - fix the test case + // let a = a_source.clone(); + // assert_set_equal(a.substitute_multi(&vec![2, 2], vec![4, 5]), vec![1, 5, 3]); + // let a = a_source.clone(); + // assert_set_equal(a.substitute_multi(&vec![2, 4], vec![4, 5]), vec![1, 4, 3]); + let a = a_source.clone(); + assert_set_equal(a.substitute_multi(&vec![4, 5], vec![1, 1]), vec![1, 2, 3]); + } + // assert_seq_equal(e.substitute_multi(&vec![1], vec![2]), vec![]); + + // sum + let a = a_source.clone(); + let b = b_source.clone(); + let e = e_source.clone(); + if sequence { + assert_eq!(b.sum(), 8); + } else { + assert_eq!(a.sum(), 6); + } + assert_eq!(e.sum(), 0); + + // unit + assert_seq_equal(C::unit(1), vec![1]); } diff --git a/tests/extensions/list.rs b/tests/extensions/list.rs index 6ad3b61..bda3109 100644 --- a/tests/extensions/list.rs +++ b/tests/extensions/list.rs @@ -17,14 +17,16 @@ where { let a_source = C::from_iter(vec![1, 2, 3]); let e_source = C::from_iter(vec![]); - let a = a_source.clone(); - let e = e_source.clone(); // first + let a = a_source.clone(); + let e = e_source.clone(); assert_eq!(a.first(), Some(&1)); assert_eq!(e.first(), None); // last + let a = a_source.clone(); + let e = e_source.clone(); assert_eq!(a.last(), Some(&3)); assert_eq!(e.last(), None); diff --git a/tests/extensions/traversable.rs b/tests/extensions/traversable.rs index ef73543..3e53c96 100644 --- a/tests/extensions/traversable.rs +++ b/tests/extensions/traversable.rs @@ -104,7 +104,8 @@ where assert_eq!(e.minmax_of(), None); // reduce - assert_eq!(a.reduce(|&acc, &e| acc + e).unwrap(), 6); + assert_eq!(a.reduce(|&acc, &e| acc + e), Some(6)); + assert_eq!(e.reduce(|&acc, &e| acc + e), None); // subset assert!(a.subset(&vec![4, 3, 2, 2, 1]));