Skip to content

Add sanity checks with alternate hashers #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions tests/hasher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! Sanity check that alternate hashers work correctly.

#![cfg(not(miri))] // FIXME: takes too long

use hashbrown::HashSet;
use std::hash::{BuildHasher, BuildHasherDefault, Hasher};

fn check<S: BuildHasher + Default>() {
let range = 0..1_000;

let mut set = HashSet::<i32, S>::default();
set.extend(range.clone());

assert!(!set.contains(&i32::min_value()));
assert!(!set.contains(&(range.start - 1)));
for i in range.clone() {
assert!(set.contains(&i));
}
assert!(!set.contains(&range.end));
assert!(!set.contains(&i32::max_value()));
}

/// Use hashbrown's default hasher.
#[test]
fn default() {
check::<hashbrown::hash_map::DefaultHashBuilder>();
}

/// Use std's default hasher.
#[test]
fn random_state() {
check::<std::collections::hash_map::RandomState>();
}

/// Use a constant 0 hash.
#[test]
fn zero() {
#[derive(Default)]
struct ZeroHasher;

impl Hasher for ZeroHasher {
fn finish(&self) -> u64 {
0
}
fn write(&mut self, _: &[u8]) {}
}

check::<BuildHasherDefault<ZeroHasher>>();
}

/// Use a constant maximum hash.
#[test]
fn max() {
#[derive(Default)]
struct MaxHasher;

impl Hasher for MaxHasher {
fn finish(&self) -> u64 {
u64::max_value()
}
fn write(&mut self, _: &[u8]) {}
}

check::<BuildHasherDefault<MaxHasher>>();
}