//! Conversion from [`U256`] into [`U320`].
use super::U320;
use crate::u256::U256;

/// Creates a [`U320`] from a [`U256`] by embedding the four limbs in the
/// lower positions and setting the most significant limb to zero.
///
/// Both types use LE internal layout, so the four limbs are copied
/// directly into positions `[0..3]` with a zero MSB at index 4.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
/// use cnfy_uint::u320::U320;
///
/// let narrow = U256::from_be_limbs([1, 2, 3, 4]);
/// let wide = U320::from(narrow);
/// assert_eq!(wide, U320::from_be_limbs([0, 1, 2, 3, 4]));
/// ```
impl From<U256> for U320 {
    #[inline]
    fn from(value: U256) -> Self {
        Self([value.0[0], value.0[1], value.0[2], value.0[3], 0])
    }
}

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

    /// Zero U256 produces zero U320.
    #[test]
    fn zero() {
        assert_eq!(U320::from(U256::ZERO), U320::ZERO);
    }

    /// One U256 produces one U320.
    #[test]
    fn one() {
        assert_eq!(U320::from(U256::ONE), U320::ONE);
    }

    /// U256::MAX embeds in the lower 4 limbs with zero upper limb.
    #[test]
    fn max_u256() {
        assert_eq!(
            U320::from(U256::MAX),
            U320::from_be_limbs([0, u64::MAX, u64::MAX, u64::MAX, u64::MAX]),
        );
    }

    /// Round-trip through low_u256 recovers the original value.
    #[test]
    fn round_trip_low_u256() {
        let original = U256::from_be_limbs([0xAA, 0xBB, 0xCC, 0xDD]);
        let wide = U320::from(original);
        assert_eq!(wide.low_u256(), original);
    }

    /// The upper limb is always zero.
    #[test]
    fn upper_limb_zero() {
        let wide = U320::from(U256::MAX);
        assert_eq!(wide.to_be_limbs()[0], 0);
    }
}
