add test::stress_test
This commit is contained in:
parent
5fd36f901c
commit
04d59b77d8
|
@ -7,5 +7,8 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
rand = "0.8.5"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
unstable = []
|
unstable = []
|
|
@ -103,7 +103,7 @@
|
||||||
//! │ │ └──────────────────────────────────────────┘ │ │
|
//! │ │ └──────────────────────────────────────────┘ │ │
|
||||||
//! │ └─────────────────────────────────────────────────────────┘ │
|
//! │ └─────────────────────────────────────────────────────────┘ │
|
||||||
//! │ │
|
//! │ │
|
||||||
//! └─ aligned to usize also aligned to usize ─┘
|
//! └─ aligned to align_of::<*const T>() also aligned to *const T ─┘
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! [`Vec<T: ?Sized>`]: Vec
|
//! [`Vec<T: ?Sized>`]: Vec
|
||||||
|
@ -142,7 +142,7 @@ use std::{
|
||||||
alloc::{alloc, dealloc, Layout},
|
alloc::{alloc, dealloc, Layout},
|
||||||
any::Any,
|
any::Any,
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
mem::{self, align_of_val, size_of, size_of_val},
|
mem::{self, align_of, align_of_val, size_of, size_of_val},
|
||||||
ops::{Index, IndexMut},
|
ops::{Index, IndexMut},
|
||||||
ptr::{drop_in_place, NonNull},
|
ptr::{drop_in_place, NonNull},
|
||||||
slice,
|
slice,
|
||||||
|
@ -312,7 +312,7 @@ impl<T: ?Sized> Vec<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn realloc(&mut self, size: usize) {
|
unsafe fn realloc(&mut self, size: usize) {
|
||||||
let layout = Layout::from_size_align_unchecked(size, 8).pad_to_align();
|
let layout = Layout::from_size_align_unchecked(size, align_of::<Extra<T>>()).pad_to_align();
|
||||||
if self.capacity == 0 {
|
if self.capacity == 0 {
|
||||||
// will panic if OOM
|
// will panic if OOM
|
||||||
self.ptr = NonNull::new(alloc(layout)).unwrap();
|
self.ptr = NonNull::new(alloc(layout)).unwrap();
|
||||||
|
@ -519,7 +519,7 @@ impl<T: ?Sized> Vec<T> {
|
||||||
if self.capacity != 0 {
|
if self.capacity != 0 {
|
||||||
dealloc(
|
dealloc(
|
||||||
self.ptr.as_ptr(),
|
self.ptr.as_ptr(),
|
||||||
Layout::from_size_align_unchecked(self.capacity, 8),
|
Layout::from_size_align_unchecked(self.capacity, align_of::<Extra<T>>()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
32
src/test.rs
32
src/test.rs
|
@ -1,5 +1,7 @@
|
||||||
//! Always use `cargo miri test`, and never just `cargo test`.
|
//! Always use `cargo miri test`, and never just `cargo test`.
|
||||||
|
|
||||||
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
use super::prelude::{vec, *};
|
use super::prelude::{vec, *};
|
||||||
use std::{
|
use std::{
|
||||||
any::Any,
|
any::Any,
|
||||||
|
@ -211,3 +213,33 @@ fn remove() {
|
||||||
assert_eq!(vec.remove(1).unwrap().debug(), "\"foo\"");
|
assert_eq!(vec.remove(1).unwrap().debug(), "\"foo\"");
|
||||||
assert_eq!(vec.debug(), "[1, true]");
|
assert_eq!(vec.debug(), "[1, true]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn stress_test() {
|
||||||
|
let mut vec: Vec<dyn Debug> = Vec::new();
|
||||||
|
|
||||||
|
let mut rng = thread_rng();
|
||||||
|
|
||||||
|
let mut len = 0;
|
||||||
|
|
||||||
|
let iterations = if cfg!(miri) { 100_000 } else { 1_000_000 };
|
||||||
|
|
||||||
|
for _ in 0..iterations {
|
||||||
|
if len == 0 || rng.gen_bool(0.7) {
|
||||||
|
len += 1;
|
||||||
|
|
||||||
|
match rng.gen_range(1..=3) {
|
||||||
|
1 => vec.push_unsize_stable("a string", |v| v as _),
|
||||||
|
2 => vec.push_unsize_stable(1, |v| v as _),
|
||||||
|
3 => vec.push_unsize_stable(true, |v| v as _),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
len -= 1;
|
||||||
|
|
||||||
|
vec.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(len, vec.len());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue