Skip to content

Commit f31b617

Browse files
committed
test_std::k_smallest: Avoid allocating too much memory
By limiting k and m to be u16, we use at most 2¹⁶ sizeof(u64) = 512 kiB for each allocated array and heap (at most 1MiB total for k_smallest_range)
1 parent 900f1e8 commit f31b617

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tests/test_std.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,11 @@ fn sorted_by() {
359359
}
360360

361361
qc::quickcheck! {
362-
fn k_smallest_range(n: u64, m: u64, k: u64) -> () {
362+
fn k_smallest_range(n: u64, m: u16, k: u16) -> () {
363+
// u16 is used to constrain k and m to 0..2¹⁶,
364+
// otherwise the test could use too much memory.
365+
let (k, m) = (k as u64, m as u64);
366+
363367
// Generate a random permutation of n..n+m
364368
let i = {
365369
let mut v: Vec<u64> = (n..n.saturating_add(m)).collect();
@@ -409,12 +413,13 @@ impl<T: Clone + Send, R: Clone + Rng + SeedableRng + Send> qc::Arbitrary for Ran
409413

410414
// Check that taking the k smallest is the same as
411415
// sorting then taking the k first elements
412-
fn k_smallest_sort<I>(i: I, k: usize) -> ()
416+
fn k_smallest_sort<I>(i: I, k: u16) -> ()
413417
where
414418
I: Iterator + Clone,
415419
I::Item: Ord + Debug,
416420
{
417421
let j = i.clone();
422+
let k = k as usize;
418423
it::assert_equal(
419424
i.k_smallest(k),
420425
j.sorted().take(k)

0 commit comments

Comments
 (0)