pub(crate) const FNV1A_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
pub(crate) const FNV1A_PRIME: u64 = 0x0000_0100_0000_01b3;
#[inline]
pub(crate) fn fnv1a_step(acc: u64, value: u64) -> u64 {
(acc ^ value).wrapping_mul(FNV1A_PRIME)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn step_matches_open_coded_fnv1a() {
let reference = |bytes: &[u64]| {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for &b in bytes {
h ^= b;
h = h.wrapping_mul(0x100000001b3);
}
h
};
for seq in [&[1u64, 2, 3][..], &[][..], &[0, 255, 4096], &[u64::MAX, 0]] {
let folded = seq.iter().fold(FNV1A_OFFSET, |acc, &v| fnv1a_step(acc, v));
assert_eq!(
folded,
reference(seq),
"helper must match the open-coded fold"
);
}
}
#[test]
fn step_is_order_sensitive() {
let a = fnv1a_step(fnv1a_step(FNV1A_OFFSET, 1), 2);
let b = fnv1a_step(fnv1a_step(FNV1A_OFFSET, 2), 1);
assert_ne!(a, b, "FNV-1a fold depends on input order");
}
}