//! Bitwise OR via the [`BitOr`] trait.
use super::U320;
use core::ops::BitOr;

/// Computes the bitwise OR of two 320-bit integers, producing a
/// result where each bit is set if either corresponding input bit
/// is set.
///
/// Applied independently to each of the five `u64` limbs.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u320::U320;
///
/// let a = U320::from_be_limbs([0xF0, 0, 0, 0, 0]);
/// let b = U320::from_be_limbs([0x0F, 0, 0, 0, 0]);
/// assert_eq!(a | b, U320::from_be_limbs([0xFF, 0, 0, 0, 0]));
/// ```
impl BitOr for U320 {
    type Output = U320;

    #[inline]
    fn bitor(self, rhs: U320) -> U320 {
        U320([
            self.0[0] | rhs.0[0],
            self.0[1] | rhs.0[1],
            self.0[2] | rhs.0[2],
            self.0[3] | rhs.0[3],
            self.0[4] | rhs.0[4],
        ])
    }
}

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

    /// OR with self is identity.
    #[test]
    fn self_identity() {
        let a = U320::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111]);
        assert_eq!(a | a, a);
    }

    /// OR with zero is identity.
    #[test]
    fn or_zero() {
        let a = U320::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111]);
        assert_eq!(a | U320::ZERO, a);
    }

    /// OR with MAX is MAX.
    #[test]
    fn or_max() {
        let a = U320::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111]);
        assert_eq!(a | U320::MAX, U320::MAX);
    }

    /// OR combines bits from both operands.
    #[test]
    fn combines_bits() {
        let a = U320::from_be_limbs([0xF0, 0, 0, 0, 0]);
        let b = U320::from_be_limbs([0x0F, 0, 0, 0, 0]);
        assert_eq!(a | b, U320::from_be_limbs([0xFF, 0, 0, 0, 0]));
    }

    /// OR is commutative.
    #[test]
    fn commutative() {
        let a = U320::from_be_limbs([1, 2, 3, 4, 5]);
        let b = U320::from_be_limbs([5, 6, 7, 8, 9]);
        assert_eq!(a | b, b | a);
    }
}
