remove unsafe from Droppable test helper

This commit is contained in:
Jorge Aparicio 2022-05-09 15:14:00 +02:00
parent 44fb37d168
commit 433e4a3cdb
4 changed files with 24 additions and 47 deletions

View File

@ -582,7 +582,7 @@ mod tests {
v.pop().unwrap(); v.pop().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut v: BinaryHeap<Droppable, Max, 2> = BinaryHeap::new(); let mut v: BinaryHeap<Droppable, Max, 2> = BinaryHeap::new();
@ -590,7 +590,7 @@ mod tests {
v.push(Droppable::new()).ok().unwrap(); v.push(Droppable::new()).ok().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut v: BinaryHeap<Droppable, Min, 2> = BinaryHeap::new(); let mut v: BinaryHeap<Droppable, Min, 2> = BinaryHeap::new();
@ -599,7 +599,7 @@ mod tests {
v.pop().unwrap(); v.pop().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut v: BinaryHeap<Droppable, Min, 2> = BinaryHeap::new(); let mut v: BinaryHeap<Droppable, Min, 2> = BinaryHeap::new();
@ -607,7 +607,7 @@ mod tests {
v.push(Droppable::new()).ok().unwrap(); v.push(Droppable::new()).ok().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
} }
#[test] #[test]

View File

@ -565,29 +565,6 @@ mod tests {
let mut _v: Deque<i32, 4> = Deque::new(); let mut _v: Deque<i32, 4> = Deque::new();
} }
macro_rules! droppable {
() => {
struct Droppable;
impl Droppable {
fn new() -> Self {
unsafe {
COUNT += 1;
}
Droppable
}
}
impl Drop for Droppable {
fn drop(&mut self) {
unsafe {
COUNT -= 1;
}
}
}
static mut COUNT: i32 = 0;
};
}
#[test] #[test]
fn drop() { fn drop() {
droppable!(); droppable!();
@ -599,7 +576,7 @@ mod tests {
v.pop_front().unwrap(); v.pop_front().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut v: Deque<Droppable, 2> = Deque::new(); let mut v: Deque<Droppable, 2> = Deque::new();
@ -607,14 +584,14 @@ mod tests {
v.push_back(Droppable::new()).ok().unwrap(); v.push_back(Droppable::new()).ok().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut v: Deque<Droppable, 2> = Deque::new(); let mut v: Deque<Droppable, 2> = Deque::new();
v.push_front(Droppable::new()).ok().unwrap(); v.push_front(Droppable::new()).ok().unwrap();
v.push_front(Droppable::new()).ok().unwrap(); v.push_front(Droppable::new()).ok().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
} }
#[test] #[test]
@ -754,7 +731,7 @@ mod tests {
let _ = items.next(); let _ = items.next();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut deque: Deque<Droppable, 2> = Deque::new(); let mut deque: Deque<Droppable, 2> = Deque::new();
@ -764,7 +741,7 @@ mod tests {
// Move none // Move none
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut deque: Deque<Droppable, 2> = Deque::new(); let mut deque: Deque<Droppable, 2> = Deque::new();
@ -774,7 +751,7 @@ mod tests {
let _ = items.next(); // Move partly let _ = items.next(); // Move partly
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
} }
#[test] #[test]

View File

@ -1,23 +1,23 @@
macro_rules! droppable { macro_rules! droppable {
() => { () => {
static COUNT: core::sync::atomic::AtomicI32 = core::sync::atomic::AtomicI32::new(0);
#[derive(Eq, Ord, PartialEq, PartialOrd)] #[derive(Eq, Ord, PartialEq, PartialOrd)]
struct Droppable(i32); struct Droppable(i32);
impl Droppable { impl Droppable {
fn new() -> Self { fn new() -> Self {
unsafe { COUNT.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
COUNT += 1; Droppable(Self::count())
Droppable(COUNT) }
}
fn count() -> i32 {
COUNT.load(core::sync::atomic::Ordering::Relaxed)
} }
} }
impl Drop for Droppable { impl Drop for Droppable {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { COUNT.fetch_sub(1, core::sync::atomic::Ordering::Relaxed);
COUNT -= 1;
}
} }
} }
static mut COUNT: i32 = 0;
}; };
} }

View File

@ -912,7 +912,7 @@ mod tests {
v.pop().unwrap(); v.pop().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut v: Vec<Droppable, 2> = Vec::new(); let mut v: Vec<Droppable, 2> = Vec::new();
@ -920,7 +920,7 @@ mod tests {
v.push(Droppable::new()).ok().unwrap(); v.push(Droppable::new()).ok().unwrap();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
} }
#[test] #[test]
@ -1055,7 +1055,7 @@ mod tests {
let _ = items.next(); let _ = items.next();
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut vec: Vec<Droppable, 2> = Vec::new(); let mut vec: Vec<Droppable, 2> = Vec::new();
@ -1065,7 +1065,7 @@ mod tests {
// Move none // Move none
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
{ {
let mut vec: Vec<Droppable, 2> = Vec::new(); let mut vec: Vec<Droppable, 2> = Vec::new();
@ -1075,7 +1075,7 @@ mod tests {
let _ = items.next(); // Move partly let _ = items.next(); // Move partly
} }
assert_eq!(unsafe { COUNT }, 0); assert_eq!(Droppable::count(), 0);
} }
#[test] #[test]