Skip to content

Commit 30f8491

Browse files
authored
Fix warning when testing without the allocator_api cargo feature (#245)
1 parent 4eeab88 commit 30f8491

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

tests/all/allocator_api.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
#![cfg(feature = "allocator_api")]
22

33
use crate::quickcheck;
4-
use crate::quickcheck::arbitrary_layout;
54
use bumpalo::Bump;
65
use std::alloc::{AllocError, Allocator, Layout};
76
use std::ptr::NonNull;
87
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
98

9+
/// Map an arbitrary `x` to a power of 2 that is less than or equal to `max`,
10+
/// but with as little bias as possible (eg rounding `min(x, max)` to the
11+
/// nearest power of 2 is unacceptable because it would majorly bias `max` for
12+
/// small values of `max`).
13+
fn clamp_to_pow2_in_range(x: usize, max: usize) -> usize {
14+
let log_x = max.ilog2() as usize;
15+
if log_x == 0 {
16+
return 1;
17+
}
18+
let divisor = usize::MAX / log_x;
19+
let y = 1_usize << (x / divisor);
20+
assert!(y.is_power_of_two(), "{y} is not a power of two");
21+
assert!(y <= max, "{y} is larger than {max}");
22+
y
23+
}
24+
25+
/// Helper to turn a pair of arbitrary `usize`s into a valid `Layout` of
26+
/// reasonable size for use with quickchecks.
27+
pub fn arbitrary_layout(size: usize, align: usize) -> Layout {
28+
const MAX_ALIGN: usize = 64;
29+
const MAX_SIZE: usize = 1024;
30+
31+
let align = clamp_to_pow2_in_range(align, MAX_ALIGN);
32+
33+
let size = size % (MAX_SIZE + 1);
34+
let size = size.next_multiple_of(align);
35+
36+
Layout::from_size_align(size, align).unwrap()
37+
}
38+
1039
#[derive(Debug)]
1140
struct AllocatorDebug {
1241
bump: Bump,

tests/all/quickcheck.rs

-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::alloc::Layout;
2-
31
/// A redefinition/wrapper macro of `quickcheck::quickcheck!` that supports
42
/// limiting the number of test iterations to one when we are running under
53
/// MIRI.
@@ -43,33 +41,3 @@ macro_rules! quickcheck {
4341
)*
4442
};
4543
}
46-
47-
/// Map an arbitrary `x` to a power of 2 that is less than or equal to `max`,
48-
/// but with as little bias as possible (eg rounding `min(x, max)` to the
49-
/// nearest power of 2 is unacceptable because it would majorly bias `max` for
50-
/// small values of `max`).
51-
fn clamp_to_pow2_in_range(x: usize, max: usize) -> usize {
52-
let log_x = max.ilog2() as usize;
53-
if log_x == 0 {
54-
return 1;
55-
}
56-
let divisor = usize::MAX / log_x;
57-
let y = 1_usize << (x / divisor);
58-
assert!(y.is_power_of_two(), "{y} is not a power of two");
59-
assert!(y <= max, "{y} is larger than {max}");
60-
y
61-
}
62-
63-
/// Helper to turn a pair of arbitrary `usize`s into a valid `Layout` of
64-
/// reasonable size for use with quickchecks.
65-
pub fn arbitrary_layout(size: usize, align: usize) -> Layout {
66-
const MAX_ALIGN: usize = 64;
67-
const MAX_SIZE: usize = 1024;
68-
69-
let align = clamp_to_pow2_in_range(align, MAX_ALIGN);
70-
71-
let size = size % (MAX_SIZE + 1);
72-
let size = size.next_multiple_of(align);
73-
74-
Layout::from_size_align(size, align).unwrap()
75-
}

0 commit comments

Comments
 (0)