Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 19, 2024
1 parent 93d8b95 commit 4b107ac
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/extensions/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,11 @@ pub trait Sequence<Item> {
/// ```
/// use cantrip::*;
///
/// let a = vec![-1, 4, 0, 1];
/// let a = vec![1, 2, 3];
///
/// assert_eq!(
/// a.map_while(|x| 16_i32.checked_div(*x)),
/// vec![-16, 4]
/// a.map_while(|&x| if x < 3 { Some(x + 1) } else { None }),
/// vec![2, 3]
/// );
/// ```
///
Expand All @@ -868,11 +868,11 @@ pub trait Sequence<Item> {
/// ```
/// use cantrip::*;
///
/// let a = vec![-1, 4, 0, 1];
/// let a = vec![1, 2, 3];
///
/// assert_eq!(
/// a.map(|x| 16_i32.checked_div(*x)).take_while(|x| x.is_some()).map(|x| x.unwrap()),
/// vec![-16, 4]
/// a.map(|&x| if x < 3 { Some(x + 1) } else { None }).take_while(|x| x.is_some()).map(|x| x.unwrap()),
/// vec![2, 3]
/// );
/// ```
fn map_while<B>(&self, predicate: impl FnMut(&Item) -> Option<B>) -> Self::This<B>;
Expand All @@ -887,7 +887,7 @@ pub trait Sequence<Item> {
/// assert_eq!(
/// a.merge(vec![0, 4, 5]),
/// vec![0, 1, 2, 3, 4, 5]
/// );
/// );
/// ```
fn merge(self, elements: impl IntoIterator<Item = Item>) -> Self
where
Expand Down
37 changes: 37 additions & 0 deletions tests/extensions/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ where
+ Iterable<Item<'a> = &'a i64>
+ Clone
+ Equal
+ UnwindSafe
+ Debug
+ 'a,
for<'c> &'c C: UnwindSafe,
Expand Down Expand Up @@ -245,6 +246,42 @@ where
// assert_seq_equal(a.intersperse_with(3, || 0), vec![1, 2, 3]);
// assert_seq_equal(e.intersperse_with(1, || 0), vec![]);

// map_while - FIXME - implement test
// assert_seq_equal(a.map_while(|&x| if x < 2 { Some(x + 1) } else { None }), vec![2, 3]);
// assert_seq_equal(e.map_while(|&x| if x < 2 { Some(x + 1) } else { None }), vec![]);

// merge
let a = a_source.clone();
let e = e_source.clone();
assert_seq_equal(a.merge(vec![0, 4, 5]), vec![0, 1, 2, 3, 4, 5]);
assert_seq_equal(e.merge(vec![1]), vec![1]);
let e = e_source.clone();
assert_seq_equal(e.merge(vec![]), vec![]);

// merge_by
let a = a_source.clone();
let e = e_source.clone();
assert_seq_equal(a.merge_by(vec![0, 4, 5], |l, r| l.cmp(r)), vec![0, 1, 2, 3, 4, 5]);
assert_seq_equal(e.merge_by(vec![1], |l, r| l.cmp(r)), vec![1]);
let e = e_source.clone();
assert_seq_equal(e.merge_by(vec![], |l, r| l.cmp(r)), vec![]);

// move_at
let a = a_source.clone();
let e = e_source.clone();
assert_seq_equal(a.move_at(0, 2), vec![2, 3, 1]);
let a = a_source.clone();
assert_seq_equal(a.move_at(2, 1), vec![1, 3, 2]);
let a = a_source.clone();
assert_seq_equal(a.move_at(1, 1), vec![1, 2, 3]);
assert!(panic::catch_unwind(|| { e.move_at(0, 0) }).is_err());

// pad_left - FIXME - implement test
// let a = a_source.clone();
// let e = e_source.clone();
// assert_seq_equal(a.pad_left(5, 4), vec![4, 4, 1, 2, 3]);
// assert_seq_equal(e.pad_left(1, 1), vec![1]);

// // rev
// assert_equal(repeated.clone().rev(), vec![3, 2, 2, 1]);
// assert_equal(empty.clone().rev(), vec![]);
Expand Down
2 changes: 2 additions & 0 deletions tests/extensions/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ where
+ Extend<i64>
+ Clone
+ Equal
+ UnwindSafe
+ Debug
+ 'a,
<C as Collectible<i64>>::This<i64>: FromIterator<i64> + Default + Extend<i64> + Equal + Debug,
Expand Down Expand Up @@ -89,6 +90,7 @@ where
+ Extend<i64>
+ Clone
+ Equal
+ UnwindSafe
+ Debug
+ 'a,
<C as Collectible<i64>>::This<i64>: FromIterator<i64> + Default + Extend<i64> + Equal + Debug,
Expand Down

0 comments on commit 4b107ac

Please sign in to comment.