From 3f624ee87d6d9dbe1de6037e5a0a7a20b04dd1ab Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sun, 20 Mar 2022 14:10:38 +0000 Subject: [PATCH] Fail test in extreme case test if we drop twice --- agb/src/hash_map.rs | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/agb/src/hash_map.rs b/agb/src/hash_map.rs index 57596238..7bafabba 100644 --- a/agb/src/hash_map.rs +++ b/agb/src/hash_map.rs @@ -558,6 +558,41 @@ mod test { } } + struct NoisyDrop { + i: i32, + dropped: bool, + } + + impl NoisyDrop { + fn new(i: i32) -> Self { + Self { i, dropped: false } + } + } + + impl PartialEq for NoisyDrop { + fn eq(&self, other: &Self) -> bool { + self.i == other.i + } + } + + impl Eq for NoisyDrop {} + + impl Hash for NoisyDrop { + fn hash(&self, hasher: &mut H) { + hasher.write_i32(self.i); + } + } + + impl Drop for NoisyDrop { + fn drop(&mut self) { + if self.dropped { + panic!("NoisyDropped dropped twice"); + } + + self.dropped = true; + } + } + #[test_case] fn extreme_case(_gba: &mut Gba) { let mut map = HashMap::new(); @@ -574,18 +609,21 @@ mod test { 0 => { // insert answers[key as usize] = Some(value); - map.insert(key, value); + map.insert(NoisyDrop::new(key), NoisyDrop::new(value)); } 1 => { // remove answers[key as usize] = None; - map.remove(&key); + map.remove(&NoisyDrop::new(key)); } _ => {} } for (i, answer) in answers.iter().enumerate() { - assert_eq!(map.get(&(i as i32)), answer.as_ref()); + assert_eq!( + map.get(&NoisyDrop::new(i as i32)).map(|nd| &nd.i), + answer.as_ref() + ); } } }