grippo 0.3.0

Grok, regex, CEL and scrubbing on typed primitives. Up to 9 times faster than the stock Rust regex.
Documentation
// Project:   grippo
// File:      src/number/tests.rs
// Purpose:   Tests for reading a number's text under each reading
// Language:  Rust
//
// License:   Apache-2.0
// Copyright: (c) 2026 HYPERI PTY LIMITED

use proptest::prelude::*;

use super::{Number, NumberReading};

#[cfg(all(feature = "convert", feature = "parse", feature = "record"))]
mod table;

/// A reading as its kind and value bits, so `-0.0` isn't taken for `0.0`.
fn bits(number: Option<Number>) -> Option<(char, u64)> {
    Some(match number? {
        Number::Int(n) => ('i', n.cast_unsigned()),
        Number::UInt(n) => ('u', n),
        Number::Float(n) => ('f', n.to_bits()),
    })
}

fn same(a: Option<Number>, b: Option<Number>) -> bool {
    bits(a) == bits(b)
}

#[test]
fn the_readings_differ_only_past_the_integer_range() {
    use Number::{Float, Int, UInt};
    let wide_digits = "9".repeat(400);
    // Text, then what Exact reads, then what Cel reads.
    let cases: &[(&str, Option<Number>, Option<Number>)] = &[
        ("0", Some(Int(0)), Some(Int(0))),
        ("42", Some(Int(42)), Some(Int(42))),
        ("-7", Some(Int(-7)), Some(Int(-7))),
        ("-0", Some(Int(0)), Some(Float(-0.0))),
        ("+0", Some(Int(0)), Some(Int(0))),
        ("+5", Some(Int(5)), Some(Int(5))),
        ("007", Some(Int(7)), Some(Int(7))),
        (
            "9223372036854775807",
            Some(Int(i64::MAX)),
            Some(Int(i64::MAX)),
        ),
        (
            "9223372036854775808",
            Some(UInt(1 << 63)),
            Some(UInt(1 << 63)),
        ),
        (
            "18446744073709551615",
            Some(UInt(u64::MAX)),
            Some(UInt(u64::MAX)),
        ),
        (
            "000000000000000000000018446744073709551615",
            Some(UInt(u64::MAX)),
            Some(UInt(u64::MAX)),
        ),
        (
            "-9223372036854775808",
            Some(Int(i64::MIN)),
            Some(Int(i64::MIN)),
        ),
        (
            "18446744073709551616",
            None,
            Some(Float(18_446_744_073_709_551_616.0)),
        ),
        (
            "+18446744073709551616",
            None,
            Some(Float(18_446_744_073_709_551_616.0)),
        ),
        (
            "-9223372036854775809",
            None,
            Some(Float(-9_223_372_036_854_775_808.0)),
        ),
        (
            "9007199254740993",
            Some(Int(9_007_199_254_740_993)),
            Some(Int(9_007_199_254_740_993)),
        ),
        ("1.5", Some(Float(1.5)), Some(Float(1.5))),
        ("1.0", Some(Float(1.0)), Some(Float(1.0))),
        ("1e5", Some(Float(1e5)), Some(Float(1e5))),
        ("1E+3", Some(Float(1000.0)), Some(Float(1000.0))),
        (".5", Some(Float(0.5)), Some(Float(0.5))),
        ("5.", Some(Float(5.0)), Some(Float(5.0))),
        ("-0.0", Some(Float(-0.0)), Some(Float(-0.0))),
        ("1e-9999", Some(Float(0.0)), Some(Float(0.0))),
        ("-1e-9999", Some(Float(-0.0)), Some(Float(-0.0))),
        ("1e309", None, None),
        ("-1e309", None, None),
        (&wide_digits, None, None),
        ("", None, None),
        ("-", None, None),
        ("+", None, None),
        (".", None, None),
        ("e5", None, None),
        ("1e", None, None),
        ("1e+", None, None),
        ("--1", None, None),
        ("1+2", None, None),
        ("1.5.2", None, None),
        (" 1", None, None),
        ("1 ", None, None),
        ("1\n", None, None),
        ("0x10", None, None),
        ("1_000", None, None),
        ("1,5", None, None),
        ("inf", None, None),
        ("-inf", None, None),
        ("infinity", None, None),
        ("NaN", None, None),
        ("nan", None, None),
        ("\u{661}\u{662}", None, None),
    ];
    for (text, exact, cel) in cases {
        let got = NumberReading::Exact.read(text);
        assert!(same(got, *exact), "Exact {text}: {got:?}, want {exact:?}");
        let got = NumberReading::Cel.read(text);
        assert!(same(got, *cel), "Cel {text}: {got:?}, want {cel:?}");
    }
}

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

    #[test]
    fn every_i64_reads_the_same_under_both(n in any::<i64>()) {
        let want = Some(Number::Int(n));
        for reading in [NumberReading::Exact, NumberReading::Cel] {
            prop_assert!(same(reading.read(&n.to_string()), want), "{:?} {}", reading, n);
        }
    }

    #[test]
    fn every_u64_reads_the_same_under_both(n in any::<u64>()) {
        let want = Some(i64::try_from(n).map_or(Number::UInt(n), Number::Int));
        for reading in [NumberReading::Exact, NumberReading::Cel] {
            prop_assert!(same(reading.read(&n.to_string()), want), "{:?} {}", reading, n);
        }
    }

    /// Past `u64` the digits have no `Exact` value, and `Cel` rounds them.
    #[test]
    #[allow(
        clippy::cast_precision_loss,
        reason = "rounding to the nearest double is the point"
    )]
    fn a_whole_number_past_u64_is_rounded_only_by_cel(
        n in (u128::from(u64::MAX) + 1)..=u128::MAX,
        negative in any::<bool>(),
    ) {
        let text = if negative { format!("-{n}") } else { n.to_string() };
        prop_assert_eq!(NumberReading::Exact.read(&text), None);
        let rounded = if negative { -(n as f64) } else { n as f64 };
        prop_assert!(
            same(NumberReading::Cel.read(&text), Some(Number::Float(rounded))),
            "{}",
            text
        );
    }

    /// Below `i64::MIN` is past the range too.
    #[test]
    #[allow(
        clippy::cast_precision_loss,
        reason = "rounding to the nearest double is the point"
    )]
    fn a_whole_number_below_i64_min_is_rounded_only_by_cel(
        n in (i128::from(i64::MIN) - (1_i128 << 100))..i128::from(i64::MIN),
    ) {
        let text = n.to_string();
        prop_assert_eq!(NumberReading::Exact.read(&text), None);
        prop_assert!(
            same(NumberReading::Cel.read(&text), Some(Number::Float(n as f64))),
            "{}",
            text
        );
    }

    #[test]
    fn every_finite_f64_reads_the_same_under_both(
        x in any::<f64>().prop_filter("finite", |x| x.is_finite()),
    ) {
        // `{x}` writes a large float out whole, which is whole text, so only a
        // spelling with a point or an exponent is a float.
        for text in [format!("{x}"), format!("{x:e}"), format!("{x:E}")] {
            if !text.contains(['.', 'e', 'E']) {
                continue;
            }
            for reading in [NumberReading::Exact, NumberReading::Cel] {
                let got = reading.read(&text);
                prop_assert!(
                    same(got, Some(Number::Float(x))),
                    "{:?} {}: {:?}",
                    reading,
                    text,
                    got
                );
            }
        }
    }

    /// Any text, whatever it holds, reads under both without a panic, and the
    /// two differ only where `Cel` rounds a whole number or reads `-0`.
    #[test]
    fn any_text_reads_and_the_readings_differ_only_on_rounding(text in "[-+.eE0-9 xnaif_,]{0,24}") {
        let exact = NumberReading::Exact.read(&text);
        let cel = NumberReading::Cel.read(&text);
        if !same(exact, cel) {
            prop_assert!(matches!(cel, Some(Number::Float(_))), "{}", text);
            let digits = text.strip_prefix(['+', '-']).unwrap_or(&text);
            prop_assert!(
                !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit()),
                "{}",
                text
            );
            // Rounded past `i64` and `u64`, or a negative zero.
            let zero = text.starts_with('-') && digits.bytes().all(|b| b == b'0');
            prop_assert!(exact.is_none() || zero, "{}", text);
        }
    }
}