Skip to content

Commit be3fe80

Browse files
bjorn3gitbot
authored and
gitbot
committed
Move some alloc tests to the alloctests crate
Unit tests directly inside of standard library crates require a very fragile way of building that is hard to reproduce outside of bootstrap.
1 parent a84e8c8 commit be3fe80

File tree

14 files changed

+123
-27
lines changed

14 files changed

+123
-27
lines changed

alloc/src/alloc.rs

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ use core::hint;
1010
#[cfg(not(test))]
1111
use core::ptr::{self, NonNull};
1212

13-
#[cfg(test)]
14-
mod tests;
15-
1613
extern "Rust" {
1714
// These are the magic symbols to call the global allocator. rustc generates
1815
// them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute

alloc/src/collections/binary_heap/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,6 @@ use crate::collections::TryReserveError;
155155
use crate::slice;
156156
use crate::vec::{self, AsVecIntoIter, Vec};
157157

158-
#[cfg(test)]
159-
mod tests;
160-
161158
/// A priority queue implemented with a binary heap.
162159
///
163160
/// This will be a max-heap.

alloc/src/ffi/c_str.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//! [`CString`] and its related types.
22
3-
#[cfg(test)]
4-
mod tests;
5-
63
use core::borrow::Borrow;
74
use core::ffi::{CStr, c_char};
85
use core::num::NonZero;

alloc/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ pub mod string;
239239
pub mod sync;
240240
#[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync)))]
241241
pub mod task;
242-
#[cfg(test)]
243-
mod tests;
244242
pub mod vec;
245243

246244
#[doc(hidden)]

alloc/src/sync.rs

-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ use crate::string::String;
3939
#[cfg(not(no_global_oom_handling))]
4040
use crate::vec::Vec;
4141

42-
#[cfg(test)]
43-
mod tests;
44-
4542
/// A soft limit on the amount of references that may be made to an `Arc`.
4643
///
4744
/// Going above this limit will abort your program (although not

alloc/src/alloc/tests.rs alloc/tests/alloc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use super::*;
1+
use alloc::alloc::*;
2+
use alloc::boxed::Box;
23

34
extern crate test;
45
use test::Bencher;
56

6-
use crate::boxed::Box;
7-
87
#[test]
98
fn allocate_zeroed() {
109
unsafe {

alloc/src/ffi/c_str/tests.rs alloc/tests/c_str2.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use alloc::ffi::CString;
2+
use alloc::rc::Rc;
3+
use alloc::sync::Arc;
14
use core::assert_matches::assert_matches;
2-
use core::ffi::FromBytesUntilNulError;
5+
use core::ffi::{CStr, FromBytesUntilNulError, c_char};
36
#[allow(deprecated)]
47
use core::hash::SipHasher13 as DefaultHasher;
58
use core::hash::{Hash, Hasher};
69

7-
use super::*;
8-
910
#[test]
1011
fn c_to_rust() {
1112
let data = b"123\0";

alloc/src/collections/binary_heap/tests.rs alloc/tests/collections/binary_heap.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use alloc::boxed::Box;
2+
use alloc::collections::binary_heap::*;
3+
use std::iter::TrustedLen;
4+
use std::mem;
15
use std::panic::{AssertUnwindSafe, catch_unwind};
26

3-
use super::*;
4-
use crate::boxed::Box;
57
use crate::testing::crash_test::{CrashTestDummy, Panic};
68

79
#[test]
@@ -531,7 +533,7 @@ fn panic_safe() {
531533
self.0.partial_cmp(&other.0)
532534
}
533535
}
534-
let mut rng = crate::test_helpers::test_rng();
536+
let mut rng = crate::test_rng();
535537
const DATASZ: usize = 32;
536538
// Miri is too slow
537539
let ntest = if cfg!(miri) { 1 } else { 10 };

alloc/tests/collections/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod binary_heap;

alloc/tests/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#![feature(btree_extract_if)]
66
#![feature(cow_is_borrowed)]
77
#![feature(core_intrinsics)]
8+
#![feature(downcast_unchecked)]
89
#![feature(extract_if)]
910
#![feature(exact_size_is_empty)]
11+
#![feature(hashmap_internals)]
1012
#![feature(linked_list_cursors)]
1113
#![feature(map_try_insert)]
1214
#![feature(pattern)]
@@ -29,9 +31,11 @@
2931
#![feature(const_str_from_utf8)]
3032
#![feature(panic_update_hook)]
3133
#![feature(pointer_is_aligned_to)]
34+
#![feature(test)]
3235
#![feature(thin_box)]
3336
#![feature(drain_keep_rest)]
3437
#![feature(local_waker)]
38+
#![feature(str_as_str)]
3539
#![feature(strict_provenance_lints)]
3640
#![feature(vec_pop_if)]
3741
#![feature(unique_rc_arc)]
@@ -40,25 +44,33 @@
4044
#![deny(fuzzy_provenance_casts)]
4145
#![deny(unsafe_op_in_unsafe_fn)]
4246

47+
extern crate test;
48+
4349
use std::hash::{DefaultHasher, Hash, Hasher};
4450

51+
mod alloc;
4552
mod arc;
4653
mod autotraits;
4754
mod borrow;
4855
mod boxed;
4956
mod btree_set_hash;
5057
mod c_str;
58+
mod c_str2;
59+
mod collections;
5160
mod const_fns;
5261
mod cow_str;
5362
mod fmt;
5463
mod heap;
5564
mod linked_list;
65+
mod misc_tests;
5666
mod rc;
5767
mod slice;
5868
mod sort;
5969
mod str;
6070
mod string;
71+
mod sync;
6172
mod task;
73+
mod testing;
6274
mod thin_box;
6375
mod vec;
6476
mod vec_deque;
@@ -69,6 +81,18 @@ fn hash<T: Hash>(t: &T) -> u64 {
6981
s.finish()
7082
}
7183

84+
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
85+
/// seed not being the same for every RNG invocation too.
86+
fn test_rng() -> rand_xorshift::XorShiftRng {
87+
use std::hash::{BuildHasher, Hash, Hasher};
88+
let mut hasher = std::hash::RandomState::new().build_hasher();
89+
std::panic::Location::caller().hash(&mut hasher);
90+
let hc64 = hasher.finish();
91+
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
92+
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
93+
rand::SeedableRng::from_seed(seed)
94+
}
95+
7296
// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.
7397
// See https://github.com/kripken/emscripten-fastcomp/issues/169
7498
#[cfg(not(target_os = "emscripten"))]
File renamed without changes.

alloc/src/sync/tests.rs alloc/tests/sync.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
use alloc::sync::*;
2+
use std::alloc::{AllocError, Allocator, Layout};
3+
use std::any::Any;
14
use std::clone::Clone;
25
use std::mem::MaybeUninit;
36
use std::option::Option::None;
7+
use std::ptr::NonNull;
48
use std::sync::Mutex;
5-
use std::sync::atomic::AtomicUsize;
6-
use std::sync::atomic::Ordering::SeqCst;
9+
use std::sync::atomic::Ordering::*;
10+
use std::sync::atomic::{self, AtomicUsize};
711
use std::sync::mpsc::channel;
812
use std::thread;
913

10-
use super::*;
11-
1214
struct Canary(*mut AtomicUsize);
1315

1416
impl Drop for Canary {

alloc/tests/testing/crash_test.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::cmp::Ordering;
2+
use std::fmt::Debug;
3+
use std::sync::atomic::AtomicUsize;
4+
use std::sync::atomic::Ordering::SeqCst;
5+
6+
/// A blueprint for crash test dummy instances that monitor drops.
7+
/// Some instances may be configured to panic at some point.
8+
///
9+
/// Crash test dummies are identified and ordered by an id, so they can be used
10+
/// as keys in a BTreeMap.
11+
#[derive(Debug)]
12+
pub struct CrashTestDummy {
13+
pub id: usize,
14+
dropped: AtomicUsize,
15+
}
16+
17+
impl CrashTestDummy {
18+
/// Creates a crash test dummy design. The `id` determines order and equality of instances.
19+
pub fn new(id: usize) -> CrashTestDummy {
20+
CrashTestDummy { id, dropped: AtomicUsize::new(0) }
21+
}
22+
23+
/// Creates an instance of a crash test dummy that records what events it experiences
24+
/// and optionally panics.
25+
pub fn spawn(&self, panic: Panic) -> Instance<'_> {
26+
Instance { origin: self, panic }
27+
}
28+
29+
/// Returns how many times instances of the dummy have been dropped.
30+
pub fn dropped(&self) -> usize {
31+
self.dropped.load(SeqCst)
32+
}
33+
}
34+
35+
#[derive(Debug)]
36+
pub struct Instance<'a> {
37+
origin: &'a CrashTestDummy,
38+
panic: Panic,
39+
}
40+
41+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
42+
pub enum Panic {
43+
Never,
44+
InDrop,
45+
}
46+
47+
impl Instance<'_> {
48+
pub fn id(&self) -> usize {
49+
self.origin.id
50+
}
51+
}
52+
53+
impl Drop for Instance<'_> {
54+
fn drop(&mut self) {
55+
self.origin.dropped.fetch_add(1, SeqCst);
56+
if self.panic == Panic::InDrop {
57+
panic!("panic in `drop`");
58+
}
59+
}
60+
}
61+
62+
impl PartialOrd for Instance<'_> {
63+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
64+
self.id().partial_cmp(&other.id())
65+
}
66+
}
67+
68+
impl Ord for Instance<'_> {
69+
fn cmp(&self, other: &Self) -> Ordering {
70+
self.id().cmp(&other.id())
71+
}
72+
}
73+
74+
impl PartialEq for Instance<'_> {
75+
fn eq(&self, other: &Self) -> bool {
76+
self.id().eq(&other.id())
77+
}
78+
}
79+
80+
impl Eq for Instance<'_> {}

alloc/tests/testing/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod crash_test;

0 commit comments

Comments
 (0)