Skip to content

Commit

Permalink
Merge #574
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and SkiFire13 committed Aug 20, 2021
2 parents a139571 + bf4ca16 commit d232918
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
13 changes: 0 additions & 13 deletions tests/fold_specialization.rs

This file was deleted.

38 changes: 22 additions & 16 deletions tests/specializations.rs
Expand Up @@ -15,16 +15,16 @@ where
}
}

fn check_specialized<'a, V, IterItem, Iter, F>(iterator: &Iter, mapper: F)
where
V: Eq + Debug,
Iter: Iterator<Item = IterItem> + Clone + 'a,
F: Fn(Box<dyn Iterator<Item = IterItem> + 'a>) -> V,
{
assert_eq!(
mapper(Box::new(Unspecialized(iterator.clone()))),
mapper(Box::new(iterator.clone()))
)
macro_rules! check_specialized {
($src:expr, |$it:pat| $closure:expr) => {
let $it = $src.clone();
let v1 = $closure;

let $it = Unspecialized($src.clone());
let v2 = $closure;

assert_eq!(v1, v2);
}
}

fn test_specializations<IterItem, Iter>(
Expand All @@ -33,10 +33,10 @@ fn test_specializations<IterItem, Iter>(
IterItem: Eq + Debug + Clone,
Iter: Iterator<Item = IterItem> + Clone,
{
check_specialized(it, |i| i.count());
check_specialized(it, |i| i.last());
check_specialized(it, |i| i.collect::<Vec<_>>());
check_specialized(it, |i| {
check_specialized!(it, |i| i.count());
check_specialized!(it, |i| i.last());
check_specialized!(it, |i| i.collect::<Vec<_>>());
check_specialized!(it, |i| {
let mut parameters_from_fold = vec![];
let fold_result = i.fold(vec![], |mut acc, v: IterItem| {
parameters_from_fold.push((acc.clone(), v.clone()));
Expand All @@ -45,7 +45,7 @@ fn test_specializations<IterItem, Iter>(
});
(parameters_from_fold, fold_result)
});
check_specialized(it, |mut i| {
check_specialized!(it, |mut i| {
let mut parameters_from_all = vec![];
let first = i.next();
let all_result = i.all(|x| {
Expand All @@ -56,7 +56,7 @@ fn test_specializations<IterItem, Iter>(
});
let size = it.clone().count();
for n in 0..size + 2 {
check_specialized(it, |mut i| i.nth(n));
check_specialized!(it, |mut i| i.nth(n));
}
// size_hint is a bit harder to check
let mut it_sh = it.clone();
Expand All @@ -72,6 +72,12 @@ fn test_specializations<IterItem, Iter>(
}
}

quickcheck! {
fn intersperse(v: Vec<u8>) -> () {
test_specializations(&v.into_iter().intersperse(0));
}
}

quickcheck! {
fn put_back_qc(test_vec: Vec<i32>) -> () {
test_specializations(&itertools::put_back(test_vec.iter()));
Expand Down

0 comments on commit d232918

Please sign in to comment.