Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 18, 2024
1 parent 60947ca commit bd1192d
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 89 deletions.
3 changes: 2 additions & 1 deletion src/extensions/collectible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,10 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
/// let a = vec![1, 2, 2, 3];
/// let e = Vec::<i32>::new();
///
/// let intersection = a.intersect(&vec![4, 2, 2, 3, 4]);
/// let intersection = a.intersect(&vec![4, 3, 2, 2, 5]);
///
/// assert_eq!(intersection, vec![2, 2, 3]);
///
/// assert_eq!(e.intersect(&vec![1]), vec![]);
///
/// // Print 2, 3.
Expand Down
190 changes: 143 additions & 47 deletions tests/extensions/collectible.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use std::collections::HashMap;
use std::fmt::Debug;

use cantrip::{Collectible, Iterable};

use crate::extensions::util::{assert_equal, assert_vec_set_equal, assert_set_equal, Equal};
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)
where
C: Collectible<i64> + FromIterator<i64> + Iterable<Item<'a> = &'a i64> + Clone + Equal + Debug + 'a,
C::This<i64>: FromIterator<i64> + Equal + Debug,
C: Collectible<i64>
+ FromIterator<i64>
+ Iterable<Item<'a> = &'a i64>
+ Default
+ Extend<i64>
+ Clone
+ Equal
+ Debug
+ 'a,
C::This<i64>: FromIterator<i64> + Default + Extend<i64> + Equal + Debug,
{
// FIXME - implement test for all trait methods
let a_source = C::from_iter(vec![1, 2, 3]);
Expand All @@ -18,114 +28,200 @@ where

// add
if sequence {
assert_equal(a.add(3), vec![1, 2, 3, 3]);
assert_seq_equal(a.add(3), vec![1, 2, 3, 3]);
} else {
assert_set_equal(a.add(3), vec![1, 2, 3]);
}
assert_equal(e.add(1), vec![1]);
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();
if sequence {
assert_equal(a.add_multi(vec![3, 4]), vec![1, 2, 3, 3, 4]);
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();
assert_equal(e.add(1), vec![1]);

// combinations
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();
assert_vec_set_equal(a.combinations(0), vec![vec![]]);
assert_vec_set_equal(a.combinations(1), vec![vec![1], vec![2], vec![3]]);
assert_vec_set_equal(a.combinations(2), vec![vec![1, 2], vec![1, 3], vec![2, 3]]);
assert_vec_set_equal(a.combinations(3), vec![vec![1, 2, 3]]);
assert_vec_set_equal(a.combinations(4), vec![]);
let e = e_source.clone();
assert_vec_set_equal(e.combinations(2), vec![]);

// delete
if sequence {
assert_equal(b.delete(&2), vec![1, 2, 3]);
assert_seq_equal(b.delete(&2), vec![1, 2, 3]);
let b = b_source.clone();
assert_equal(b.delete(&4), vec![1, 2, 2, 3]);
assert_seq_equal(b.delete(&4), vec![1, 2, 2, 3]);
} else {
let a = a_source.clone();
assert_equal(a.delete(&2), vec![1, 3]);
assert_seq_equal(a.delete(&2), vec![1, 3]);
let a = a_source.clone();
assert_set_equal(a.delete(&4), vec![1, 2, 3]);
}
assert_seq_equal(e.delete(&2), vec![]);
let a = a_source.clone();
let b = b_source.clone();
let e = e_source.clone();
assert_equal(e.delete(&2), vec![]);

// delete_multi
if sequence {
assert_seq_equal(b.delete_multi(&vec![1, 2]), vec![2, 3]);
let b = b_source.clone();
assert_equal(b.delete_multi(&vec![1, 2]), vec![2, 3]);
let b = b_source.clone();
assert_equal(b.delete_multi(&vec![4]), vec![1, 2, 2, 3]);
assert_seq_equal(b.delete_multi(&vec![4]), vec![1, 2, 2, 3]);
} else {
assert_seq_equal(a.delete_multi(&vec![1, 2]), vec![3]);
let a = a_source.clone();
assert_equal(a.delete_multi(&vec![1, 2]), vec![3]);
let a = a_source.clone();
assert_equal(a.delete_multi(&vec![4]), vec![1, 2, 3]);
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();
assert_equal(e.delete_multi(&vec![1]), vec![]);

// fill_with
if sequence {
assert_equal(C::fill_with(|| 1, 2), vec![1, 1]);
assert_seq_equal(C::fill_with(|| 1, 2), vec![1, 1]);
} else {
assert_equal(C::fill_with(|| 1, 2), vec![1]);
assert_seq_equal(C::fill_with(|| 1, 2), vec![1]);
}
assert_equal(C::fill_with(|| 1, 0), vec![]);
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();
assert_equal(a.filter(|&x| x > 1), vec![2, 3]);
let e = e_source.clone();
assert_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();
assert_equal(a.filter_map(|&x| if x % 2 == 0 { Some(x + 1) } else { None } ), vec![3]);
let e = e_source.clone();
assert_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();
assert_equal(a.filter_map_to(|x| if x % 2 == 0 { Some(x + 1) } else { None } ), vec![3]);
let e = e_source.clone();
assert_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();
assert_eq!(a.find_map_to(|x| if x % 2 == 0 { Some(x) } else { None } ), Some(2));
let e = e_source.clone();
assert_eq!(e.find_map_to(|x| if x % 2 == 0 { Some(x) } else { None } ), None);

// flat - FIXME
// flat - FIXME - implement test
// 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]);
// let e = e_source.clone();
// assert_equal(e.map(|&x| C::from_iter(vec![x, -x])).flat(), vec![]);

// flat_map - FIXME
// let a = a_source.clone();
// assert_equal(a.flat_map(|&x| vec![x, -x]), vec![1, -1, 2, -2, 3, -3]);
// let e = e_source.clone();
// assert_equal(e.flat_map(|&x| vec![x, -x]), vec![]);

// flat_map_to - FIXME
// 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();
// assert_equal(a.flat_map_to(|x| vec![x, -x]), vec![1, -1, 2, -2, 3, -3]);
// let e = e_source.clone();

// 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();

// fold_to
let a = a_source.clone();
assert_eq!(a.fold_to(0, |acc, x| acc + x), 6);
let e = e_source.clone();
assert_eq!(e.fold_to(0, |acc, x| acc + x), 0);
let a = a_source.clone();
let e = e_source.clone();

// group_by
if sequence {
// FIXME - fix the test case
// let b = a_source.clone();
// assert_map_vec_equivalent(b.group_by(|x| x % 2), HashMap::from([(0, vec![2, 2]), (1, vec![1, 3])]));
} else {
assert_map_vec_equivalent(a.group_by(|x| x % 2), HashMap::from([
(0, vec![2]),
(1, vec![1, 3])
]));
}
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
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
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
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();

// 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
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_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![]);
let a = a_source.clone();
let e = e_source.clone();

// partition_map
let (a_even, a_odd) = a.partition_map(|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) });
assert_seq_equal(e_even, vec![]);
assert_seq_equal(e_odd, vec![]);

}
8 changes: 4 additions & 4 deletions tests/extensions/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Debug;

use cantrip::{Iterable, List};

use crate::extensions::util::{assert_equal, Equal};
use crate::extensions::util::{assert_seq_equal, Equal};

pub(crate) fn test_list<'a, C>()
where
Expand All @@ -29,8 +29,8 @@ where
assert_eq!(e.last(), None);

// repeat
assert_equal(a.repeat(2), vec![1, 2, 3, 1, 2, 3]);
assert_seq_equal(a.repeat(2), vec![1, 2, 3, 1, 2, 3]);
let a = a_source.clone();
assert_equal(a.repeat(0), vec![]);
assert_equal(e.repeat(2), vec![]);
assert_seq_equal(a.repeat(0), vec![]);
assert_seq_equal(e.repeat(2), vec![]);
}
10 changes: 6 additions & 4 deletions tests/extensions/map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt::Debug;

use cantrip::{Iterable, Map};
Expand All @@ -22,10 +23,11 @@ where
let e = e_source.clone();

// add
assert_map_equal(a.add(3, 3), vec![(0, 0), (1, 1), (2, 2), (3, 3)]);
assert_map_equal(e.add(0, 0), vec![(0, 0)]);
// assert_map_equal(empty.clone(), vec![]);
//
assert_map_equal(a.add(3, 3), HashMap::from([(0, 0), (1, 1), (2, 2), (3, 3)]));
assert_map_equal(e.add(0, 0), HashMap::from([(0, 0)]));
let e = e_source.clone();
assert_map_equal(e, HashMap::new());

// // all
// assert!(distinct.all(|(&k, &v)| k >= 0 && v >= 0));
// assert!(!distinct.all(|(&k, &v)| k == 1 && v >= 0));
Expand Down
2 changes: 1 addition & 1 deletion tests/extensions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(crate) mod collectible;
pub(crate) mod collections;
pub(crate) mod traits;
pub(crate) mod list;
pub(crate) mod map;
pub(crate) mod ordered;
Expand Down
12 changes: 9 additions & 3 deletions tests/extensions/collections.rs → tests/extensions/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ where
+ Collectible<i64>
+ FromIterator<i64>
+ Iterable<Item<'a> = &'a i64>
+ Default
+ Extend<i64>
+ Clone
+ Equal
+ Debug
+ 'a,
<C as Collectible<i64>>::This<i64>: FromIterator<i64> + Equal + Debug,
<C as Collectible<i64>>::This<i64>: FromIterator<i64> + Default + Extend<i64> + Equal + Debug,
{
test_traversable::<C>(false);
test_collectible::<C>(false);
Expand Down Expand Up @@ -56,11 +58,13 @@ where
+ FromIterator<i64>
+ IntoIterator<Item = i64, IntoIter = I>
+ Iterable<Item<'a> = &'a i64>
+ Default
+ Extend<i64>
+ Clone
+ Equal
+ Debug
+ 'a,
<C as Collectible<i64>>::This<i64>: FromIterator<i64> + Equal + Debug,
<C as Collectible<i64>>::This<i64>: FromIterator<i64> + Default + Extend<i64> + Equal + Debug,
{
test_traversable::<C>(true);
test_collectible::<C>(true);
Expand All @@ -80,11 +84,13 @@ where
+ FromIterator<i64>
+ IntoIterator<Item = i64, IntoIter = I>
+ Iterable<Item<'a> = &'a i64>
+ Default
+ Extend<i64>
+ Clone
+ Equal
+ Debug
+ 'a,
<C as Collectible<i64>>::This<i64>: FromIterator<i64> + Equal + Debug,
<C as Collectible<i64>>::This<i64>: FromIterator<i64> + Default + Extend<i64> + Equal + Debug,
{
test_traversable::<C>(true);
test_collectible::<C>(true);
Expand Down
Loading

0 comments on commit bd1192d

Please sign in to comment.