Skip to content

Commit f36c8df

Browse files
committed
Add test for growth with collisions
1 parent d213be8 commit f36c8df

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

crates/hash-sorted-map/src/hash_sorted_map.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ impl<K, V, S> Drop for HashSortedMap<K, V, S> {
589589

590590
#[cfg(test)]
591591
mod tests {
592+
use std::hash::{BuildHasher, Hasher};
593+
592594
use super::*;
593595

594596
#[test]
@@ -772,4 +774,37 @@ mod tests {
772774
*map.entry(7).and_modify(|v| *v *= 2).or_insert(99) += 1;
773775
assert_eq!(map.get(&7), Some(&203));
774776
}
777+
778+
/// Degenerate hasher that returns a fixed hash code, for forcing collisions.
779+
struct FixedHasher(u64);
780+
781+
impl Hasher for FixedHasher {
782+
fn finish(&self) -> u64 {
783+
self.0
784+
}
785+
fn write(&mut self, _bytes: &[u8]) {}
786+
}
787+
788+
#[derive(Clone)]
789+
struct FixedState(u64);
790+
791+
impl BuildHasher for FixedState {
792+
type Hasher = FixedHasher;
793+
fn build_hasher(&self) -> FixedHasher {
794+
FixedHasher(self.0)
795+
}
796+
}
797+
798+
#[test]
799+
fn test_collisions() {
800+
// Tiny initial capacity + all collisions
801+
let mut m = HashSortedMap::with_capacity_and_hasher(1, FixedState(0));
802+
for i in 0..200u32 {
803+
m.insert(i, i);
804+
}
805+
assert_eq!(m.len(), 200);
806+
for i in 0..200u32 {
807+
assert_eq!(m.get(&i), Some(&i));
808+
}
809+
}
775810
}

0 commit comments

Comments
 (0)