Skip to main content

rajac_base/
hash.rs

1/// Hashes a string using the rapidhash algorithm.
2pub fn hash_string(s: &str) -> u64 {
3    hash_bytes(s.as_bytes())
4}
5
6/// Hashes a byte array using the rapidhash algorithm.
7pub fn hash_bytes(bytes: &[u8]) -> u64 {
8    rapidhash::v3::rapidhash_v3(bytes)
9}
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14
15    #[test]
16    fn hash_string_matches_hash_bytes_for_utf8_input() {
17        let input = "rajac-λ";
18
19        assert_eq!(hash_string(input), hash_bytes(input.as_bytes()));
20    }
21
22    #[test]
23    fn hash_empty_string_matches_empty_bytes() {
24        assert_eq!(hash_string(""), hash_bytes(&[]));
25    }
26
27    #[test]
28    fn hash_changes_for_different_inputs() {
29        let hash_a = hash_string("alpha");
30        let hash_b = hash_string("beta");
31
32        assert_ne!(hash_a, hash_b);
33    }
34
35    #[test]
36    fn hash_string_matches_expected_concrete_values() {
37        assert_eq!(hash_string(""), 232177599295442350);
38        assert_eq!(hash_string("alpha"), 6106490483247698475);
39        assert_eq!(hash_string("beta"), 9996705554587609234);
40        assert_eq!(hash_string("lambda"), 4005924631819820944);
41    }
42
43    #[test]
44    fn hash_bytes_matches_expected_concrete_values() {
45        assert_eq!(hash_bytes(&[]), 232177599295442350);
46        assert_eq!(hash_bytes(b"alpha"), 6106490483247698475);
47    }
48}