#[must_use]
pub fn compute_seed(description: &str) -> u64 {
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for byte in description.bytes() {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
hash
}
#[must_use]
pub fn resolve_seed(explicit: Option<u64>, description: &str) -> u64 {
explicit.unwrap_or_else(|| compute_seed(description))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compute_seed_deterministic() {
let a = compute_seed("hello:42:3.14");
let b = compute_seed("hello:42:3.14");
assert_eq!(a, b);
}
#[test]
fn test_compute_seed_known_value() {
let seed = compute_seed("test");
assert_eq!(seed, 0xf9e6_e6ef_197c_2b25);
}
#[test]
fn test_compute_seed_different_inputs() {
let a = compute_seed("hello");
let b = compute_seed("world");
assert_ne!(a, b);
}
#[test]
fn test_resolve_seed_explicit() {
let seed = resolve_seed(Some(42), "ignored");
assert_eq!(seed, 42);
}
#[test]
fn test_resolve_seed_derived() {
let seed = resolve_seed(None, "hello");
let expected = compute_seed("hello");
assert_eq!(seed, expected);
}
}