From ad3b4306825a0929330f233db8ec2353e5f5d062 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 12 Sep 2024 08:47:07 -0700 Subject: [PATCH] Add tests from #400 Co-authored-by: JustForFun88 --- src/set.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/set.rs b/src/set.rs index a93c0928f..9490d0e2f 100644 --- a/src/set.rs +++ b/src/set.rs @@ -2588,7 +2588,7 @@ fn assert_covariance() { #[cfg(test)] mod test_set { - use super::HashSet; + use super::{make_hash, Equivalent, HashSet}; use crate::DefaultHashBuilder; use std::vec::Vec; @@ -3062,4 +3062,57 @@ mod test_set { assert_eq!(HashSet::::new().allocation_size(), 0); assert!(HashSet::::with_capacity(1).allocation_size() > core::mem::size_of::()); } + + #[test] + fn duplicate_insert() { + let mut set = HashSet::new(); + set.insert(1); + set.get_or_insert_with(&1, |_| 1); + set.get_or_insert_with(&1, |_| 1); + assert!([1].iter().eq(set.iter())); + } + + #[test] + #[should_panic] + fn some_invalid_equivalent() { + use core::hash::{Hash, Hasher}; + struct Invalid { + count: u32, + other: u32, + } + + struct InvalidRef { + count: u32, + other: u32, + } + + impl PartialEq for Invalid { + fn eq(&self, other: &Self) -> bool { + self.count == other.count && self.other == other.other + } + } + impl Eq for Invalid {} + + impl Equivalent for InvalidRef { + fn equivalent(&self, key: &Invalid) -> bool { + self.count == key.count && self.other == key.other + } + } + impl Hash for Invalid { + fn hash(&self, state: &mut H) { + self.count.hash(state); + } + } + impl Hash for InvalidRef { + fn hash(&self, state: &mut H) { + self.count.hash(state); + } + } + let mut set: HashSet = HashSet::new(); + let key = InvalidRef { count: 1, other: 1 }; + let value = Invalid { count: 1, other: 2 }; + if make_hash(set.hasher(), &key) == make_hash(set.hasher(), &value) { + set.get_or_insert_with(&key, |_| value); + } + } }