vyre-conform 0.1.0

Conformance suite for vyre backends — proves byte-identical output to CPU reference
Documentation
//! Deterministic test names.

/// Build a deterministic Rust test function name.
#[inline]
pub fn test_name(op: &str, archetype: &str, oracle: &str, seed: u64) -> String {
    format!(
        "test_{}_{}_{}_{}",
        sanitize(op),
        sanitize(archetype),
        sanitize(oracle),
        seed_hash(op, archetype, oracle, seed)
    )
}

/// Convert arbitrary ids into lowercase Rust identifier segments.
#[inline]
pub fn sanitize(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    let mut last_was_underscore = false;
    for ch in input.chars() {
        let next = if ch.is_ascii_alphanumeric() {
            ch.to_ascii_lowercase()
        } else {
            '_'
        };
        if next == '_' {
            if !last_was_underscore {
                out.push(next);
            }
            last_was_underscore = true;
        } else {
            out.push(next);
            last_was_underscore = false;
        }
    }
    let trimmed = out.trim_matches('_');
    if trimmed.is_empty() {
        let hash = simple_hash(input);
        format!("unnamed_{:016x}", hash)
    } else {
        trimmed.to_string()
    }
}

fn simple_hash(input: &str) -> u64 {
    let mut hash = 0xcbf2_9ce4_8422_2325u64;
    for byte in input.bytes() {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    hash
}

/// Stable FNV-1a hash rendered as fixed-width lowercase hex.
#[inline]
pub fn seed_hash(op: &str, archetype: &str, oracle: &str, seed: u64) -> String {
    let mut hash = 0xcbf2_9ce4_8422_2325u64;
    for byte in op
        .bytes()
        .chain([0xff])
        .chain(archetype.bytes())
        .chain([0xfe])
        .chain(oracle.bytes())
        .chain([0xfd])
        .chain(seed.to_le_bytes())
    {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    format!("{hash:016x}")
}