Skip to content
This repository has been archived by the owner. It is now read-only.

quickcheck test for Combinations::nth specialization #1

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::default::Default;
use quickcheck as qc;
use std::ops::Range;
use std::cmp::Ordering;
use std::panic::catch_unwind;
use itertools::Itertools;
use itertools::{
multizip,
Expand All @@ -33,6 +34,25 @@ use rand::Rng;
use rand::seq::SliceRandom;
use quickcheck::TestResult;

/// An unspecialized wrapper around another iterator.
struct Unspecialized<I>(I);

impl<I> Iterator for Unspecialized<I>
where I: Iterator
{
type Item = I::Item;

#[inline(always)]
fn next(&mut self) -> Option<I::Item> {
self.0.next()
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}

/// Trait for size hint modifier types
trait HintKind: Copy + Send + qc::Arbitrary {
fn loosen_bounds(&self, org_hint: (usize, Option<usize>)) -> (usize, Option<usize>);
Expand Down Expand Up @@ -1039,3 +1059,28 @@ quickcheck! {
}
}
}

quickcheck! {
// check that the specialization of `Combinations::nth` produces the same
// results as the unspecialized version.
fn combinations_nth(l: u8, n: u8, i: u8) -> TestResult {
let (l, n, i) = (l as usize, n as usize, i as usize);

let s_ith = catch_unwind(|| {
(0..l).combinations(n).nth(i)
}).map_err(|_| "PANICKED");

let u_ith = catch_unwind(|| {
Unspecialized((0..l).combinations(n as usize)).nth(i)
}).map_err(|_| "PANICKED");

if s_ith == u_ith {
TestResult::passed()
} else {
TestResult::error(
format!(
"(0..{}).combinations({}).nth({}): {:?} != {:?}",
l, n, i, s_ith, u_ith))
}
}
}