Skip to content

Commit 7aadb1e

Browse files
committed
tests::test_std: Support generating random iterators with quickcheck
1 parent d292fa0 commit 7aadb1e

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

tests/test_std.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use permutohedron;
2-
use quickcheck::quickcheck;
2+
use quickcheck as qc;
3+
use rand::{distributions::{Distribution, Standard}, Rng, SeedableRng, rngs::StdRng};
34
use rand::{seq::SliceRandom, thread_rng};
4-
use std::cmp::min;
5+
use std::{cmp::min, fmt::Debug, marker::PhantomData};
56
use itertools as it;
67
use crate::it::Itertools;
78
use crate::it::ExactlyOneError;
@@ -357,7 +358,7 @@ fn sorted_by() {
357358
it::assert_equal(v, vec![4, 3, 2, 1, 0]);
358359
}
359360

360-
quickcheck! {
361+
qc::quickcheck! {
361362
fn k_smallest_range(n: u64, m: u64, k: u64) -> () {
362363
// Generate a random permutation of n..n+m
363364
let i = {
@@ -374,6 +375,38 @@ quickcheck! {
374375
}
375376
}
376377

378+
#[derive(Clone, Debug)]
379+
struct RandIter<T: 'static + Clone + Send, R: 'static + Clone + Rng + SeedableRng + Send = StdRng> {
380+
idx: usize,
381+
len: usize,
382+
rng: R,
383+
_t: PhantomData<T>
384+
}
385+
386+
impl<T: Clone + Send, R: Clone + Rng + SeedableRng + Send> Iterator for RandIter<T, R>
387+
where Standard: Distribution<T> {
388+
type Item = T;
389+
fn next(&mut self) -> Option<T> {
390+
if self.idx == self.len {
391+
None
392+
} else {
393+
self.idx += 1;
394+
Some(self.rng.gen())
395+
}
396+
}
397+
}
398+
399+
impl<T: Clone + Send, R: Clone + Rng + SeedableRng + Send> qc::Arbitrary for RandIter<T, R> {
400+
fn arbitrary<G: qc::Gen>(g: &mut G) -> Self {
401+
RandIter {
402+
idx: 0,
403+
len: g.size(),
404+
rng: R::seed_from_u64(g.next_u64()),
405+
_t : PhantomData{},
406+
}
407+
}
408+
}
409+
377410
#[test]
378411
fn sorted_by_key() {
379412
let sc = [3, 4, 1, 2].iter().cloned().sorted_by_key(|&x| x);

0 commit comments

Comments
 (0)