Skip to content

Commit 204f854

Browse files
committed
Slightly tighten leak-on-panic test cases
1 parent 321247d commit 204f854

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

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

+21-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::*;
22
use crate::boxed::Box;
3+
use crate::testing::crash_test::{CrashTestDummy, Panic};
34
use std::iter::TrustedLen;
45
use std::panic::{catch_unwind, AssertUnwindSafe};
5-
use std::sync::atomic::{AtomicU32, Ordering};
66

77
#[test]
88
fn test_iterator() {
@@ -291,33 +291,30 @@ fn test_drain_sorted() {
291291

292292
#[test]
293293
fn test_drain_sorted_leak() {
294-
static DROPS: AtomicU32 = AtomicU32::new(0);
295-
296-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
297-
struct D(u32, bool);
298-
299-
impl Drop for D {
300-
fn drop(&mut self) {
301-
DROPS.fetch_add(1, Ordering::SeqCst);
302-
303-
if self.1 {
304-
panic!("panic in `drop`");
305-
}
306-
}
307-
}
308-
294+
let d0 = CrashTestDummy::new(0);
295+
let d1 = CrashTestDummy::new(1);
296+
let d2 = CrashTestDummy::new(2);
297+
let d3 = CrashTestDummy::new(3);
298+
let d4 = CrashTestDummy::new(4);
299+
let d5 = CrashTestDummy::new(5);
309300
let mut q = BinaryHeap::from(vec![
310-
D(0, false),
311-
D(1, false),
312-
D(2, false),
313-
D(3, true),
314-
D(4, false),
315-
D(5, false),
301+
d0.spawn(Panic::Never),
302+
d1.spawn(Panic::Never),
303+
d2.spawn(Panic::Never),
304+
d3.spawn(Panic::InDrop),
305+
d4.spawn(Panic::Never),
306+
d5.spawn(Panic::Never),
316307
]);
317308

318-
catch_unwind(AssertUnwindSafe(|| drop(q.drain_sorted()))).ok();
309+
catch_unwind(AssertUnwindSafe(|| drop(q.drain_sorted()))).unwrap_err();
319310

320-
assert_eq!(DROPS.load(Ordering::SeqCst), 6);
311+
assert_eq!(d0.dropped(), 1);
312+
assert_eq!(d1.dropped(), 1);
313+
assert_eq!(d2.dropped(), 1);
314+
assert_eq!(d3.dropped(), 1);
315+
assert_eq!(d4.dropped(), 1);
316+
assert_eq!(d5.dropped(), 1);
317+
assert!(q.is_empty());
321318
}
322319

323320
#[test]

library/alloc/src/collections/linked_list/tests.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use crate::testing::crash_test::{CrashTestDummy, Panic};
23
use crate::vec::Vec;
34

45
use std::panic::{catch_unwind, AssertUnwindSafe};
@@ -984,35 +985,34 @@ fn drain_filter_complex() {
984985

985986
#[test]
986987
fn drain_filter_drop_panic_leak() {
987-
static mut DROPS: i32 = 0;
988-
989-
struct D(bool);
990-
991-
impl Drop for D {
992-
fn drop(&mut self) {
993-
unsafe {
994-
DROPS += 1;
995-
}
996-
997-
if self.0 {
998-
panic!("panic in `drop`");
999-
}
1000-
}
1001-
}
1002-
988+
let d0 = CrashTestDummy::new(0);
989+
let d1 = CrashTestDummy::new(1);
990+
let d2 = CrashTestDummy::new(2);
991+
let d3 = CrashTestDummy::new(3);
992+
let d4 = CrashTestDummy::new(4);
993+
let d5 = CrashTestDummy::new(5);
994+
let d6 = CrashTestDummy::new(6);
995+
let d7 = CrashTestDummy::new(7);
1003996
let mut q = LinkedList::new();
1004-
q.push_back(D(false));
1005-
q.push_back(D(false));
1006-
q.push_back(D(false));
1007-
q.push_back(D(false));
1008-
q.push_back(D(false));
1009-
q.push_front(D(false));
1010-
q.push_front(D(true));
1011-
q.push_front(D(false));
1012-
1013-
catch_unwind(AssertUnwindSafe(|| drop(q.drain_filter(|_| true)))).ok();
1014-
1015-
assert_eq!(unsafe { DROPS }, 8);
997+
q.push_back(d3.spawn(Panic::Never));
998+
q.push_back(d4.spawn(Panic::Never));
999+
q.push_back(d5.spawn(Panic::Never));
1000+
q.push_back(d6.spawn(Panic::Never));
1001+
q.push_back(d7.spawn(Panic::Never));
1002+
q.push_front(d2.spawn(Panic::Never));
1003+
q.push_front(d1.spawn(Panic::InDrop));
1004+
q.push_front(d0.spawn(Panic::Never));
1005+
1006+
catch_unwind(AssertUnwindSafe(|| drop(q.drain_filter(|_| true)))).unwrap_err();
1007+
1008+
assert_eq!(d0.dropped(), 1);
1009+
assert_eq!(d1.dropped(), 1);
1010+
assert_eq!(d2.dropped(), 1);
1011+
assert_eq!(d3.dropped(), 1);
1012+
assert_eq!(d4.dropped(), 1);
1013+
assert_eq!(d5.dropped(), 1);
1014+
assert_eq!(d6.dropped(), 1);
1015+
assert_eq!(d7.dropped(), 1);
10161016
assert!(q.is_empty());
10171017
}
10181018

0 commit comments

Comments
 (0)