Skip to content

Commit d232918

Browse files
bors[bot]SkiFire13
andauthored
Merge #574
574: Fix and refactor specializations tests r=phimuemue a=SkiFire13 As found in #563 specializations tests don't actually test the specialize tests due to the use of `Box<dyn Iterator>`. This PR fix this problem by having `check_specialized` be a macro so that the code inside (what used to be the) closure directly calls methods on the iterator types. This also does a tiny refactor, moving the single test in `tests/fold_specialization.rs` together with the others in `tests/specializations.rs`. Note that this may conflict with #563 (but I hope it doesn't since they touch different parts of the file) Co-authored-by: Giacomo Stevanato <giaco.stevanato@gmail.com>
2 parents a139571 + bf4ca16 commit d232918

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

tests/fold_specialization.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/specializations.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ where
1515
}
1616
}
1717

18-
fn check_specialized<'a, V, IterItem, Iter, F>(iterator: &Iter, mapper: F)
19-
where
20-
V: Eq + Debug,
21-
Iter: Iterator<Item = IterItem> + Clone + 'a,
22-
F: Fn(Box<dyn Iterator<Item = IterItem> + 'a>) -> V,
23-
{
24-
assert_eq!(
25-
mapper(Box::new(Unspecialized(iterator.clone()))),
26-
mapper(Box::new(iterator.clone()))
27-
)
18+
macro_rules! check_specialized {
19+
($src:expr, |$it:pat| $closure:expr) => {
20+
let $it = $src.clone();
21+
let v1 = $closure;
22+
23+
let $it = Unspecialized($src.clone());
24+
let v2 = $closure;
25+
26+
assert_eq!(v1, v2);
27+
}
2828
}
2929

3030
fn test_specializations<IterItem, Iter>(
@@ -33,10 +33,10 @@ fn test_specializations<IterItem, Iter>(
3333
IterItem: Eq + Debug + Clone,
3434
Iter: Iterator<Item = IterItem> + Clone,
3535
{
36-
check_specialized(it, |i| i.count());
37-
check_specialized(it, |i| i.last());
38-
check_specialized(it, |i| i.collect::<Vec<_>>());
39-
check_specialized(it, |i| {
36+
check_specialized!(it, |i| i.count());
37+
check_specialized!(it, |i| i.last());
38+
check_specialized!(it, |i| i.collect::<Vec<_>>());
39+
check_specialized!(it, |i| {
4040
let mut parameters_from_fold = vec![];
4141
let fold_result = i.fold(vec![], |mut acc, v: IterItem| {
4242
parameters_from_fold.push((acc.clone(), v.clone()));
@@ -45,7 +45,7 @@ fn test_specializations<IterItem, Iter>(
4545
});
4646
(parameters_from_fold, fold_result)
4747
});
48-
check_specialized(it, |mut i| {
48+
check_specialized!(it, |mut i| {
4949
let mut parameters_from_all = vec![];
5050
let first = i.next();
5151
let all_result = i.all(|x| {
@@ -56,7 +56,7 @@ fn test_specializations<IterItem, Iter>(
5656
});
5757
let size = it.clone().count();
5858
for n in 0..size + 2 {
59-
check_specialized(it, |mut i| i.nth(n));
59+
check_specialized!(it, |mut i| i.nth(n));
6060
}
6161
// size_hint is a bit harder to check
6262
let mut it_sh = it.clone();
@@ -72,6 +72,12 @@ fn test_specializations<IterItem, Iter>(
7272
}
7373
}
7474

75+
quickcheck! {
76+
fn intersperse(v: Vec<u8>) -> () {
77+
test_specializations(&v.into_iter().intersperse(0));
78+
}
79+
}
80+
7581
quickcheck! {
7682
fn put_back_qc(test_vec: Vec<i32>) -> () {
7783
test_specializations(&itertools::put_back(test_vec.iter()));

0 commit comments

Comments
 (0)