instinct-cli 0.3.9

Shared local memory for coding agents. Save a fix once and find it again in a new session or another connected tool.
// generated by hxc 1.0.0-acgt-compiler from src/check/rational.hlx -- do not edit by hand

#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
    let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
    if !ok {
        eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
    }
    ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
        std::process::exit(13);
    }
    use std::os::windows::process::CommandExt;
    c.creation_flags(0x08000000);
    c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
        std::process::exit(13);
    }
    c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
        std::process::exit(13);
    }
    use std::os::windows::process::CommandExt;
    c.creation_flags(0x08000000);
    c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
        std::process::exit(13);
    }
    c
}

// hlx:2
use std::cmp::Ordering;
// hlx:3
#[derive(Debug, Clone, Copy)]
// hlx:4
pub(super) struct Rational {
    numerator: i128,
    denominator: u128,
}
// hlx:5
impl Rational {
    // hlx:6
    pub(super) fn parse(text: &str) -> Option<Self> {
        let (n, d) = text.split_once('/')?;
        let numerator = super::canonical_int(n)?;
        let denominator = super::canonical_int(d)?;
        if denominator <= 0 {
            return None;
        }
        Some(Self {
            numerator,
            denominator: denominator as u128,
        })
    }
    // hlx:7
    /// Continued fractions compare integer quotients and invert positive remainders.
    // hlx:8
    /// Euclidean descent strictly shrinks denominators; at most 256 rounds for u128.
    // hlx:9
    pub(super) fn compare(self, other: Self) -> (Ordering, u64) {
        // hlx:10
        if self.numerator.is_negative() != other.numerator.is_negative() {
            return (self.numerator.cmp(&other.numerator), 1);
        }
        // hlx:11
        let (mut a, mut b, mut c, mut d) = (
            self.numerator.unsigned_abs(),
            self.denominator,
            other.numerator.unsigned_abs(),
            other.denominator,
        );
        // hlx:12
        let mut reverse = self.numerator.is_negative();
        let mut steps = 0;
        // hlx:13
        loop {
            steps += 1;
            let order = (a / b).cmp(&(c / d));
            if order != Ordering::Equal {
                return (if reverse { order.reverse() } else { order }, steps);
            }
            let (ar, cr) = (a % b, c % d);
            if ar == 0 || cr == 0 {
                let order = ar.cmp(&cr);
                return (if reverse { order.reverse() } else { order }, steps);
            }
            (a, b, c, d) = (b, ar, d, cr);
            reverse = !reverse;
        }
        // hlx:14
    }
    // hlx:15
}
// hlx:16
#[cfg(test)]
// hlx:17
mod tests {
    // hlx:18
    use super::*;
    // hlx:19
    #[test]
    fn exhaustive_small_fractions_match_cross_product_oracle() {
        for a in -12i128..=12 {
            for b in 1u128..=12 {
                for c in -12i128..=12 {
                    for d in 1u128..=12 {
                        let (x, steps) = Rational {
                            numerator: a,
                            denominator: b,
                        }
                        .compare(Rational {
                            numerator: c,
                            denominator: d,
                        });
                        assert_eq!(x, (a * d as i128).cmp(&(c * b as i128)));
                        assert!((1..=256).contains(&steps));
                    }
                }
            }
        }
    }
    // hlx:20
    #[test]
    fn extremes_never_multiply_or_negate_min() {
        let min = Rational {
            numerator: i128::MIN,
            denominator: 1,
        };
        let max = Rational {
            numerator: i128::MAX,
            denominator: 1,
        };
        assert_eq!(min.compare(max).0, Ordering::Less);
        assert_eq!(min.compare(min).0, Ordering::Equal);
        let x = Rational {
            numerator: i128::MAX,
            denominator: i128::MAX as u128 - 1,
        };
        let y = Rational {
            numerator: i128::MAX - 1,
            denominator: i128::MAX as u128 - 2,
        };
        assert_eq!(x.compare(y).0, Ordering::Less);
    }
    // hlx:21
    #[test]
    fn parsing_is_strict() {
        for bad in [
            "1",
            "1/0",
            "1/-2",
            "01/2",
            "-0/2",
            "1/+2",
            "1/02",
            "1/2/3",
            " 1/2",
            "1/2.0",
            "1e2/3",
            "1/170141183460469231731687303715884105728",
        ] {
            assert!(Rational::parse(bad).is_none(), "{bad}");
        }
        for good in [
            "0/1",
            "-1/2",
            "2/4",
            "-170141183460469231731687303715884105728/1",
        ] {
            assert!(Rational::parse(good).is_some());
        }
    }
    // hlx:22
    #[test]
    fn equivalent_forms_compare_equal() {
        assert_eq!(
            Rational::parse("2/4")
                .unwrap()
                .compare(Rational::parse("1/2").unwrap())
                .0,
            Ordering::Equal
        );
        assert_eq!(
            Rational::parse("0/17")
                .unwrap()
                .compare(Rational::parse("0/1").unwrap())
                .0,
            Ordering::Equal
        );
    }
    // hlx:23
}