diff options
| author | bors <bors@rust-lang.org> | 2021-10-03 16:22:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-10-03 16:22:37 +0000 |
| commit | 08759c691e2e9799a3c6780ffdf910240ebd4a6b (patch) | |
| tree | 74d76de0eb552336c9259d1d866689e7043a7aa7 /library/alloc/src | |
| parent | 5051904d66b14d7b8dd2750bd30610c8c81cb01d (diff) | |
| parent | 923212e3e8fd39a7ef68db12bc4196c8ea41390e (diff) | |
| download | rust-08759c691e2e9799a3c6780ffdf910240ebd4a6b.tar.gz rust-08759c691e2e9799a3c6780ffdf910240ebd4a6b.zip | |
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.
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/collections/btree/map/tests.rs | 55 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/testing/crash_test.rs | 2 |
2 files changed, 32 insertions, 25 deletions
diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index a99d6c49ab7..b8e11e109c8 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -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] diff --git a/library/alloc/src/collections/btree/testing/crash_test.rs b/library/alloc/src/collections/btree/testing/crash_test.rs index b2527b95f5b..488eaa07a95 100644 --- a/library/alloc/src/collections/btree/testing/crash_test.rs +++ b/library/alloc/src/collections/btree/testing/crash_test.rs @@ -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, |
