|
| 1 | +use paste; |
1 | 2 | use permutohedron;
|
| 3 | +use quickcheck as qc; |
| 4 | +use rand::{distributions::{Distribution, Standard}, Rng, SeedableRng, rngs::StdRng}; |
| 5 | +use rand::{seq::SliceRandom, thread_rng}; |
| 6 | +use std::{cmp::min, fmt::Debug, marker::PhantomData}; |
2 | 7 | use itertools as it;
|
3 | 8 | use crate::it::Itertools;
|
4 | 9 | use crate::it::ExactlyOneError;
|
@@ -374,6 +379,88 @@ fn sorted_by() {
|
374 | 379 | it::assert_equal(v, vec![4, 3, 2, 1, 0]);
|
375 | 380 | }
|
376 | 381 |
|
| 382 | +qc::quickcheck! { |
| 383 | + fn k_smallest_range(n: u64, m: u16, k: u16) -> () { |
| 384 | + // u16 is used to constrain k and m to 0..2¹⁶, |
| 385 | + // otherwise the test could use too much memory. |
| 386 | + let (k, m) = (k as u64, m as u64); |
| 387 | + |
| 388 | + // Generate a random permutation of n..n+m |
| 389 | + let i = { |
| 390 | + let mut v: Vec<u64> = (n..n.saturating_add(m)).collect(); |
| 391 | + v.shuffle(&mut thread_rng()); |
| 392 | + v.into_iter() |
| 393 | + }; |
| 394 | + |
| 395 | + // Check that taking the k smallest elements yields n..n+min(k, m) |
| 396 | + it::assert_equal( |
| 397 | + i.k_smallest(k as usize), |
| 398 | + n..n.saturating_add(min(k, m)) |
| 399 | + ); |
| 400 | + } |
| 401 | +} |
| 402 | + |
| 403 | +#[derive(Clone, Debug)] |
| 404 | +struct RandIter<T: 'static + Clone + Send, R: 'static + Clone + Rng + SeedableRng + Send = StdRng> { |
| 405 | + idx: usize, |
| 406 | + len: usize, |
| 407 | + rng: R, |
| 408 | + _t: PhantomData<T> |
| 409 | +} |
| 410 | + |
| 411 | +impl<T: Clone + Send, R: Clone + Rng + SeedableRng + Send> Iterator for RandIter<T, R> |
| 412 | +where Standard: Distribution<T> { |
| 413 | + type Item = T; |
| 414 | + fn next(&mut self) -> Option<T> { |
| 415 | + if self.idx == self.len { |
| 416 | + None |
| 417 | + } else { |
| 418 | + self.idx += 1; |
| 419 | + Some(self.rng.gen()) |
| 420 | + } |
| 421 | + } |
| 422 | +} |
| 423 | + |
| 424 | +impl<T: Clone + Send, R: Clone + Rng + SeedableRng + Send> qc::Arbitrary for RandIter<T, R> { |
| 425 | + fn arbitrary<G: qc::Gen>(g: &mut G) -> Self { |
| 426 | + RandIter { |
| 427 | + idx: 0, |
| 428 | + len: g.size(), |
| 429 | + rng: R::seed_from_u64(g.next_u64()), |
| 430 | + _t : PhantomData{}, |
| 431 | + } |
| 432 | + } |
| 433 | +} |
| 434 | + |
| 435 | +// Check that taking the k smallest is the same as |
| 436 | +// sorting then taking the k first elements |
| 437 | +fn k_smallest_sort<I>(i: I, k: u16) -> () |
| 438 | +where |
| 439 | + I: Iterator + Clone, |
| 440 | + I::Item: Ord + Debug, |
| 441 | +{ |
| 442 | + let j = i.clone(); |
| 443 | + let k = k as usize; |
| 444 | + it::assert_equal( |
| 445 | + i.k_smallest(k), |
| 446 | + j.sorted().take(k) |
| 447 | + ) |
| 448 | +} |
| 449 | + |
| 450 | +macro_rules! generic_test { |
| 451 | + ($f:ident, $($t:ty),+) => { |
| 452 | + $(paste::item! { |
| 453 | + qc::quickcheck! { |
| 454 | + fn [< $f _ $t >](i: RandIter<$t>, k: u16) -> () { |
| 455 | + $f(i, k) |
| 456 | + } |
| 457 | + } |
| 458 | + })+ |
| 459 | + }; |
| 460 | +} |
| 461 | + |
| 462 | +generic_test!(k_smallest_sort, u8, u16, u32, u64, i8, i16, i32, i64); |
| 463 | + |
377 | 464 | #[test]
|
378 | 465 | fn sorted_by_key() {
|
379 | 466 | let sc = [3, 4, 1, 2].iter().cloned().sorted_by_key(|&x| x);
|
@@ -407,7 +494,6 @@ fn test_multipeek() {
|
407 | 494 | assert_eq!(mp.next(), Some(5));
|
408 | 495 | assert_eq!(mp.next(), None);
|
409 | 496 | assert_eq!(mp.peek(), None);
|
410 |
| - |
411 | 497 | }
|
412 | 498 |
|
413 | 499 | #[test]
|
|
0 commit comments