use super::*;
impl_safe_convert_unsigned_demote!(u128 => u8, u16, u32, u64);
impl_safe_unsigned_convert!(u128 => i8, i16, i32, i64, i128);
impl_safe_convert_unsigned_to_float!(24; u128 => f32);
impl_safe_convert_unsigned_to_float!(53; u128 => f64);
impl_safe_convert_to_int!(u128);
impl_safe_convert_unsigned_to_uint!(u128);
impl_safe_convert_to_decimal_from_large_int!(u128);
#[cfg(test)]
pub mod tests {
use super::SafeConvert;
mod i8 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 42;
let y: Option<i8> = x.checked_convert();
assert_eq!(y, Some(42i8));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = 500;
let y: Option<i8> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = 500;
let y: i8 = x.saturating_convert();
assert_eq!(y, i8::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 500;
let y: i8 = x.wrapping_convert();
assert_eq!(y, -12i8);
}
}
mod i16 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 42;
let y: Option<i16> = x.checked_convert();
assert_eq!(y, Some(42i16));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = 100000;
let y: Option<i16> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = 100000;
let y: i16 = x.saturating_convert();
assert_eq!(y, i16::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 100000;
let y: i16 = x.wrapping_convert();
assert_eq!(y, -31072i16);
}
}
mod i32 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 42;
let y: Option<i32> = x.checked_convert();
assert_eq!(y, Some(42i32));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = 5000000000u128;
let y: Option<i32> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = 5000000000u128;
let y: i32 = x.saturating_convert();
assert_eq!(y, i32::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 5000000000u128;
let y: i32 = x.wrapping_convert();
assert_eq!(y, 705032704i32);
}
}
mod i64 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 42;
let y: Option<i64> = x.checked_convert();
assert_eq!(y, Some(42i64));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = u128::MAX;
let y: Option<i64> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = u128::MAX;
let y: i64 = x.saturating_convert();
assert_eq!(y, i64::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = u128::MAX;
let y: i64 = x.wrapping_convert();
assert_eq!(y, -1i64);
}
}
mod i128 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 42;
let y: Option<i128> = x.checked_convert();
assert_eq!(y, Some(42i128));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = u128::MAX;
let y: Option<i128> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = u128::MAX;
let y: i128 = x.saturating_convert();
assert_eq!(y, i128::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = u128::MAX;
let y: i128 = x.wrapping_convert();
assert_eq!(y, -1i128);
}
}
mod f32 {
use super::*;
#[test]
fn test_checked_convert() {
let x: u128 = 42;
let y: Option<f32> = x.checked_convert();
assert_eq!(y, Some(42.0f32));
}
#[test]
fn test_saturating_convert() {
let x: u128 = 100;
let y: f32 = x.saturating_convert();
assert_eq!(y, 100.0f32);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 1000;
let y: f32 = x.wrapping_convert();
assert_eq!(y, 1000.0f32);
}
#[test]
fn test_checked_convert_overflow() {
let x: u128 = u128::MAX;
let y: Option<f32> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert_overflow() {
let x: u128 = u128::MAX;
let y: f32 = x.saturating_convert();
assert_eq!(y, (1u64 << 24) as f32);
}
#[test]
fn test_wrapping_convert_overflow() {
let x: u128 = u128::MAX;
let y: f32 = x.wrapping_convert();
assert_eq!(y, u128::MAX as f32);
}
}
mod f64 {
use super::*;
#[test]
fn test_checked_convert() {
let x: u128 = 42;
let y: Option<f64> = x.checked_convert();
assert_eq!(y, Some(42.0f64));
}
#[test]
fn test_saturating_convert() {
let x: u128 = 100;
let y: f64 = x.saturating_convert();
assert_eq!(y, 100.0f64);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 1000;
let y: f64 = x.wrapping_convert();
assert_eq!(y, 1000.0f64);
}
#[test]
fn test_checked_convert_overflow() {
let x: u128 = u128::MAX;
let y: Option<f64> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert_overflow() {
let x: u128 = u128::MAX;
let y: f64 = x.saturating_convert();
assert_eq!(y, (1u64 << 53) as f64);
}
#[test]
fn test_wrapping_convert_overflow() {
let x: u128 = u128::MAX;
let y: f64 = x.wrapping_convert();
assert!(y.is_finite());
}
}
mod u8 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 255;
let y: Option<u8> = x.checked_convert();
assert_eq!(y, Some(255u8));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = 256;
let y: Option<u8> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = 1000;
let y: u8 = x.saturating_convert();
assert_eq!(y, u8::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 256;
let y: u8 = x.wrapping_convert();
assert_eq!(y, 0u8);
}
}
mod u16 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 65535;
let y: Option<u16> = x.checked_convert();
assert_eq!(y, Some(65535u16));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = 65536;
let y: Option<u16> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = 100000;
let y: u16 = x.saturating_convert();
assert_eq!(y, u16::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 65536;
let y: u16 = x.wrapping_convert();
assert_eq!(y, 0u16);
}
}
mod u32 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 4294967295;
let y: Option<u32> = x.checked_convert();
assert_eq!(y, Some(4294967295u32));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = 4294967296;
let y: Option<u32> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = u128::MAX;
let y: u32 = x.saturating_convert();
assert_eq!(y, u32::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 4294967296;
let y: u32 = x.wrapping_convert();
assert_eq!(y, 0u32);
}
}
mod u64 {
use super::*;
#[test]
fn test_checked_convert_happy() {
let x: u128 = 18446744073709551615;
let y: Option<u64> = x.checked_convert();
assert_eq!(y, Some(18446744073709551615u64));
}
#[test]
fn test_checked_convert_unhappy() {
let x: u128 = 18446744073709551616;
let y: Option<u64> = x.checked_convert();
assert_eq!(y, None);
}
#[test]
fn test_saturating_convert() {
let x: u128 = u128::MAX;
let y: u64 = x.saturating_convert();
assert_eq!(y, u64::MAX);
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 18446744073709551616;
let y: u64 = x.wrapping_convert();
assert_eq!(y, 0u64);
}
}
mod decimal {
use super::*;
use crate::value::decimal::Decimal;
#[test]
fn test_checked_convert() {
let x: u128 = 42;
let y: Option<Decimal> = x.checked_convert();
assert!(y.is_some());
let decimal = y.unwrap();
assert_eq!(decimal.to_string(), "42");
}
#[test]
fn test_saturating_convert() {
let x: u128 = u128::MAX;
let y: Decimal = x.saturating_convert();
assert_eq!(y.to_string(), "340282366920938463463374607431768211455");
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 1000000000000;
let y: Decimal = x.wrapping_convert();
assert_eq!(y.to_string(), "1000000000000");
}
}
mod int {
use super::*;
use crate::value::int::Int;
#[test]
fn test_checked_convert() {
let x: u128 = u128::MAX;
let y: Option<Int> = x.checked_convert();
assert!(y.is_some());
assert_eq!(y.unwrap().to_string(), "340282366920938463463374607431768211455");
}
#[test]
fn test_saturating_convert() {
let x: u128 = i128::MAX as u128;
let y: Int = x.saturating_convert();
assert_eq!(y.to_string(), "170141183460469231731687303715884105727");
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 0;
let y: Int = x.wrapping_convert();
assert_eq!(y.to_string(), "0");
}
}
mod uint {
use super::*;
use crate::value::uint::Uint;
#[test]
fn test_checked_convert() {
let x: u128 = 42;
let y: Option<Uint> = x.checked_convert();
assert!(y.is_some());
assert_eq!(y.unwrap().to_string(), "42");
}
#[test]
fn test_saturating_convert() {
let x: u128 = u128::MAX;
let y: Uint = x.saturating_convert();
assert_eq!(y.to_string(), "340282366920938463463374607431768211455");
}
#[test]
fn test_wrapping_convert() {
let x: u128 = 1234567890123456789;
let y: Uint = x.wrapping_convert();
assert_eq!(y.to_string(), "1234567890123456789");
}
}
}