Skip to content

Commit 8940d96

Browse files
newAMeldruin
authored andcommitted
Add test vectors for FnvHasher
1 parent abf14ad commit 8940d96

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/fnv.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const BASIS: u32 = 0x811c9dc5;
44
const PRIME: u32 = 0x1000193;
55

66
/// 32-bit Fowler-Noll-Vo hasher
7+
///
8+
/// Specifically this implements the [FNV-1a hash].
9+
///
10+
/// [FNV-1a hash]: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
711
pub struct Hasher {
812
state: u32,
913
}

tests/fnv.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
extern crate hash32;
2+
3+
use hash32::FnvHasher;
4+
use std::hash::Hasher;
5+
6+
#[test]
7+
fn fnv1a_vectors() {
8+
// Test vectors adapted from this public domain repo:
9+
// https://github.com/drichardson/fnv/blob/cd12926ef306ec32dbef3e65c62662c4eb5b99ed/test_fnv.c
10+
11+
const FNV1A_TEST_VECTORS: &[(&str, u64)] = &[
12+
("", 0x811c9dc5),
13+
("a", 0xe40c292c),
14+
("b", 0xe70c2de5),
15+
("c", 0xe60c2c52),
16+
("d", 0xe10c2473),
17+
("e", 0xe00c22e0),
18+
("f", 0xe30c2799),
19+
("fo", 0x6222e842),
20+
("foo", 0xa9f37ed7),
21+
("foob", 0x3f5076ef),
22+
("fooba", 0x39aaa18a),
23+
("foobar", 0xbf9cf968),
24+
("ch", 0x5f299f4e),
25+
("cho", 0xef8580f3),
26+
("chon", 0xac297727),
27+
("chong", 0x4546b9c0),
28+
("chongo", 0xbd564e7d),
29+
("chongo ", 0x6bdd5c67),
30+
("chongo w", 0xdd77ed30),
31+
("chongo wa", 0xf4ca9683),
32+
("chongo was", 0x4aeb9bd0),
33+
("chongo was ", 0xe0e67ad0),
34+
("chongo was h", 0xc2d32fa8),
35+
("chongo was he", 0x7f743fb7),
36+
("chongo was her", 0x6900631f),
37+
("chongo was here", 0xc59c990e),
38+
("chongo was here!", 0x448524fd),
39+
("chongo was here!\n", 0xd49930d5),
40+
];
41+
42+
for (data, expected) in FNV1A_TEST_VECTORS {
43+
let mut hasher: FnvHasher = Default::default();
44+
hasher.write(data.as_bytes());
45+
assert_eq!(hasher.finish(), *expected);
46+
}
47+
}

0 commit comments

Comments
 (0)