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 12, 2024
1 parent a7367db commit 2f5de33
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 @@ -2578,7 +2578,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 @@ -3045,4 +3045,57 @@ mod test_set {
// (and without the `map`, it does not).
let mut _set: HashSet<_> = (0..3).map(|_| ()).collect();
}

#[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 2f5de33

Please sign in to comment.