Skip to content

Commit c9fcc54

Browse files
bors[bot]cuviper
andcommitted
Merge #63
63: Add sanity checks with alternate hashers r=Amanieu a=cuviper The `max` and `random_state` tests currently fail on 32-bit targets per #60. Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents 5adef52 + 1f14361 commit c9fcc54

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

tests/hasher.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! Sanity check that alternate hashers work correctly.
2+
3+
#![cfg(not(miri))] // FIXME: takes too long
4+
5+
use hashbrown::HashSet;
6+
use std::hash::{BuildHasher, BuildHasherDefault, Hasher};
7+
8+
fn check<S: BuildHasher + Default>() {
9+
let range = 0..1_000;
10+
11+
let mut set = HashSet::<i32, S>::default();
12+
set.extend(range.clone());
13+
14+
assert!(!set.contains(&i32::min_value()));
15+
assert!(!set.contains(&(range.start - 1)));
16+
for i in range.clone() {
17+
assert!(set.contains(&i));
18+
}
19+
assert!(!set.contains(&range.end));
20+
assert!(!set.contains(&i32::max_value()));
21+
}
22+
23+
/// Use hashbrown's default hasher.
24+
#[test]
25+
fn default() {
26+
check::<hashbrown::hash_map::DefaultHashBuilder>();
27+
}
28+
29+
/// Use std's default hasher.
30+
#[test]
31+
fn random_state() {
32+
check::<std::collections::hash_map::RandomState>();
33+
}
34+
35+
/// Use a constant 0 hash.
36+
#[test]
37+
fn zero() {
38+
#[derive(Default)]
39+
struct ZeroHasher;
40+
41+
impl Hasher for ZeroHasher {
42+
fn finish(&self) -> u64 {
43+
0
44+
}
45+
fn write(&mut self, _: &[u8]) {}
46+
}
47+
48+
check::<BuildHasherDefault<ZeroHasher>>();
49+
}
50+
51+
/// Use a constant maximum hash.
52+
#[test]
53+
fn max() {
54+
#[derive(Default)]
55+
struct MaxHasher;
56+
57+
impl Hasher for MaxHasher {
58+
fn finish(&self) -> u64 {
59+
u64::max_value()
60+
}
61+
fn write(&mut self, _: &[u8]) {}
62+
}
63+
64+
check::<BuildHasherDefault<MaxHasher>>();
65+
}

0 commit comments

Comments
 (0)