Skip to content

Commit 08759c6

Browse files
committed
Auto merge of #88086 - ssomers:btree_clone_testing, r=dtolnay
BTree: toughen panicky test of clone() Test did not cover the second half of `clone_subtree` and why this clones key & value first.
2 parents 5051904 + 923212e commit 08759c6

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

library/alloc/src/collections/btree/map/tests.rs

+31-24
Original file line numberDiff line numberDiff line change
@@ -1494,33 +1494,40 @@ fn test_clone() {
14941494
map.check();
14951495
}
14961496

1497-
#[test]
1498-
fn test_clone_panic_leak() {
1499-
let a = CrashTestDummy::new(0);
1500-
let b = CrashTestDummy::new(1);
1501-
let c = CrashTestDummy::new(2);
1497+
fn test_clone_panic_leak(size: usize) {
1498+
for i in 0..size {
1499+
let dummies: Vec<CrashTestDummy> = (0..size).map(|id| CrashTestDummy::new(id)).collect();
1500+
let map: BTreeMap<_, ()> = dummies
1501+
.iter()
1502+
.map(|dummy| {
1503+
let panic = if dummy.id == i { Panic::InClone } else { Panic::Never };
1504+
(dummy.spawn(panic), ())
1505+
})
1506+
.collect();
15021507

1503-
let mut map = BTreeMap::new();
1504-
map.insert(a.spawn(Panic::Never), ());
1505-
map.insert(b.spawn(Panic::InClone), ());
1506-
map.insert(c.spawn(Panic::Never), ());
1508+
catch_unwind(|| map.clone()).unwrap_err();
1509+
for d in &dummies {
1510+
assert_eq!(d.cloned(), if d.id <= i { 1 } else { 0 }, "id={}/{}", d.id, i);
1511+
assert_eq!(d.dropped(), if d.id < i { 1 } else { 0 }, "id={}/{}", d.id, i);
1512+
}
1513+
assert_eq!(map.len(), size);
15071514

1508-
catch_unwind(|| map.clone()).unwrap_err();
1509-
assert_eq!(a.cloned(), 1);
1510-
assert_eq!(b.cloned(), 1);
1511-
assert_eq!(c.cloned(), 0);
1512-
assert_eq!(a.dropped(), 1);
1513-
assert_eq!(b.dropped(), 0);
1514-
assert_eq!(c.dropped(), 0);
1515-
assert_eq!(map.len(), 3);
1515+
drop(map);
1516+
for d in &dummies {
1517+
assert_eq!(d.cloned(), if d.id <= i { 1 } else { 0 }, "id={}/{}", d.id, i);
1518+
assert_eq!(d.dropped(), if d.id < i { 2 } else { 1 }, "id={}/{}", d.id, i);
1519+
}
1520+
}
1521+
}
15161522

1517-
drop(map);
1518-
assert_eq!(a.cloned(), 1);
1519-
assert_eq!(b.cloned(), 1);
1520-
assert_eq!(c.cloned(), 0);
1521-
assert_eq!(a.dropped(), 2);
1522-
assert_eq!(b.dropped(), 1);
1523-
assert_eq!(c.dropped(), 1);
1523+
#[test]
1524+
fn test_clone_panic_leak_height_0() {
1525+
test_clone_panic_leak(3)
1526+
}
1527+
1528+
#[test]
1529+
fn test_clone_panic_leak_height_1() {
1530+
test_clone_panic_leak(MIN_INSERTS_HEIGHT_1)
15241531
}
15251532

15261533
#[test]

library/alloc/src/collections/btree/testing/crash_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
1111
/// on anything defined in the crate, apart from the `Debug` trait.
1212
#[derive(Debug)]
1313
pub struct CrashTestDummy {
14-
id: usize,
14+
pub id: usize,
1515
cloned: AtomicUsize,
1616
dropped: AtomicUsize,
1717
queried: AtomicUsize,

0 commit comments

Comments
 (0)