|
1 | 1 | use super::*;
|
2 | 2 | use crate::boxed::Box;
|
| 3 | +use crate::testing::crash_test::{CrashTestDummy, Panic}; |
3 | 4 | use std::iter::TrustedLen;
|
4 | 5 | use std::panic::{catch_unwind, AssertUnwindSafe};
|
5 |
| -use std::sync::atomic::{AtomicU32, Ordering}; |
6 | 6 |
|
7 | 7 | #[test]
|
8 | 8 | fn test_iterator() {
|
@@ -291,33 +291,83 @@ fn test_drain_sorted() {
|
291 | 291 |
|
292 | 292 | #[test]
|
293 | 293 | 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); |
309 | 300 | 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), |
316 | 307 | ]);
|
317 | 308 |
|
318 |
| - catch_unwind(AssertUnwindSafe(|| drop(q.drain_sorted()))).ok(); |
| 309 | + catch_unwind(AssertUnwindSafe(|| drop(q.drain_sorted()))).unwrap_err(); |
| 310 | + |
| 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()); |
| 318 | +} |
319 | 319 |
|
320 |
| - assert_eq!(DROPS.load(Ordering::SeqCst), 6); |
| 320 | +#[test] |
| 321 | +fn test_drain_forget() { |
| 322 | + let a = CrashTestDummy::new(0); |
| 323 | + let b = CrashTestDummy::new(1); |
| 324 | + let c = CrashTestDummy::new(2); |
| 325 | + let mut q = |
| 326 | + BinaryHeap::from(vec![a.spawn(Panic::Never), b.spawn(Panic::Never), c.spawn(Panic::Never)]); |
| 327 | + |
| 328 | + catch_unwind(AssertUnwindSafe(|| { |
| 329 | + let mut it = q.drain(); |
| 330 | + it.next(); |
| 331 | + mem::forget(it); |
| 332 | + })) |
| 333 | + .unwrap(); |
| 334 | + // Behaviour after leaking is explicitly unspecified and order is arbitrary, |
| 335 | + // so it's fine if these start failing, but probably worth knowing. |
| 336 | + assert!(q.is_empty()); |
| 337 | + assert_eq!(a.dropped() + b.dropped() + c.dropped(), 1); |
| 338 | + assert_eq!(a.dropped(), 0); |
| 339 | + assert_eq!(b.dropped(), 0); |
| 340 | + assert_eq!(c.dropped(), 1); |
| 341 | + drop(q); |
| 342 | + assert_eq!(a.dropped(), 0); |
| 343 | + assert_eq!(b.dropped(), 0); |
| 344 | + assert_eq!(c.dropped(), 1); |
| 345 | +} |
| 346 | + |
| 347 | +#[test] |
| 348 | +fn test_drain_sorted_forget() { |
| 349 | + let a = CrashTestDummy::new(0); |
| 350 | + let b = CrashTestDummy::new(1); |
| 351 | + let c = CrashTestDummy::new(2); |
| 352 | + let mut q = |
| 353 | + BinaryHeap::from(vec![a.spawn(Panic::Never), b.spawn(Panic::Never), c.spawn(Panic::Never)]); |
| 354 | + |
| 355 | + catch_unwind(AssertUnwindSafe(|| { |
| 356 | + let mut it = q.drain_sorted(); |
| 357 | + it.next(); |
| 358 | + mem::forget(it); |
| 359 | + })) |
| 360 | + .unwrap(); |
| 361 | + // Behaviour after leaking is explicitly unspecified, |
| 362 | + // so it's fine if these start failing, but probably worth knowing. |
| 363 | + assert_eq!(q.len(), 2); |
| 364 | + assert_eq!(a.dropped(), 0); |
| 365 | + assert_eq!(b.dropped(), 0); |
| 366 | + assert_eq!(c.dropped(), 1); |
| 367 | + drop(q); |
| 368 | + assert_eq!(a.dropped(), 1); |
| 369 | + assert_eq!(b.dropped(), 1); |
| 370 | + assert_eq!(c.dropped(), 1); |
321 | 371 | }
|
322 | 372 |
|
323 | 373 | #[test]
|
|
0 commit comments