Adapt inplace collection leak test to check for no leaks

This commit is contained in:
Giacomo Stevanato 2022-09-10 11:27:21 +02:00
parent fa61678a7d
commit f81b07e947

View File

@ -1110,11 +1110,13 @@ fn test_from_iter_specialization_panic_during_iteration_drops() {
}
#[test]
fn test_from_iter_specialization_panic_during_drop_leaks() {
static mut DROP_COUNTER: usize = 0;
fn test_from_iter_specialization_panic_during_drop_doesnt_leak() {
static mut DROP_COUNTER_SHOULD_BE_DROPPED: usize = 0;
static mut DROP_COUNTER_DROPPED_TWICE: usize = 0;
#[derive(Debug)]
enum Droppable {
ShouldBeDropped,
DroppedTwice(Box<i32>),
PanicOnDrop,
}
@ -1122,11 +1124,17 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
impl Drop for Droppable {
fn drop(&mut self) {
match self {
Droppable::ShouldBeDropped => {
unsafe {
DROP_COUNTER_SHOULD_BE_DROPPED += 1;
}
println!("Dropping ShouldBeDropped!")
}
Droppable::DroppedTwice(_) => {
unsafe {
DROP_COUNTER += 1;
DROP_COUNTER_DROPPED_TWICE += 1;
}
println!("Dropping!")
println!("Dropping DroppedTwice!")
}
Droppable::PanicOnDrop => {
if !std::thread::panicking() {
@ -1137,21 +1145,17 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
}
}
let mut to_free: *mut Droppable = core::ptr::null_mut();
let mut cap = 0;
let _ = std::panic::catch_unwind(AssertUnwindSafe(|| {
let mut v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop];
to_free = v.as_mut_ptr();
cap = v.capacity();
let _ = v.into_iter().take(0).collect::<Vec<_>>();
let v = vec![
Droppable::ShouldBeDropped,
Droppable::DroppedTwice(Box::new(123)),
Droppable::PanicOnDrop,
];
let _ = v.into_iter().take(1).collect::<Vec<_>>();
}));
assert_eq!(unsafe { DROP_COUNTER }, 1);
// clean up the leak to keep miri happy
unsafe {
drop(Vec::from_raw_parts(to_free, 0, cap));
}
assert_eq!(unsafe { DROP_COUNTER_SHOULD_BE_DROPPED }, 1);
assert_eq!(unsafe { DROP_COUNTER_DROPPED_TWICE }, 1);
}
// regression test for issue #85322. Peekable previously implemented InPlaceIterable,