//! Even parity check for [`U256`] values.

use super::U256;

impl U256 {
    /// Returns `true` if the least significant bit is clear (the value is even).
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u256::U256;
    ///
    /// assert!(U256::ZERO.is_even());
    /// assert!(!U256::ONE.is_even());
    /// assert!(U256::from_be_limbs([0, 0, 0, 2]).is_even());
    /// ```
    #[inline]
    pub fn is_even(&self) -> bool {
        self.0[0] & 1 == 0
    }
}

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

    /// Zero is even.
    #[test]
    fn zero() {
        assert!(U256::ZERO.is_even());
    }

    /// One is not even.
    #[test]
    fn one() {
        assert!(!U256::ONE.is_even());
    }

    /// Two is even.
    #[test]
    fn two() {
        assert!(U256::from_be_limbs([0, 0, 0, 2]).is_even());
    }

    /// MAX is not even.
    #[test]
    fn max() {
        assert!(!U256::MAX.is_even());
    }

    /// is_even is the complement of is_odd.
    #[test]
    fn complement_of_is_odd() {
        for &v in &[U256::ZERO, U256::ONE, U256::MAX, U256::from_be_limbs([0x8000_0000_0000_0000, 0, 0, 0])] {
            assert_eq!(v.is_even(), !v.is_odd());
        }
    }
}
