Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and refactor specializations tests #574

Merged
merged 2 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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