#![cfg(test)] use crate::SmallInt; use crate::SmallUint; #[cfg(feature = "num-bigint")] use num_bigint::{BigInt, BigUint, Sign}; macro_rules! conversion_tests { ($t:ty, $i:ident) => { #[test] fn $i() { let i = <$t>::MAX; let s = SmallInt::from(i); assert_eq!(<$t>::try_from(s).unwrap(), i); let i = <$t>::MIN; let s = SmallInt::from(i); assert_eq!(<$t>::try_from(s).unwrap(), i); } }; } conversion_tests!(u8, test_u8); conversion_tests!(i8, test_i8); conversion_tests!(u16, test_u16); conversion_tests!(i16, test_i16); conversion_tests!(u32, test_u32); conversion_tests!(i32, test_i32); conversion_tests!(u64, test_u64); conversion_tests!(i64, test_i64); conversion_tests!(u128, test_u128); conversion_tests!(i128, test_i128); #[test] #[cfg(feature = "num-bigint")] fn test_cmp_u() { let i = SmallUint::from(30u32); let k = SmallUint::from(50u32); assert!(i < k); let i = SmallUint::from(u128::MAX); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); assert!(i < k); let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1])); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); assert!(i < k); } #[test] #[cfg(feature = "num-bigint")] fn test_cmp_i() { let i = SmallInt::from(30u32); let k = SmallInt::from(50u32); assert!(i < k); let i = SmallInt::from(u128::MAX); let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); assert!(i < k); let i = SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1])); let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); assert!(i < k); let i = SmallInt::from(30u32); let k = -SmallInt::from(50u32); assert!(k < i); let i = SmallInt::from(u128::MAX); let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); assert!(k < i); let i = SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1])); let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); assert!(k < i); } #[test] #[cfg(feature = "num-bigint")] fn test_op_add_u() { let i = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX); let q = i + k; assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) + u128::MAX); let i = SmallUint::from(u128::MAX); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let q = i + k; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) + u128::MAX ); let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1])); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let q = i + k; assert_eq!( BigUint::from(&q), BigUint::new(vec![3, 9, 8, 3, 1]) + BigUint::new(vec![5, 4, 9, 3, 1, 81]) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_add_i() { let i = SmallInt::from(u128::MAX); let k = -SmallInt::from(u128::MAX); let q = i + k; assert_eq!(BigInt::from(&q), BigInt::from(0)); let i = SmallInt::from(u128::MAX); let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); let q = i + k; assert_eq!( BigInt::from(&q), u128::MAX - BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]) ); let i = -SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1])); let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); let q = i + k; assert_eq!( BigInt::from(&q), BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]) - BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1]) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_mul_u() { let i = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX); let q = i * k; assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) * u128::MAX); let i = SmallUint::from(u32::MAX); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let q = i * k; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) * u32::MAX ); let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1])); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let q = i * k; assert_eq!( BigUint::from(&q), BigUint::new(vec![3, 9, 8, 3, 1]) * BigUint::new(vec![5, 4, 9, 3, 1, 81]) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_mul_i() { let i = -SmallInt::from(u128::MAX); let k = SmallInt::from(u128::MAX); let q = i * k; assert_eq!(BigInt::from(&q), -BigInt::from(u128::MAX) * u128::MAX); let i = -SmallInt::from(u32::MAX); let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); let q = i * k; assert_eq!( BigInt::from(&q), BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]) * u32::MAX ); let i = -SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1])); let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); let q = i * k; assert_eq!( BigInt::from(&q), BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1]) * -BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_sub_u() { let i = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX); let q = i - k; assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) - u128::MAX); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let i = SmallUint::from(u128::MAX); let q = k - i; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) - u128::MAX ); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1])); let q = k - i; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) - BigUint::new(vec![3, 9, 8, 3, 1]) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_sub_i() { let i = -SmallInt::from(u128::MAX); let k = SmallInt::from(u128::MAX); let q = i - k; assert_eq!(BigInt::from(&q), -BigInt::from(u128::MAX) - u128::MAX); let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); let i = -SmallInt::from(u128::MAX); let q = k - i; assert_eq!( BigInt::from(&q), BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]) + u128::MAX ); let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])); let i = SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1])); let q = k - i; assert_eq!( BigInt::from(&q), -(BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]) + BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1])) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_and_u() { let i = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX); let q = i & k; assert_eq!( BigUint::from(&q), BigUint::from(u128::MAX) & BigUint::from(u128::MAX) ); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let i = SmallUint::from(u128::MAX); let q = k & i; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3]) & BigUint::from(u128::MAX) ); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1])); let q = k & i; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) & BigUint::new(vec![3, 9, 8, 3, 1]) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_or_u() { let i = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX); let q = i | k; assert_eq!( BigUint::from(&q), BigUint::from(u128::MAX) | BigUint::from(u128::MAX) ); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let i = SmallUint::from(u128::MAX); let q = k | i; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) | BigUint::from(u128::MAX) ); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1])); let q = k | i; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) | BigUint::new(vec![3, 9, 8, 3, 1]) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_xor_u() { let i = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX); let q = i ^ k; assert_eq!( BigUint::from(&q), BigUint::from(u128::MAX) ^ BigUint::from(u128::MAX) ); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let i = SmallUint::from(u128::MAX); let q = k ^ i; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) ^ BigUint::from(u128::MAX) ); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1])); let q = k ^ i; assert_eq!( BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) ^ BigUint::new(vec![3, 9, 8, 3, 1]) ); } #[test] #[cfg(feature = "num-bigint")] fn test_op_neg() { let i = SmallInt::from(u128::MAX); let q = -i; assert_eq!(BigInt::from(&q), -BigInt::from(u128::MAX)); let i = SmallInt::from(i128::MAX); let q = -i; assert_eq!(BigInt::from(&q), -BigInt::from(i128::MAX)); let i = SmallInt::from(i128::MIN); let q = -i; assert_eq!(BigInt::from(&q), -BigInt::from(i128::MIN)); } #[test] #[cfg(feature = "num-bigint")] fn test_conversion_sign_drop() { let si = SmallInt::from(i128::MIN + 1); let i = SmallUint::from_smallint_unsigned(si); assert_eq!(BigUint::from(&i), BigUint::from(-(i128::MIN + 1) as u128)) } #[test] #[cfg(feature = "num-bigint")] fn test_bigint() { let i = BigInt::new(Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3]); let s = SmallInt::from(&i); assert_eq!( BigInt::from(&s).to_u32_digits(), (Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3]) ); }