//! Conversion from `u128` into [`U256`].
use super::U256;

/// Creates a [`U256`] from a `u128` value, placing the high 64 bits in
/// limb `w2` and the low 64 bits in limb `w3`, with the upper two limbs
/// set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
///
/// let v = U256::from(0x1_0000_0000_0000_0000u128);
/// assert_eq!(v, U256::from_be_limbs([0, 0, 1, 0]));
/// ```
impl From<u128> for U256 {
    #[inline]
    fn from(value: u128) -> Self {
        Self::from_be_limbs([0, 0, (value >> 64) as u64, value as u64])
    }
}

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

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

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

    /// Converting u128::MAX fills the lower two limbs.
    #[test]
    fn max_u128() {
        assert_eq!(
            U256::from(u128::MAX),
            U256::from_be_limbs([0, 0, u64::MAX, u64::MAX]),
        );
    }

    /// Value spanning both u64 halves of the u128.
    #[test]
    fn cross_limb() {
        let v = U256::from(((5u128) << 64) | 7);
        assert_eq!(v, U256::from_be_limbs([0, 0, 5, 7]));
    }

    /// Low 128 bits round-trip correctly.
    #[test]
    fn round_trip_low() {
        let n: u128 = 0xABCD_1234_5678_9ABC_DEF0_1234_5678_9ABC;
        let v = U256::from(n);
        assert_eq!(v.low_u128(), n);
        assert_eq!(v.high_u128(), 0);
    }
}
