Skip to content

Commit d353c9f

Browse files
committed
Add specialization fn fold to Coalesce (1) tests
1 parent 7a97808 commit d353c9f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

tests/quick.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,29 @@ quickcheck! {
753753
}
754754
}
755755

756+
quickcheck! {
757+
fn dedup_via_coalesce(a: Vec<i32>) -> bool {
758+
let mut b = a.clone();
759+
b.dedup();
760+
itertools::equal(
761+
&b,
762+
a
763+
.iter()
764+
.coalesce(|x, y| {
765+
if x==y {
766+
Ok(x)
767+
} else {
768+
Err((x, y))
769+
}
770+
})
771+
.fold(vec![], |mut v, n| {
772+
v.push(n);
773+
v
774+
})
775+
)
776+
}
777+
}
778+
756779
quickcheck! {
757780
fn equal_dedup(a: Vec<i32>) -> bool {
758781
let mut b = a.clone();

tests/test_std.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ fn dedup() {
109109
assert_eq!(&xs_d, &ys);
110110
}
111111

112+
#[test]
113+
fn coalesce() {
114+
let data = vec![-1., -2., -3., 3., 1., 0., -1.];
115+
let it = data.iter().cloned().coalesce(|x, y|
116+
if (x >= 0.) == (y >= 0.) {
117+
Ok(x + y)
118+
} else {
119+
Err((x, y))
120+
}
121+
);
122+
itertools::assert_equal(it.clone(), vec![-6., 4., -1.]);
123+
assert_eq!(
124+
it.fold(vec![], |mut v, n| {
125+
v.push(n);
126+
v
127+
}),
128+
vec![-6., 4., -1.]
129+
);
130+
}
131+
112132
#[test]
113133
fn dedup_by() {
114134
let xs = [(0, 0), (0, 1), (1, 1), (2, 1), (0, 2), (3, 1), (0, 3), (1, 3)];

0 commit comments

Comments
 (0)