Skip to content

Commit

Permalink
Vec::dedup optimization - add test for panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Soveu committed Mar 15, 2021
1 parent afdbc9e commit 2285f11
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions library/alloc/tests/vec.rs
Expand Up @@ -7,6 +7,7 @@ use std::mem::{size_of, swap};
use std::ops::Bound::*;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::rc::Rc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::vec::{Drain, IntoIter};

struct DropCounter<'a> {
Expand Down Expand Up @@ -2169,3 +2170,56 @@ fn test_vec_dedup() {
assert_eq!(vec, dedup);
}
}

#[test]
fn test_vec_dedup_panicking() {
#[derive(Debug)]
struct Panic {
drop_counter: &'static AtomicU32,
value: bool,
index: usize,
}

impl PartialEq for Panic {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}

impl Drop for Panic {
fn drop(&mut self) {
let x = self.drop_counter.fetch_add(1, Ordering::SeqCst);
assert!(x != 4);
}
}

static DROP_COUNTER: AtomicU32 = AtomicU32::new(0);
let expected = [
Panic { drop_counter: &DROP_COUNTER, value: false, index: 0 },
Panic { drop_counter: &DROP_COUNTER, value: false, index: 5 },
Panic { drop_counter: &DROP_COUNTER, value: true, index: 6 },
Panic { drop_counter: &DROP_COUNTER, value: true, index: 7 },
];
let mut vec = vec![
Panic { drop_counter: &DROP_COUNTER, value: false, index: 0 },
// these elements get deduplicated
Panic { drop_counter: &DROP_COUNTER, value: false, index: 1 },
Panic { drop_counter: &DROP_COUNTER, value: false, index: 2 },
Panic { drop_counter: &DROP_COUNTER, value: false, index: 3 },
Panic { drop_counter: &DROP_COUNTER, value: false, index: 4 },
// here it panics
Panic { drop_counter: &DROP_COUNTER, value: false, index: 5 },
Panic { drop_counter: &DROP_COUNTER, value: true, index: 6 },
Panic { drop_counter: &DROP_COUNTER, value: true, index: 7 },
];

let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
vec.dedup();
}));

let ok = vec.iter().zip(expected.iter()).all(|(x, y)| x.index == y.index);

if !ok {
panic!("expected: {:?}\ngot: {:?}\n", expected, vec);
}
}

0 comments on commit 2285f11

Please sign in to comment.