grippo 0.1.1

Parse, match and scrub data at scale.
Documentation
// Project:   grippo
// File:      src/kernel/tests.rs
// Purpose:   Tests for the class-run kernel: every tier against the byte loop
// Language:  Rust
//
// License:   Apache-2.0
// Copyright: (c) 2026 HYPERI PTY LIMITED

use proptest::prelude::*;

use super::ByteClass;

/// The run by every path this host can take: the dispatching one, and each
/// vector tier called directly with the byte loop after it.
fn runs(class: &ByteClass, h: &[u8]) -> Vec<(&'static str, usize)> {
    let mut all = vec![("dispatch", class.run(h))];
    #[cfg(target_arch = "x86_64")]
    let tiers = super::x86::tiers();
    #[cfg(all(
        target_arch = "aarch64",
        target_endian = "little",
        target_feature = "neon"
    ))]
    let tiers = super::neon::tiers();
    #[cfg(not(any(
        target_arch = "x86_64",
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "neon"
        )
    )))]
    let tiers: Vec<(&'static str, fn(&[u8; 16], &[u8]) -> usize)> = Vec::new();
    for (name, tier) in tiers {
        let at = tier(&class.low, h);
        // A tier stops at a non-member or where the last whole step ends.
        assert!(at <= h.len(), "{name} went past the slice");
        assert!(
            at == h.len() - h.len() % 16 || !class.has(h[at]),
            "{name} stopped at a member before its last whole step"
        );
        all.push((name, at + class.run_by_table(&h[at..])));
    }
    all
}

/// The reference: a bit test a byte, written apart from the kernel's own.
fn reference(ascii: u128, h: &[u8]) -> usize {
    h.iter()
        .take_while(|&&b| b < 0x80 && ascii >> b & 1 == 1)
        .count()
}

fn agrees(ascii: u128, h: &[u8]) {
    let class = ByteClass::new(ascii);
    let want = reference(ascii, h);
    assert_eq!(class.run_by_table(h), want, "table on {h:?}");
    for (name, got) in runs(&class, h) {
        assert_eq!(got, want, "{name} on {h:?} with set {ascii:#x}");
    }
}

#[cfg(target_arch = "x86_64")]
#[test]
fn a_cpu_with_a_tier_takes_whole_steps_by_vector() {
    // Every run test passes on the table alone, so this is the one that
    // fails if the dispatcher stops handing whole steps to a tier.
    let class = ByteClass::new(u128::MAX);
    let members = [b'a'; 80];
    let by_vector = super::x86::whole_steps(&class.low, &members);
    if std::arch::is_x86_feature_detected!("ssse3") {
        assert_eq!(by_vector, 80);
    } else {
        assert_eq!(by_vector, 0);
    }
    // A non-member inside a step stops the steps there.
    let mut broken = members;
    broken[41] = b' ';
    let class = ByteClass::new(!(1 << b' '));
    let stopped = super::x86::whole_steps(&class.low, &broken);
    assert!(stopped == 41 || stopped == 0, "{stopped}");
}

#[cfg(all(
    target_arch = "aarch64",
    target_endian = "little",
    target_feature = "neon"
))]
#[test]
fn an_aarch64_cpu_takes_whole_steps_by_vector() {
    // Every run test passes on the table alone, so this is the one that
    // fails if the run stops handing whole steps to the tier.
    let class = ByteClass::new(u128::MAX);
    let members = [b'a'; 80];
    assert_eq!(super::neon::run_steps(&class.low, &members), 80);
    let mut broken = members;
    broken[41] = b' ';
    let class = ByteClass::new(!(1 << b' '));
    assert_eq!(super::neon::run_steps(&class.low, &broken), 41);
}

#[test]
fn every_byte_at_every_place_of_a_step_and_its_tail() {
    let sets = [
        0_u128,
        u128::MAX,
        1 << b'a',
        !(1 << b' '),
        (1 << 0x7F) | 1,
        0x5555_5555_5555_5555_5555_5555_5555_5555,
    ];
    for ascii in sets {
        for len in [0, 1, 15, 16, 17, 31, 32, 33, 47, 48, 63, 64, 65] {
            // A member everywhere, then each byte value at each place.
            let member = (0..128_u8).find(|&b| ascii >> b & 1 == 1);
            let fill = member.unwrap_or(b'x');
            agrees(ascii, &vec![fill; len]);
            for at in 0..len {
                for byte in 0..=255_u8 {
                    let mut h = vec![fill; len];
                    h[at] = byte;
                    agrees(ascii, &h);
                }
            }
        }
    }
}

#[test]
fn a_class_holds_the_bytes_its_bits_name() {
    let class = ByteClass::new((1 << b'0') | (1 << b'z') | (1 << 0x7F));
    for byte in 0..=255_u8 {
        let want = matches!(byte, b'0' | b'z' | 0x7F);
        assert_eq!(class.has(byte), want, "{byte:#x}");
    }
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(2000))]

    #[test]
    fn a_run_is_the_byte_loops_run(
        halves in any::<(u64, u64)>(),
        before in proptest::collection::vec(any::<u8>(), 0..40),
        body in proptest::collection::vec(any::<u8>(), 0..300),
        dense in any::<bool>(),
    ) {
        let ascii = u128::from(halves.0) << 64 | u128::from(halves.1);
        // A dense body is mostly members, so runs cross step ends.
        let member = (0..128_u8).find(|&b| ascii >> b & 1 == 1).unwrap_or(0);
        let body: Vec<u8> = body
            .iter()
            .enumerate()
            .map(|(i, &b)| if dense && i % 37 != 36 { member } else { b })
            .collect();
        agrees(ascii, &body);
        // The same bytes at another offset of a longer buffer.
        let mut longer = before.clone();
        longer.extend_from_slice(&body);
        agrees(ascii, &longer[before.len()..]);
    }
}