Skip to content

Commit

Permalink
Added quickchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ten0 committed Dec 26, 2019
1 parent a88946d commit a0eb01c
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions tests/specializations.rs
@@ -1,5 +1,8 @@
extern crate itertools;

#[macro_use]
extern crate quickcheck;

use itertools::{EitherOrBoth, Itertools};

use std::fmt::Debug;
Expand Down Expand Up @@ -36,13 +39,17 @@ where
)
}

fn check_specialized_count_last_nth_sizeh<'a, IterItem, Iter>(it: &Iter, expected_size: usize)
where
fn check_specialized_count_last_nth_sizeh<'a, IterItem, Iter>(
it: &Iter,
known_expected_size: Option<usize>,
) where
IterItem: 'a + Eq + Debug,
Iter: Iterator<Item = IterItem> + Clone + 'a,
{
let size = it.clone().count();
assert_eq!(size, expected_size);
if let Some(expected_size) = known_expected_size {
assert_eq!(size, expected_size);
}
check_specialized(it, |i| i.count());
check_specialized(it, |i| i.last());
for n in 0..size + 2 {
Expand Down Expand Up @@ -79,28 +86,36 @@ where
});
}

#[test]
fn put_back() {
let test_vec = vec![7, 4, 1];
fn put_back_test(test_vec: Vec<i32>, known_expected_size: Option<usize>) {
{
// Lexical lifetimes support
let pb = itertools::put_back(test_vec.iter());
check_specialized_count_last_nth_sizeh(&pb, 3);
check_specialized_count_last_nth_sizeh(&pb, known_expected_size);
check_specialized_fold_xor(&pb);
}

let mut pb = itertools::put_back(test_vec.into_iter());
pb.put_back(1);
check_specialized_count_last_nth_sizeh(&pb, 4);
check_specialized_fold_xor(&pb);
check_specialized_count_last_nth_sizeh(&pb, known_expected_size.map(|x| x + 1));
check_specialized_fold_xor(&pb)
}

#[test]
fn merge_join_by() {
let i1 = vec![1, 3, 5, 7, 8, 9].into_iter();
let i2 = vec![0, 3, 4, 5].into_iter();
fn put_back() {
put_back_test(vec![7, 4, 1], Some(3));
}

quickcheck! {
fn put_back_qc(test_vec: Vec<i32>) -> () {
put_back_test(test_vec, None)
}
}

fn merge_join_by_test(i1: Vec<usize>, i2: Vec<usize>, known_expected_size: Option<usize>) {
let i1 = i1.into_iter();
let i2 = i2.into_iter();
let mjb = i1.clone().merge_join_by(i2.clone(), std::cmp::Ord::cmp);
check_specialized_count_last_nth_sizeh(&mjb, 8);
check_specialized_count_last_nth_sizeh(&mjb, known_expected_size);
// Rust 1.24 compatibility:
fn eob_left_z(eob: EitherOrBoth<usize, usize>) -> usize {
eob.left().unwrap_or(0)
Expand All @@ -117,10 +132,23 @@ fn merge_join_by() {
check_specialized_fold_xor(&mjb.clone().map(eob_right_z));
check_specialized_fold_xor(&mjb.clone().map(eob_both_z));

// And the other way around
// And the other way around
let mjb = i2.merge_join_by(i1, std::cmp::Ord::cmp);
check_specialized_count_last_nth_sizeh(&mjb, 8);
check_specialized_count_last_nth_sizeh(&mjb, known_expected_size);
check_specialized_fold_xor(&mjb.clone().map(eob_left_z));
check_specialized_fold_xor(&mjb.clone().map(eob_right_z));
check_specialized_fold_xor(&mjb.clone().map(eob_both_z));
}

#[test]
fn merge_join_by() {
let i1 = vec![1, 3, 5, 7, 8, 9];
let i2 = vec![0, 3, 4, 5];
merge_join_by_test(i1, i2, Some(8));
}

quickcheck! {
fn merge_join_by_qc(i1: Vec<usize>, i2: Vec<usize>) -> () {
merge_join_by_test(i1, i2, None)
}
}

0 comments on commit a0eb01c

Please sign in to comment.