//! Parity check for [`U512`] values.
use super::U512;

impl U512 {
    /// Returns `true` if the least significant bit is set (the value is odd).
    ///
    /// Checks bit 0 of the least significant limb (index 0).
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u512::U512;
    ///
    /// assert!(!U512::ZERO.is_odd());
    /// assert!(U512::ONE.is_odd());
    /// assert!(U512::MAX.is_odd());
    /// ```
    #[inline]
    pub fn is_odd(&self) -> bool {
        self.0[0] & 1 == 1
    }
}

#[cfg(test)]
mod ai_tests {
    use super::*;

    /// Zero is even.
    #[test]
    fn zero_is_even() {
        assert!(!U512::ZERO.is_odd());
    }

    /// One is odd.
    #[test]
    fn one_is_odd() {
        assert!(U512::ONE.is_odd());
    }

    /// Two is even.
    #[test]
    fn two_is_even() {
        assert!(!U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 2]).is_odd());
    }

    /// MAX (all bits set) is odd.
    #[test]
    fn max_is_odd() {
        assert!(U512::MAX.is_odd());
    }

    /// A large even number with only the MSB limb set.
    #[test]
    fn large_even() {
        let v = U512::from_be_limbs([0x8000_0000_0000_0000, 0, 0, 0, 0, 0, 0, 0]);
        assert!(!v.is_odd());
    }

    /// A large odd number with MSB and LSB set.
    #[test]
    fn large_odd() {
        let v = U512::from_be_limbs([0x8000_0000_0000_0000, 0, 0, 0, 0, 0, 0, 1]);
        assert!(v.is_odd());
    }
}
