Skip to content

Commit f794151

Browse files
bonziniBennoLossin
authored andcommitted
tests: properly conditionalize tests that require allocator_api
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent fc5e064 commit f794151

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

examples/linked_list.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![feature(allocator_api)]
1+
#![cfg_attr(feature = "alloc", feature(allocator_api))]
2+
23
use core::{
34
cell::Cell,
45
convert::Infallible,

examples/mutex.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![feature(allocator_api)]
1+
#![cfg_attr(feature = "alloc", feature(allocator_api))]
2+
23
use core::{
34
cell::{Cell, UnsafeCell},
45
marker::PhantomPinned,
@@ -161,8 +162,12 @@ impl WaitEntry {
161162
}
162163
}
163164

165+
#[cfg(not(feature = "alloc"))]
166+
fn main() {}
167+
164168
#[allow(dead_code)]
165169
#[cfg_attr(test, test)]
170+
#[cfg(feature = "alloc")]
166171
fn main() {
167172
let mtx: Pin<Arc<CMutex<usize>>> = Arc::pin_init(CMutex::new(0)).unwrap();
168173
let mut handles = vec![];

examples/pthread_mutex.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// inspired by https://github.com/nbdd0121/pin-init/blob/trunk/examples/pthread_mutex.rs
2-
#![feature(allocator_api)]
2+
#![cfg_attr(feature = "alloc", feature(allocator_api))]
33
#[cfg(not(windows))]
44
mod pthread_mtx {
5+
#[cfg(feature = "alloc")]
6+
use core::alloc::AllocError;
57
use core::{
6-
alloc::AllocError,
78
cell::UnsafeCell,
89
marker::PhantomPinned,
910
mem::MaybeUninit,
@@ -45,6 +46,8 @@ mod pthread_mtx {
4546
match e {}
4647
}
4748
}
49+
50+
#[cfg(feature = "alloc")]
4851
impl From<AllocError> for Error {
4952
fn from(_: AllocError) -> Self {
5053
Self::Alloc
@@ -129,7 +132,7 @@ mod pthread_mtx {
129132

130133
#[cfg_attr(test, test)]
131134
fn main() {
132-
#[cfg(not(windows))]
135+
#[cfg(all(feature = "alloc", not(windows)))]
133136
{
134137
use core::pin::Pin;
135138
use pinned_init::*;

examples/static_init.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(allocator_api)]
1+
#![cfg_attr(feature = "alloc", feature(allocator_api))]
22

33
use core::{
44
cell::{Cell, UnsafeCell},
@@ -77,6 +77,10 @@ unsafe impl PinInit<CMutex<usize>> for CountInit {
7777

7878
pub static COUNT: StaticInit<CMutex<usize>, CountInit> = StaticInit::new(CountInit);
7979

80+
#[cfg(not(feature = "alloc"))]
81+
fn main() {}
82+
83+
#[cfg(feature = "alloc")]
8084
fn main() {
8185
let mtx: Pin<Arc<CMutex<usize>>> = Arc::pin_init(CMutex::new(0)).unwrap();
8286
let mut handles = vec![];

tests/alloc_fail.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
#![feature(allocator_api)]
1+
#![cfg_attr(feature = "alloc", feature(allocator_api))]
22

3-
use core::{alloc::AllocError, convert::Infallible};
3+
#[cfg(feature = "alloc")]
4+
use core::alloc::AllocError;
5+
6+
use core::convert::Infallible;
47
use pinned_init::*;
58
use std::sync::Arc;
69

710
#[path = "./ring_buf.rs"]
811
mod ring_buf;
912
use ring_buf::*;
1013

11-
#[cfg(all(not(miri), not(NO_ALLOC_FAIL_TESTS), not(target_os = "macos")))]
14+
#[cfg(all(
15+
feature = "alloc",
16+
not(miri),
17+
not(NO_ALLOC_FAIL_TESTS),
18+
not(target_os = "macos")
19+
))]
1220
#[test]
1321
fn too_big_pinned() {
1422
// should be too big with current hardware.
@@ -23,7 +31,12 @@ fn too_big_pinned() {
2331
));
2432
}
2533

26-
#[cfg(all(not(miri), not(NO_ALLOC_FAIL_TESTS), not(target_os = "macos")))]
34+
#[cfg(all(
35+
feature = "alloc",
36+
not(miri),
37+
not(NO_ALLOC_FAIL_TESTS),
38+
not(target_os = "macos")
39+
))]
2740
#[test]
2841
fn too_big_in_place() {
2942
// should be too big with current hardware.

tests/ring_buf.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ fn even_stack() {
192192
assert_eq!(val, Err(()));
193193
}
194194

195+
#[cfg(feature = "alloc")]
195196
#[test]
196197
fn even_failing() {
197198
assert!(matches!(Box::try_pin_init(EvenU64::new2(3)), Err(Error)));
199+
assert!(matches!(Box::try_init(EvenU64::new2(3)), Err(Error)));
198200
assert!(matches!(Arc::try_pin_init(EvenU64::new2(5)), Err(Error)));
199201
assert!(matches!(Box::try_init(EvenU64::new2(3)), Err(Error)));
200202
assert!(matches!(Arc::try_init(EvenU64::new2(5)), Err(Error)));
@@ -241,7 +243,7 @@ struct BigStruct {
241243
oth: MaybeUninit<u8>,
242244
}
243245

244-
#[cfg(not(miri))]
246+
#[cfg(all(feature = "alloc", not(miri)))]
245247
#[test]
246248
fn big_struct() {
247249
let x = Arc::init(init!(BigStruct {
@@ -256,7 +258,7 @@ fn big_struct() {
256258
println!("{x:?}");
257259
}
258260

259-
#[cfg(not(miri))]
261+
#[cfg(all(feature = "alloc", not(miri)))]
260262
#[test]
261263
fn with_big_struct() {
262264
let buf = Arc::pin_init(CMutex::new(RingBuffer::<BigStruct, 64>::new())).unwrap();

0 commit comments

Comments
 (0)