Skip to content

Commit

Permalink
Add tests from rust-lang#400
Browse files Browse the repository at this point in the history
Co-authored-by: JustForFun88 <[email protected]>
  • Loading branch information
cuviper and JustForFun88 committed Sep 18, 2024
1 parent 8bceb97 commit ad3b430
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -3062,4 +3062,57 @@ mod test_set {
assert_eq!(HashSet::<u32>::new().allocation_size(), 0);
assert!(HashSet::<u32>::with_capacity(1).allocation_size() > core::mem::size_of::<u32>());
}

#[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<Invalid> for InvalidRef {
fn equivalent(&self, key: &Invalid) -> bool {
self.count == key.count && self.other == key.other
}
}
impl Hash for Invalid {
fn hash<H: Hasher>(&self, state: &mut H) {
self.count.hash(state);
}
}
impl Hash for InvalidRef {
fn hash<H: Hasher>(&self, state: &mut H) {
self.count.hash(state);
}
}
let mut set: HashSet<Invalid> = 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);
}
}
}

0 comments on commit ad3b430

Please sign in to comment.