Skip to content

Commit

Permalink
Fix warning when testing without the allocator_api cargo feature (#245
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fitzgen committed Apr 14, 2024
1 parent 4eeab88 commit 30f8491
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
31 changes: 30 additions & 1 deletion tests/all/allocator_api.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
#![cfg(feature = "allocator_api")]

use crate::quickcheck;
use crate::quickcheck::arbitrary_layout;
use bumpalo::Bump;
use std::alloc::{AllocError, Allocator, Layout};
use std::ptr::NonNull;
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};

/// Map an arbitrary `x` to a power of 2 that is less than or equal to `max`,
/// but with as little bias as possible (eg rounding `min(x, max)` to the
/// nearest power of 2 is unacceptable because it would majorly bias `max` for
/// small values of `max`).
fn clamp_to_pow2_in_range(x: usize, max: usize) -> usize {
let log_x = max.ilog2() as usize;
if log_x == 0 {
return 1;
}
let divisor = usize::MAX / log_x;
let y = 1_usize << (x / divisor);
assert!(y.is_power_of_two(), "{y} is not a power of two");
assert!(y <= max, "{y} is larger than {max}");
y
}

/// Helper to turn a pair of arbitrary `usize`s into a valid `Layout` of
/// reasonable size for use with quickchecks.
pub fn arbitrary_layout(size: usize, align: usize) -> Layout {
const MAX_ALIGN: usize = 64;
const MAX_SIZE: usize = 1024;

let align = clamp_to_pow2_in_range(align, MAX_ALIGN);

let size = size % (MAX_SIZE + 1);
let size = size.next_multiple_of(align);

Layout::from_size_align(size, align).unwrap()
}

#[derive(Debug)]
struct AllocatorDebug {
bump: Bump,
Expand Down
32 changes: 0 additions & 32 deletions tests/all/quickcheck.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::alloc::Layout;

/// A redefinition/wrapper macro of `quickcheck::quickcheck!` that supports
/// limiting the number of test iterations to one when we are running under
/// MIRI.
Expand Down Expand Up @@ -43,33 +41,3 @@ macro_rules! quickcheck {
)*
};
}

/// Map an arbitrary `x` to a power of 2 that is less than or equal to `max`,
/// but with as little bias as possible (eg rounding `min(x, max)` to the
/// nearest power of 2 is unacceptable because it would majorly bias `max` for
/// small values of `max`).
fn clamp_to_pow2_in_range(x: usize, max: usize) -> usize {
let log_x = max.ilog2() as usize;
if log_x == 0 {
return 1;
}
let divisor = usize::MAX / log_x;
let y = 1_usize << (x / divisor);
assert!(y.is_power_of_two(), "{y} is not a power of two");
assert!(y <= max, "{y} is larger than {max}");
y
}

/// Helper to turn a pair of arbitrary `usize`s into a valid `Layout` of
/// reasonable size for use with quickchecks.
pub fn arbitrary_layout(size: usize, align: usize) -> Layout {
const MAX_ALIGN: usize = 64;
const MAX_SIZE: usize = 1024;

let align = clamp_to_pow2_in_range(align, MAX_ALIGN);

let size = size % (MAX_SIZE + 1);
let size = size.next_multiple_of(align);

Layout::from_size_align(size, align).unwrap()
}

0 comments on commit 30f8491

Please sign in to comment.