Debian Codegraph @

.cargo/vendor/hashbrown/tests/hasher.rs

raw
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 }