|
| 1 | +extern crate hash32; |
| 2 | + |
| 3 | +use hash32::Murmur3Hasher; |
| 4 | +use std::hash::Hasher; |
| 5 | + |
| 6 | +fn testcase(data: &[u8], expected_hash_value: u64) { |
| 7 | + let mut hasher: Murmur3Hasher = Default::default(); |
| 8 | + hasher.write(data); |
| 9 | + assert_eq!(hasher.finish(), expected_hash_value); |
| 10 | +} |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn murmurhash3_vectors() { |
| 14 | + // Test vectors are adapted from this gist |
| 15 | + // https://gist.github.com/vladimirgamalyan/defb2482feefbf5c3ea25b14c557753b |
| 16 | + |
| 17 | + // with zero data and zero seed, everything becomes zero |
| 18 | + testcase(&[], 0); |
| 19 | + // make sure 4-byte chunks use unsigned math |
| 20 | + testcase(&[0xff, 0xff, 0xff, 0xff], 0x76293B50); |
| 21 | + // Endian order. UInt32 should end up as 0x87654321 |
| 22 | + testcase(&[0x21, 0x43, 0x65, 0x87], 0xF55B516B); |
| 23 | + // Only three bytes. Should end up as 0x654321 |
| 24 | + testcase(&[0x21, 0x43, 0x65], 0x7E4A8634); |
| 25 | + // Only two bytes. Should end up as 0x4321 |
| 26 | + testcase(&[0x21, 0x43], 0xA0F7B07A); |
| 27 | + // Only one byte. Should end up as 0x21 |
| 28 | + testcase(&[0x21], 0x72661CF4); |
| 29 | + // Make sure compiler doesn't see zero and convert to null |
| 30 | + testcase(&[0x00, 0x00, 0x00, 0x00], 0x2362F9DE); |
| 31 | + testcase(&[0x00, 0x00, 0x00], 0x85F0B427); |
| 32 | + testcase(&[0x00, 0x00], 0x30F4C306); |
| 33 | + testcase(&[0x00], 0x514E28B7); |
| 34 | + |
| 35 | + testcase( |
| 36 | + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".as_bytes(), |
| 37 | + 0xEE925B90, |
| 38 | + ); |
| 39 | +} |
0 commit comments