pub fn djb2(bytes: &[u8]) -> u32 {
let mut hash: u32 = 5381;
for &byte in bytes {
hash = hash.wrapping_mul(33).wrapping_add(byte as u32);
}
hash
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_djb2() {
let result = djb2(b"hello");
assert_eq!(result, 261238937);
let empty = djb2(&[]);
assert_eq!(empty, 5381);
}
#[test]
fn test_djb2_consistency() {
let data = b"test data";
let hash1 = djb2(data);
let hash2 = djb2(data);
assert_eq!(hash1, hash2);
}
}