Skip to content

Commit

Permalink
Add collect unwinding test for zero-sized elements
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed May 23, 2020
1 parent b5869e4 commit 94baa82
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/collect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use rayon::prelude::*;

use std::panic;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Mutex;

#[test]
Expand Down Expand Up @@ -36,6 +38,9 @@ fn collect_drop_on_unwind() {
Recorddrop(elt, &drops)
})
.collect_into_vec(&mut result);

// If we reach this point, this must pass
assert_eq!(a.len(), result.len());
}));

let inserts = inserts.get_mut().unwrap();
Expand All @@ -55,3 +60,52 @@ fn collect_drop_on_unwind() {
}
}

#[test]
fn collect_drop_on_unwind_zst() {
static INSERTS: AtomicUsize = AtomicUsize::new(0);
static DROPS: AtomicUsize = AtomicUsize::new(0);

struct RecorddropZst;

impl Drop for RecorddropZst {
fn drop(&mut self) {
DROPS.fetch_add(1, Ordering::SeqCst);
}
}

let test_collect_panic = |will_panic: bool| {
INSERTS.store(0, Ordering::SeqCst);
DROPS.store(0, Ordering::SeqCst);

let test_vec_len = 1024;
let panic_point = 740;

let a = (0..test_vec_len).collect::<Vec<_>>();

let _result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
let mut result = Vec::new();
a.par_iter()
.map(|&a| {
if a > panic_point && will_panic {
panic!("unwinding for test");
}
INSERTS.fetch_add(1, Ordering::SeqCst);
RecorddropZst
})
.collect_into_vec(&mut result);

// If we reach this point, this must pass
assert_eq!(a.len(), result.len());
}));

let inserts = INSERTS.load(Ordering::SeqCst);
let drops = DROPS.load(Ordering::SeqCst);

assert_eq!(inserts, drops, "Incorrect number of drops");
assert!(will_panic || drops == test_vec_len)
};

for &should_panic in &[true, false] {
test_collect_panic(should_panic);
}
}

0 comments on commit 94baa82

Please sign in to comment.