Skip to content

Commit ad3b430

Browse files
cuviperJustForFun88
andcommitted
Add tests from #400
Co-authored-by: JustForFun88 <alishergaliev88@gmail.com>
1 parent 8bceb97 commit ad3b430

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/set.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ fn assert_covariance() {
25882588

25892589
#[cfg(test)]
25902590
mod test_set {
2591-
use super::HashSet;
2591+
use super::{make_hash, Equivalent, HashSet};
25922592
use crate::DefaultHashBuilder;
25932593
use std::vec::Vec;
25942594

@@ -3062,4 +3062,57 @@ mod test_set {
30623062
assert_eq!(HashSet::<u32>::new().allocation_size(), 0);
30633063
assert!(HashSet::<u32>::with_capacity(1).allocation_size() > core::mem::size_of::<u32>());
30643064
}
3065+
3066+
#[test]
3067+
fn duplicate_insert() {
3068+
let mut set = HashSet::new();
3069+
set.insert(1);
3070+
set.get_or_insert_with(&1, |_| 1);
3071+
set.get_or_insert_with(&1, |_| 1);
3072+
assert!([1].iter().eq(set.iter()));
3073+
}
3074+
3075+
#[test]
3076+
#[should_panic]
3077+
fn some_invalid_equivalent() {
3078+
use core::hash::{Hash, Hasher};
3079+
struct Invalid {
3080+
count: u32,
3081+
other: u32,
3082+
}
3083+
3084+
struct InvalidRef {
3085+
count: u32,
3086+
other: u32,
3087+
}
3088+
3089+
impl PartialEq for Invalid {
3090+
fn eq(&self, other: &Self) -> bool {
3091+
self.count == other.count && self.other == other.other
3092+
}
3093+
}
3094+
impl Eq for Invalid {}
3095+
3096+
impl Equivalent<Invalid> for InvalidRef {
3097+
fn equivalent(&self, key: &Invalid) -> bool {
3098+
self.count == key.count && self.other == key.other
3099+
}
3100+
}
3101+
impl Hash for Invalid {
3102+
fn hash<H: Hasher>(&self, state: &mut H) {
3103+
self.count.hash(state);
3104+
}
3105+
}
3106+
impl Hash for InvalidRef {
3107+
fn hash<H: Hasher>(&self, state: &mut H) {
3108+
self.count.hash(state);
3109+
}
3110+
}
3111+
let mut set: HashSet<Invalid> = HashSet::new();
3112+
let key = InvalidRef { count: 1, other: 1 };
3113+
let value = Invalid { count: 1, other: 2 };
3114+
if make_hash(set.hasher(), &key) == make_hash(set.hasher(), &value) {
3115+
set.get_or_insert_with(&key, |_| value);
3116+
}
3117+
}
30653118
}

0 commit comments

Comments
 (0)