Skip to content

Commit 036a451

Browse files
committed
Add generic test comparing k_smallest(k) and sorted().take(k)
Instantiate the test over all unsigned integer types
1 parent 7aadb1e commit 036a451

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/test_std.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,35 @@ impl<T: Clone + Send, R: Clone + Rng + SeedableRng + Send> qc::Arbitrary for Ran
407407
}
408408
}
409409

410+
// Check that taking the k smallest is the same as
411+
// sorting then taking the k first elements
412+
fn k_smallest_sort<I>(i: I, k: usize) -> ()
413+
where
414+
I: Iterator + Clone,
415+
I::Item: Ord + Debug,
416+
{
417+
let j = i.clone();
418+
it::assert_equal(
419+
i.k_smallest(k),
420+
j.sorted().take(k)
421+
)
422+
}
423+
424+
qc::quickcheck! {
425+
fn k_smallest_sort_u8(i: RandIter<u8>, k: u16) -> () {
426+
k_smallest_sort(i, k)
427+
}
428+
fn k_smallest_sort_u16(i: RandIter<u16>, k: u16) -> () {
429+
k_smallest_sort(i, k)
430+
}
431+
fn k_smallest_sort_u32(i: RandIter<u32>, k: u16) -> () {
432+
k_smallest_sort(i, k)
433+
}
434+
fn k_smallest_sort_u64(i: RandIter<u64>, k: u16) -> () {
435+
k_smallest_sort(i, k)
436+
}
437+
}
438+
410439
#[test]
411440
fn sorted_by_key() {
412441
let sc = [3, 4, 1, 2].iter().cloned().sorted_by_key(|&x| x);

0 commit comments

Comments
 (0)