Skip to content

Commit

Permalink
Auto merge of #88086 - ssomers:btree_clone_testing, r=dtolnay
Browse files Browse the repository at this point in the history
BTree: toughen panicky test of clone()

Test did not cover the second half of `clone_subtree` and why this clones key & value first.
  • Loading branch information
bors committed Oct 3, 2021
2 parents 5051904 + 923212e commit 08759c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
55 changes: 31 additions & 24 deletions library/alloc/src/collections/btree/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,33 +1494,40 @@ fn test_clone() {
map.check();
}

#[test]
fn test_clone_panic_leak() {
let a = CrashTestDummy::new(0);
let b = CrashTestDummy::new(1);
let c = CrashTestDummy::new(2);
fn test_clone_panic_leak(size: usize) {
for i in 0..size {
let dummies: Vec<CrashTestDummy> = (0..size).map(|id| CrashTestDummy::new(id)).collect();
let map: BTreeMap<_, ()> = dummies
.iter()
.map(|dummy| {
let panic = if dummy.id == i { Panic::InClone } else { Panic::Never };
(dummy.spawn(panic), ())
})
.collect();

let mut map = BTreeMap::new();
map.insert(a.spawn(Panic::Never), ());
map.insert(b.spawn(Panic::InClone), ());
map.insert(c.spawn(Panic::Never), ());
catch_unwind(|| map.clone()).unwrap_err();
for d in &dummies {
assert_eq!(d.cloned(), if d.id <= i { 1 } else { 0 }, "id={}/{}", d.id, i);
assert_eq!(d.dropped(), if d.id < i { 1 } else { 0 }, "id={}/{}", d.id, i);
}
assert_eq!(map.len(), size);

catch_unwind(|| map.clone()).unwrap_err();
assert_eq!(a.cloned(), 1);
assert_eq!(b.cloned(), 1);
assert_eq!(c.cloned(), 0);
assert_eq!(a.dropped(), 1);
assert_eq!(b.dropped(), 0);
assert_eq!(c.dropped(), 0);
assert_eq!(map.len(), 3);
drop(map);
for d in &dummies {
assert_eq!(d.cloned(), if d.id <= i { 1 } else { 0 }, "id={}/{}", d.id, i);
assert_eq!(d.dropped(), if d.id < i { 2 } else { 1 }, "id={}/{}", d.id, i);
}
}
}

drop(map);
assert_eq!(a.cloned(), 1);
assert_eq!(b.cloned(), 1);
assert_eq!(c.cloned(), 0);
assert_eq!(a.dropped(), 2);
assert_eq!(b.dropped(), 1);
assert_eq!(c.dropped(), 1);
#[test]
fn test_clone_panic_leak_height_0() {
test_clone_panic_leak(3)
}

#[test]
fn test_clone_panic_leak_height_1() {
test_clone_panic_leak(MIN_INSERTS_HEIGHT_1)
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/testing/crash_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
/// on anything defined in the crate, apart from the `Debug` trait.
#[derive(Debug)]
pub struct CrashTestDummy {
id: usize,
pub id: usize,
cloned: AtomicUsize,
dropped: AtomicUsize,
queried: AtomicUsize,
Expand Down

0 comments on commit 08759c6

Please sign in to comment.