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

/// Creates a [`U512`] from a [`U256`] value, embedding it in the lower
/// four limbs `[4..7]` with the upper four limbs `[0..3]` set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
/// use cnfy_uint::u512::U512;
///
/// let v = U256::from_be_limbs([1, 2, 3, 4]);
/// let wide = U512::from(v);
/// assert_eq!(wide, U512::from_be_limbs([0, 0, 0, 0, 1, 2, 3, 4]));
/// ```
impl From<U256> for U512 {
    #[inline]
    fn from(value: U256) -> Self {
        Self([value.0[0], value.0[1], value.0[2], value.0[3], 0, 0, 0, 0])
    }
}

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

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

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

    /// Converting U256::MAX fills only the lower four limbs.
    #[test]
    fn max_u256() {
        assert_eq!(
            U512::from(U256::MAX),
            U512::from_be_limbs([0, 0, 0, 0, u64::MAX, u64::MAX, u64::MAX, u64::MAX]),
        );
    }

    /// All four U256 limbs map to the correct U512 positions.
    #[test]
    fn limb_mapping() {
        let v = U256::from_be_limbs([0xAA, 0xBB, 0xCC, 0xDD]);
        let wide = U512::from(v);
        assert_eq!(
            wide,
            U512::from_be_limbs([0, 0, 0, 0, 0xAA, 0xBB, 0xCC, 0xDD]),
        );
    }
}
