Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
The tests compute some exact hash results that we expect for several
different integers on bytes. Since rustc_hash only sensibly supports 32 bit and
64 bit and is usize-dependent, we test them both.
Running the test suite on a different bit width will result in an error.
  • Loading branch information
Noratrieb committed Jan 13, 2024
1 parent f01869b commit 96dfdd7
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,123 @@ impl Hasher for FxHasher {
self.hash as u64
}
}

#[cfg(test)]
mod tests {
#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))]
compile_error!("The test suite only supports 64 bit and 32 bit usize");

use crate::FxHasher;
use core::hash::{BuildHasher, BuildHasherDefault, Hash};

macro_rules! test_hash {
(
$(
hash($value:expr) == $result:expr,
)*
) => {
$(
assert_eq!(BuildHasherDefault::<FxHasher>::default().hash_one($value), $result);
)*
};
}

const B32: bool = cfg!(target_pointer_width = "32");

#[test]
fn unsigned() {
test_hash! {
hash(0_u8) == 0,
hash(1_u8) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(100_u8) == if B32 { 3450571844 } else { 15329034371404145204 },
hash(u8::MAX) == if B32 { 2571255623 } else { 3117886703346944619 },

hash(0_u16) == 0,
hash(1_u16) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(100_u16) == if B32 { 3450571844 } else { 15329034371404145204 },
hash(u16::MAX) == if B32 { 3682698823 } else { 8086887590654047595 },

hash(0_u32) == 0,
hash(1_u32) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(100_u32) == if B32 { 3450571844 } else { 15329034371404145204 },
hash(u32::MAX) == if B32 { 1640531527 } else { 15394791018899305835 },

hash(0_u64) == 0,
hash(1_u64) == if B32 { 703266523 } else { 5871781006564002453 },
hash(100_u64) == if B32 { 2407204753 } else { 15329034371404145204 },
hash(u64::MAX) == if B32 { 1660667835 } else { 12574963067145549163 },

hash(0_u128) == 0,
hash(1_u128) == if B32 { 1294492036 } else { 956286968014291186 },
hash(100_u128) == if B32 { 3411300242 } else { 2770938889503972258 },
hash(u128::MAX) == if B32 { 3723263291 } else { 15973479568771280466 },

hash(0_usize) == 0,
hash(1_usize) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(100_usize) == if B32 { 3450571844 } else { 15329034371404145204 },
hash(usize::MAX) == if B32 { 1640531527 } else { 12574963067145549163 },
}
}

#[test]
fn signed() {
test_hash! {
hash(i8::MIN) == if B32 { 465362048 } else { 13718205891810249344 },
hash(0_i8) == 0,
hash(1_i8) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(100_i8) == if B32 { 3450571844 } else { 15329034371404145204 },
hash(i8::MAX) == if B32 { 2105893575 } else { 7846424885246246891 },

hash(i16::MIN) == if B32 { 3168567296 } else { 6979334298609025024 },
hash(0_i16) == 0,
hash(1_i16) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(100_i16) == if B32 { 3450571844 } else { 15329034371404145204 },
hash(i16::MAX) == if B32 { 514131527 } else { 1107553292045022571 },

hash(i32::MIN) == if B32 { 2147483648 } else { 10633286012731654144 },
hash(0_i32) == 0,
hash(1_i32) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(100_i32) == if B32 { 3450571844 } else { 15329034371404145204 },
hash(i32::MAX) == if B32 { 3788015175 } else { 4761505006167651691 },

hash(i64::MIN) == if B32 { 2147483648 } else { 9223372036854775808 },
hash(0_i64) == 0,
hash(1_i64) == if B32 { 703266523 } else { 5871781006564002453 },
hash(100_i64) == if B32 { 2407204753 } else { 15329034371404145204 },
hash(i64::MAX) == if B32 { 3808151483 } else { 3351591030290773355 },

hash(i128::MIN) == if B32 { 2147483648 } else { 9223372036854775808 },
hash(0_i128) == 0,
hash(1_i128) == if B32 { 1294492036 } else { 956286968014291186 },
hash(100_i128) == if B32 { 3411300242 } else { 2770938889503972258 },
hash(i128::MAX) == if B32 { 1575779643 } else { 6750107531916504658 },

hash(isize::MIN) == if B32 { 2147483648 } else { 9223372036854775808 },
hash(0_isize) == 0,
hash(1_isize) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(100_isize) == if B32 { 3450571844 } else { 15329034371404145204 },
hash(isize::MAX) == if B32 { 3788015175 } else { 3351591030290773355 },
}
}

// Avoid relying on any `Hash` implementations in the standard library.
struct HashBytes(&'static [u8]);
impl Hash for HashBytes {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
state.write(self.0);
}
}

#[test]
fn bytes() {
test_hash! {
hash(HashBytes(&[])) == 0,
hash(HashBytes(&[0])) == 0,
hash(HashBytes(&[0, 0, 0, 0, 0, 0])) == 0,
hash(HashBytes(&[1])) == if B32 { 2654435769 } else { 5871781006564002453 },
hash(HashBytes(&[2])) == if B32 { 1013904242 } else { 11743562013128004906 },
hash(HashBytes(b"uwu")) == if B32 { 3939043750 } else { 16622306935539548858 },
hash(HashBytes(b"These are some bytes for testing rustc_hash.")) == if B32 { 2345708736 } else { 12390864548135261390 },
}
}
}

0 comments on commit 96dfdd7

Please sign in to comment.