From 0aabaa3a154a48e5fb5a71f7c9aacf7879851843 Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Wed, 24 Jul 2024 11:36:29 +0300 Subject: [PATCH] Added `Bounded` numeric trait. commit-id:fa9cd2f7 --- corelib/src/num/traits.cairo | 3 + corelib/src/num/traits/bounded.cairo | 69 +++++ corelib/src/num/traits/ops/saturating.cairo | 12 +- corelib/src/test/num_test.cairo | 266 ++++++++++---------- 4 files changed, 209 insertions(+), 141 deletions(-) create mode 100644 corelib/src/num/traits/bounded.cairo diff --git a/corelib/src/num/traits.cairo b/corelib/src/num/traits.cairo index a5eb6adcf9c..d07f9e697ee 100644 --- a/corelib/src/num/traits.cairo +++ b/corelib/src/num/traits.cairo @@ -7,6 +7,9 @@ pub use one::One; pub mod bit_size; pub use bit_size::BitSize; +mod bounded; +pub use bounded::Bounded; + #[feature("corelib-internal-use")] pub mod ops; pub use ops::overflowing::{OverflowingAdd, OverflowingSub, OverflowingMul}; diff --git a/corelib/src/num/traits/bounded.cairo b/corelib/src/num/traits/bounded.cairo new file mode 100644 index 00000000000..7ee8493b3a3 --- /dev/null +++ b/corelib/src/num/traits/bounded.cairo @@ -0,0 +1,69 @@ +/// A trait defining minimum and maximum bounds for numeric types. +/// Only supports types that can have a constant value. +/// +/// Example: +/// ``` +/// Bounded::::MAX; +/// ``` +pub trait Bounded { + /// The minimum allowable value. + const MIN: T; + /// The maximum allowable value. + const MAX: T; +} + +impl BoundedU8 of Bounded { + const MIN: u8 = 0x0; + const MAX: u8 = 0xff; +} + +impl BoundedU16 of Bounded { + const MIN: u16 = 0x0; + const MAX: u16 = 0xffff; +} + +impl BoundedU32 of Bounded { + const MIN: u32 = 0x0; + const MAX: u32 = 0xffffffff; +} + +impl BoundedU64 of Bounded { + const MIN: u64 = 0x0; + const MAX: u64 = 0xffffffffffffffff; +} + +impl BoundedU128 of Bounded { + const MIN: u128 = 0x0; + const MAX: u128 = 0xffffffffffffffffffffffffffffffff; +} + +impl BoundedU256 of Bounded { + const MIN: u256 = 0x0; + const MAX: u256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; +} + +// Implementations for signed integer types +impl BoundedI8 of Bounded { + const MIN: i8 = -0x80; + const MAX: i8 = 0x7f; +} + +impl BoundedI16 of Bounded { + const MIN: i16 = -0x8000; + const MAX: i16 = 0x7fff; +} + +impl BoundedI32 of Bounded { + const MIN: i32 = -0x80000000; + const MAX: i32 = 0x7fffffff; +} + +impl BoundedI64 of Bounded { + const MIN: i64 = -0x8000000000000000; + const MAX: i64 = 0x7fffffffffffffff; +} + +impl BoundedI128 of Bounded { + const MIN: i128 = -0x80000000000000000000000000000000; + const MAX: i128 = 0x7fffffffffffffffffffffffffffffff; +} diff --git a/corelib/src/num/traits/ops/saturating.cairo b/corelib/src/num/traits/ops/saturating.cairo index ec6706be8c8..dff09f4927f 100644 --- a/corelib/src/num/traits/ops/saturating.cairo +++ b/corelib/src/num/traits/ops/saturating.cairo @@ -21,36 +21,36 @@ pub trait SaturatingMul { pub(crate) mod overflow_based { pub(crate) impl TSaturatingAdd< - T, +Drop, +core::num::traits::OverflowingAdd, +core::integer::BoundedInt + T, +Drop, +core::num::traits::OverflowingAdd, +core::num::traits::Bounded > of core::num::traits::SaturatingAdd { fn saturating_add(self: T, other: T) -> T { let (result, overflow) = self.overflowing_add(other); match overflow { - true => core::integer::BoundedInt::max(), + true => core::num::traits::Bounded::MAX, false => result, } } } pub(crate) impl TSaturatingSub< - T, +Drop, +core::num::traits::OverflowingSub, +core::integer::BoundedInt + T, +Drop, +core::num::traits::OverflowingSub, +core::num::traits::Bounded > of core::num::traits::SaturatingSub { fn saturating_sub(self: T, other: T) -> T { let (result, overflow) = self.overflowing_sub(other); match overflow { - true => core::integer::BoundedInt::min(), + true => core::num::traits::Bounded::MIN, false => result, } } } pub(crate) impl TSaturatingMul< - T, +Drop, +core::num::traits::OverflowingMul, +core::integer::BoundedInt + T, +Drop, +core::num::traits::OverflowingMul, +core::num::traits::Bounded > of core::num::traits::SaturatingMul { fn saturating_mul(self: T, other: T) -> T { let (result, overflow) = self.overflowing_mul(other); match overflow { - true => core::integer::BoundedInt::max(), + true => core::num::traits::Bounded::MAX, false => result, } } diff --git a/corelib/src/test/num_test.cairo b/corelib/src/test/num_test.cairo index 2831947b3a1..f6537013946 100644 --- a/corelib/src/test/num_test.cairo +++ b/corelib/src/test/num_test.cairo @@ -3,7 +3,7 @@ use core::num::traits::{ OverflowingAdd, OverflowingSub, OverflowingMul, WrappingAdd, WrappingSub, WrappingMul, CheckedAdd, CheckedSub, CheckedMul, SaturatingAdd, SaturatingSub, SaturatingMul }; -use core::integer::BoundedInt; +use core::num::traits::Bounded; #[test] @@ -27,108 +27,104 @@ fn test_bit_size() { #[test] fn tests_overflowing_add_unsigned_integers() { assert_eq!(1_u8.overflowing_add(2), (3, false)); - assert_eq!(BoundedInt::::max().overflowing_add(1), (0, true)); + assert_eq!(Bounded::::MAX.overflowing_add(1), (0, true)); assert_eq!(1_u16.overflowing_add(2), (3, false)); - assert_eq!(BoundedInt::::max().overflowing_add(1), (0, true)); + assert_eq!(Bounded::::MAX.overflowing_add(1), (0, true)); assert_eq!(1_u32.overflowing_add(2), (3, false)); - assert_eq!(BoundedInt::::max().overflowing_add(1), (0, true)); + assert_eq!(Bounded::::MAX.overflowing_add(1), (0, true)); assert_eq!(1_u64.overflowing_add(2), (3, false)); - assert_eq!(BoundedInt::::max().overflowing_add(1), (0, true)); + assert_eq!(Bounded::::MAX.overflowing_add(1), (0, true)); assert_eq!(1_u128.overflowing_add(2), (3, false)); - assert_eq!(BoundedInt::::max().overflowing_add(1), (0, true)); + assert_eq!(Bounded::::MAX.overflowing_add(1), (0, true)); assert_eq!(1_u256.overflowing_add(2), (3, false)); - assert_eq!(BoundedInt::::max().overflowing_add(1), (0, true)); + assert_eq!(Bounded::::MAX.overflowing_add(1), (0, true)); } #[test] fn test_overflowing_add_positive_signed_integers() { assert!(1_i8.overflowing_add(2) == (3, false)); - assert!(BoundedInt::::max().overflowing_add(1) == (-0x80, true)); + assert!(Bounded::::MAX.overflowing_add(1) == (-0x80, true)); assert!(1_i16.overflowing_add(2) == (3, false)); - assert!(BoundedInt::::max().overflowing_add(1) == (-0x8000, true)); + assert!(Bounded::::MAX.overflowing_add(1) == (-0x8000, true)); assert!(1_i32.overflowing_add(2) == (3, false)); - assert!(BoundedInt::::max().overflowing_add(1) == (-0x80000000, true)); + assert!(Bounded::::MAX.overflowing_add(1) == (-0x80000000, true)); assert!(1_i64.overflowing_add(2) == (3, false)); - assert!(BoundedInt::::max().overflowing_add(1) == (-0x8000000000000000, true)); + assert!(Bounded::::MAX.overflowing_add(1) == (-0x8000000000000000, true)); assert!(1_i128.overflowing_add(2) == (3, false)); - assert!( - BoundedInt::::max().overflowing_add(1) == (-0x80000000000000000000000000000000, true) - ); + assert!(Bounded::::MAX.overflowing_add(1) == (-0x80000000000000000000000000000000, true)); } #[test] fn test_overflowing_add_negative_signed_integers() { assert!((-1_i8).overflowing_add(-2) == (-3, false)); - assert!(BoundedInt::::min().overflowing_add(-1) == (0x7f, true)); + assert!(Bounded::::MIN.overflowing_add(-1) == (0x7f, true)); assert!((-1_i16).overflowing_add(-2) == (-3, false)); - assert!(BoundedInt::::min().overflowing_add(-1) == (0x7fff, true)); + assert!(Bounded::::MIN.overflowing_add(-1) == (0x7fff, true)); assert!((-1_i32).overflowing_add(-2) == (-3, false)); - assert!(BoundedInt::::min().overflowing_add(-1) == (0x7fffffff, true)); + assert!(Bounded::::MIN.overflowing_add(-1) == (0x7fffffff, true)); assert!((-1_i64).overflowing_add(-2) == (-3, false)); - assert!(BoundedInt::::min().overflowing_add(-1) == (0x7fffffffffffffff, true)); + assert!(Bounded::::MIN.overflowing_add(-1) == (0x7fffffffffffffff, true)); assert!((-1_i128).overflowing_add(-2) == (-3, false)); - assert!( - BoundedInt::::min().overflowing_add(-1) == (0x7fffffffffffffffffffffffffffffff, true) - ); + assert!(Bounded::::MIN.overflowing_add(-1) == (0x7fffffffffffffffffffffffffffffff, true)); } fn test_overflowing_sub_unsigned_integers() { assert_eq!(3_u8.overflowing_sub(2), (1, false)); - assert_eq!(0_u8.overflowing_sub(1), (BoundedInt::::max(), true)); + assert_eq!(0_u8.overflowing_sub(1), (Bounded::::MAX, true)); assert_eq!(3_u16.overflowing_sub(2), (1, false)); - assert_eq!(0_u16.overflowing_sub(1), (BoundedInt::::max(), true)); + assert_eq!(0_u16.overflowing_sub(1), (Bounded::::MAX, true)); assert_eq!(3_u32.overflowing_sub(2), (1, false)); - assert_eq!(0_u32.overflowing_sub(1), (BoundedInt::::max(), true)); + assert_eq!(0_u32.overflowing_sub(1), (Bounded::::MAX, true)); assert_eq!(3_u64.overflowing_sub(2), (1, false)); - assert_eq!(0_u64.overflowing_sub(1), (BoundedInt::::max(), true)); + assert_eq!(0_u64.overflowing_sub(1), (Bounded::::MAX, true)); assert_eq!(3_u128.overflowing_sub(2), (1, false)); - assert_eq!(0_u128.overflowing_sub(1), (BoundedInt::::max(), true)); + assert_eq!(0_u128.overflowing_sub(1), (Bounded::::MAX, true)); assert_eq!(3_u256.overflowing_sub(2), (1, false)); - assert_eq!(0_u256.overflowing_sub(1), (BoundedInt::::max(), true)); + assert_eq!(0_u256.overflowing_sub(1), (Bounded::::MAX, true)); } #[test] fn test_overflowing_sub_positive_signed_integers() { assert!(3_i8.overflowing_sub(2) == (1, false)); - assert!(BoundedInt::::min().overflowing_sub(1) == (BoundedInt::::max(), true)); + assert!(Bounded::::MIN.overflowing_sub(1) == (Bounded::::MAX, true)); assert!(3_i16.overflowing_sub(2) == (1, false)); - assert!(BoundedInt::::min().overflowing_sub(1) == (BoundedInt::::max(), true)); + assert!(Bounded::::MIN.overflowing_sub(1) == (Bounded::::MAX, true)); assert!(3_i32.overflowing_sub(2) == (1, false)); - assert!(BoundedInt::::min().overflowing_sub(1) == (BoundedInt::::max(), true)); + assert!(Bounded::::MIN.overflowing_sub(1) == (Bounded::::MAX, true)); assert!(3_i64.overflowing_sub(2) == (1, false)); - assert!(BoundedInt::::min().overflowing_sub(1) == (BoundedInt::::max(), true)); + assert!(Bounded::::MIN.overflowing_sub(1) == (Bounded::::MAX, true)); assert!(3_i128.overflowing_sub(2) == (1, false)); - assert!(BoundedInt::::min().overflowing_sub(1) == (BoundedInt::::max(), true)); + assert!(Bounded::::MIN.overflowing_sub(1) == (Bounded::::MAX, true)); } #[test] fn test_overflowing_sub_negative_signed_integers() { assert!((-3_i8).overflowing_sub(-2) == (-1, false)); - assert!(BoundedInt::::max().overflowing_sub(-1) == (BoundedInt::::min(), true)); + assert!(Bounded::::MAX.overflowing_sub(-1) == (Bounded::::MIN, true)); assert!((-3_i16).overflowing_sub(-2) == (-1, false)); - assert!(BoundedInt::::max().overflowing_sub(-1) == (BoundedInt::::min(), true)); + assert!(Bounded::::MAX.overflowing_sub(-1) == (Bounded::::MIN, true)); assert!((-3_i32).overflowing_sub(-2) == (-1, false)); - assert!(BoundedInt::::max().overflowing_sub(-1) == (BoundedInt::::min(), true)); + assert!(Bounded::::MAX.overflowing_sub(-1) == (Bounded::::MIN, true)); assert!((-3_i64).overflowing_sub(-2) == (-1, false)); - assert!(BoundedInt::::max().overflowing_sub(-1) == (BoundedInt::::min(), true)); + assert!(Bounded::::MAX.overflowing_sub(-1) == (Bounded::::MIN, true)); assert!((-3_i128).overflowing_sub(-2) == (-1, false)); - assert!(BoundedInt::::max().overflowing_sub(-1) == (BoundedInt::::min(), true)); + assert!(Bounded::::MAX.overflowing_sub(-1) == (Bounded::::MIN, true)); } #[test] fn test_overflowing_mul_unsigned_integers() { assert_eq!(2_u8.overflowing_mul(3), (6, false)); - assert_eq!(BoundedInt::::max().overflowing_mul(2), (BoundedInt::::max() - 1, true)); + assert_eq!(Bounded::::MAX.overflowing_mul(2), (Bounded::::MAX - 1, true)); assert_eq!(2_u16.overflowing_mul(3), (6, false)); - assert_eq!(BoundedInt::::max().overflowing_mul(2), (BoundedInt::::max() - 1, true)); + assert_eq!(Bounded::::MAX.overflowing_mul(2), (Bounded::::MAX - 1, true)); assert_eq!(2_u32.overflowing_mul(3), (6, false)); - assert_eq!(BoundedInt::::max().overflowing_mul(2), (BoundedInt::::max() - 1, true)); + assert_eq!(Bounded::::MAX.overflowing_mul(2), (Bounded::::MAX - 1, true)); assert_eq!(2_u64.overflowing_mul(3), (6, false)); - assert_eq!(BoundedInt::::max().overflowing_mul(2), (BoundedInt::::max() - 1, true)); + assert_eq!(Bounded::::MAX.overflowing_mul(2), (Bounded::::MAX - 1, true)); assert_eq!(2_u128.overflowing_mul(3), (6, false)); - assert_eq!(BoundedInt::::max().overflowing_mul(2), (BoundedInt::::max() - 1, true)); + assert_eq!(Bounded::::MAX.overflowing_mul(2), (Bounded::::MAX - 1, true)); assert_eq!(2_u256.overflowing_mul(3), (6, false)); - assert_eq!(BoundedInt::::max().overflowing_mul(2), (BoundedInt::::max() - 1, true)); + assert_eq!(Bounded::::MAX.overflowing_mul(2), (Bounded::::MAX - 1, true)); } // Wrapping tests @@ -136,150 +132,150 @@ fn test_overflowing_mul_unsigned_integers() { #[test] fn tests_wrapping_add_unsigned_integers() { assert_eq!(1_u8.wrapping_add(2), 3); - assert_eq!(BoundedInt::::max().wrapping_add(1), 0); + assert_eq!(Bounded::::MAX.wrapping_add(1), 0); assert_eq!(1_u16.wrapping_add(2), 3); - assert_eq!(BoundedInt::::max().wrapping_add(1), 0); + assert_eq!(Bounded::::MAX.wrapping_add(1), 0); assert_eq!(1_u32.wrapping_add(2), 3); - assert_eq!(BoundedInt::::max().wrapping_add(1), 0); + assert_eq!(Bounded::::MAX.wrapping_add(1), 0); assert_eq!(1_u64.wrapping_add(2), 3); - assert_eq!(BoundedInt::::max().wrapping_add(1), 0); + assert_eq!(Bounded::::MAX.wrapping_add(1), 0); assert_eq!(1_u128.wrapping_add(2), 3); - assert_eq!(BoundedInt::::max().wrapping_add(1), 0); + assert_eq!(Bounded::::MAX.wrapping_add(1), 0); assert_eq!(1_u256.wrapping_add(2), 3); - assert_eq!(BoundedInt::::max().wrapping_add(1), 0); + assert_eq!(Bounded::::MAX.wrapping_add(1), 0); } #[test] fn test_wrapping_add_positive_signed_integers() { assert!(1_i8.wrapping_add(2) == 3); - assert!(BoundedInt::::max().wrapping_add(1) == -0x80); + assert!(Bounded::::MAX.wrapping_add(1) == -0x80); assert!(1_i16.wrapping_add(2) == 3); - assert!(BoundedInt::::max().wrapping_add(1) == -0x8000); + assert!(Bounded::::MAX.wrapping_add(1) == -0x8000); assert!(1_i32.wrapping_add(2) == 3); - assert!(BoundedInt::::max().wrapping_add(1) == -0x80000000); + assert!(Bounded::::MAX.wrapping_add(1) == -0x80000000); assert!(1_i64.wrapping_add(2) == 3); - assert!(BoundedInt::::max().wrapping_add(1) == -0x8000000000000000); + assert!(Bounded::::MAX.wrapping_add(1) == -0x8000000000000000); assert!(1_i128.wrapping_add(2) == 3); - assert!(BoundedInt::::max().wrapping_add(1) == -0x80000000000000000000000000000000); + assert!(Bounded::::MAX.wrapping_add(1) == -0x80000000000000000000000000000000); } #[test] fn test_wrapping_add_negative_signed_integers() { assert!((-1_i8).wrapping_add(-2) == -3); - assert!(BoundedInt::::min().wrapping_add(-1) == 0x7f); + assert!(Bounded::::MIN.wrapping_add(-1) == 0x7f); assert!((-1_i16).wrapping_add(-2) == -3); - assert!(BoundedInt::::min().wrapping_add(-1) == 0x7fff); + assert!(Bounded::::MIN.wrapping_add(-1) == 0x7fff); assert!((-1_i32).wrapping_add(-2) == -3); - assert!(BoundedInt::::min().wrapping_add(-1) == 0x7fffffff); + assert!(Bounded::::MIN.wrapping_add(-1) == 0x7fffffff); assert!((-1_i64).wrapping_add(-2) == -3); - assert!(BoundedInt::::min().wrapping_add(-1) == 0x7fffffffffffffff); + assert!(Bounded::::MIN.wrapping_add(-1) == 0x7fffffffffffffff); assert!((-1_i128).wrapping_add(-2) == -3); - assert!(BoundedInt::::min().wrapping_add(-1) == 0x7fffffffffffffffffffffffffffffff); + assert!(Bounded::::MIN.wrapping_add(-1) == 0x7fffffffffffffffffffffffffffffff); } #[test] fn test_wrapping_sub_unsigned_integers() { assert_eq!(3_u8.wrapping_sub(2), 1); - assert_eq!(0_u8.wrapping_sub(1), BoundedInt::::max()); + assert_eq!(0_u8.wrapping_sub(1), Bounded::::MAX); assert_eq!(3_u16.wrapping_sub(2), 1); - assert_eq!(0_u16.wrapping_sub(1), BoundedInt::::max()); + assert_eq!(0_u16.wrapping_sub(1), Bounded::::MAX); assert_eq!(3_u32.wrapping_sub(2), 1); - assert_eq!(0_u32.wrapping_sub(1), BoundedInt::::max()); + assert_eq!(0_u32.wrapping_sub(1), Bounded::::MAX); assert_eq!(3_u64.wrapping_sub(2), 1); - assert_eq!(0_u64.wrapping_sub(1), BoundedInt::::max()); + assert_eq!(0_u64.wrapping_sub(1), Bounded::::MAX); assert_eq!(3_u128.wrapping_sub(2), 1); - assert_eq!(0_u128.wrapping_sub(1), BoundedInt::::max()); + assert_eq!(0_u128.wrapping_sub(1), Bounded::::MAX); assert_eq!(3_u256.wrapping_sub(2), 1); - assert_eq!(0_u256.wrapping_sub(1), BoundedInt::::max()); + assert_eq!(0_u256.wrapping_sub(1), Bounded::::MAX); } #[test] fn test_wrapping_sub_positive_signed_integers() { assert!(3_i8.wrapping_sub(2) == 1); - assert!(BoundedInt::::min().wrapping_sub(1) == BoundedInt::::max()); + assert!(Bounded::::MIN.wrapping_sub(1) == Bounded::::MAX); assert!(3_i16.wrapping_sub(2) == 1); - assert!(BoundedInt::::min().wrapping_sub(1) == BoundedInt::::max()); + assert!(Bounded::::MIN.wrapping_sub(1) == Bounded::::MAX); assert!(3_i32.wrapping_sub(2) == 1); - assert!(BoundedInt::::min().wrapping_sub(1) == BoundedInt::::max()); + assert!(Bounded::::MIN.wrapping_sub(1) == Bounded::::MAX); assert!(3_i64.wrapping_sub(2) == 1); - assert!(BoundedInt::::min().wrapping_sub(1) == BoundedInt::::max()); + assert!(Bounded::::MIN.wrapping_sub(1) == Bounded::::MAX); assert!(3_i128.wrapping_sub(2) == 1); - assert!(BoundedInt::::min().wrapping_sub(1) == BoundedInt::::max()); + assert!(Bounded::::MIN.wrapping_sub(1) == Bounded::::MAX); } #[test] fn test_wrapping_sub_negative_signed_integers() { assert!((-3_i8).wrapping_sub(-2) == -1); - assert!(BoundedInt::::max().wrapping_sub(-1) == BoundedInt::::min()); + assert!(Bounded::::MAX.wrapping_sub(-1) == Bounded::::MIN); assert!((-3_i16).wrapping_sub(-2) == -1); - assert!(BoundedInt::::max().wrapping_sub(-1) == BoundedInt::::min()); + assert!(Bounded::::MAX.wrapping_sub(-1) == Bounded::::MIN); assert!((-3_i32).wrapping_sub(-2) == -1); - assert!(BoundedInt::::max().wrapping_sub(-1) == BoundedInt::::min()); + assert!(Bounded::::MAX.wrapping_sub(-1) == Bounded::::MIN); assert!((-3_i64).wrapping_sub(-2) == -1); - assert!(BoundedInt::::max().wrapping_sub(-1) == BoundedInt::::min()); + assert!(Bounded::::MAX.wrapping_sub(-1) == Bounded::::MIN); assert!((-3_i128).wrapping_sub(-2) == -1); - assert!(BoundedInt::::max().wrapping_sub(-1) == BoundedInt::::min()); + assert!(Bounded::::MAX.wrapping_sub(-1) == Bounded::::MIN); } #[test] fn test_wrapping_mul_unsigned_integers() { assert_eq!(2_u8.wrapping_mul(3), 6); - assert_eq!(BoundedInt::::max().wrapping_mul(2), BoundedInt::::max() - 1); + assert_eq!(Bounded::::MAX.wrapping_mul(2), Bounded::::MAX - 1); assert_eq!(2_u16.wrapping_mul(3), 6); - assert_eq!(BoundedInt::::max().wrapping_mul(2), BoundedInt::::max() - 1); + assert_eq!(Bounded::::MAX.wrapping_mul(2), Bounded::::MAX - 1); assert_eq!(2_u32.wrapping_mul(3), 6); - assert_eq!(BoundedInt::::max().wrapping_mul(2), BoundedInt::::max() - 1); + assert_eq!(Bounded::::MAX.wrapping_mul(2), Bounded::::MAX - 1); assert_eq!(2_u64.wrapping_mul(3), 6); - assert_eq!(BoundedInt::::max().wrapping_mul(2), BoundedInt::::max() - 1); + assert_eq!(Bounded::::MAX.wrapping_mul(2), Bounded::::MAX - 1); assert_eq!(2_u128.wrapping_mul(3), 6); - assert_eq!(BoundedInt::::max().wrapping_mul(2), BoundedInt::::max() - 1); + assert_eq!(Bounded::::MAX.wrapping_mul(2), Bounded::::MAX - 1); assert_eq!(2_u256.wrapping_mul(3), 6); - assert_eq!(BoundedInt::::max().wrapping_mul(2), BoundedInt::::max() - 1); + assert_eq!(Bounded::::MAX.wrapping_mul(2), Bounded::::MAX - 1); } // Checked tests #[test] fn test_checked_add_unsigned_integers() { assert_eq!(1_u8.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_u16.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_u32.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_u64.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_u128.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_u256.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); } #[test] fn test_checked_add_positive_signed_integers() { assert_eq!(1_i8.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_i16.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_i32.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_i64.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); assert_eq!(1_i128.checked_add(2), Option::Some(3)); - assert!(BoundedInt::::max().checked_add(1).is_none()); + assert!(Bounded::::MAX.checked_add(1).is_none()); } #[test] fn test_checked_add_negative_signed_integers() { assert_eq!((-1_i8).checked_add(-2), Option::Some(-3)); - assert!(BoundedInt::::min().checked_add(-1).is_none()); + assert!(Bounded::::MIN.checked_add(-1).is_none()); assert_eq!((-1_i16).checked_add(-2), Option::Some(-3)); - assert!(BoundedInt::::min().checked_add(-1).is_none()); + assert!(Bounded::::MIN.checked_add(-1).is_none()); assert_eq!((-1_i32).checked_add(-2), Option::Some(-3)); - assert!(BoundedInt::::min().checked_add(-1).is_none()); + assert!(Bounded::::MIN.checked_add(-1).is_none()); assert_eq!((-1_i64).checked_add(-2), Option::Some(-3)); - assert!(BoundedInt::::min().checked_add(-1).is_none()); + assert!(Bounded::::MIN.checked_add(-1).is_none()); assert_eq!((-1_i128).checked_add(-2), Option::Some(-3)); - assert!(BoundedInt::::min().checked_add(-1).is_none()); + assert!(Bounded::::MIN.checked_add(-1).is_none()); } #[test] @@ -301,75 +297,75 @@ fn test_checked_sub_unsigned_integers() { #[test] fn test_checked_sub_positive_signed_integers() { assert_eq!(3_i8.checked_sub(2), Option::Some(1)); - assert!(BoundedInt::::min().checked_sub(1).is_none()); + assert!(Bounded::::MIN.checked_sub(1).is_none()); assert_eq!(3_i16.checked_sub(2), Option::Some(1)); - assert!(BoundedInt::::min().checked_sub(1).is_none()); + assert!(Bounded::::MIN.checked_sub(1).is_none()); assert_eq!(3_i32.checked_sub(2), Option::Some(1)); - assert!(BoundedInt::::min().checked_sub(1).is_none()); + assert!(Bounded::::MIN.checked_sub(1).is_none()); assert_eq!(3_i64.checked_sub(2), Option::Some(1)); - assert!(BoundedInt::::min().checked_sub(1).is_none()); + assert!(Bounded::::MIN.checked_sub(1).is_none()); assert_eq!(3_i128.checked_sub(2), Option::Some(1)); - assert!(BoundedInt::::min().checked_sub(1).is_none()); + assert!(Bounded::::MIN.checked_sub(1).is_none()); } #[test] fn test_checked_sub_negative_signed_integers() { assert_eq!((-3_i8).checked_sub(-2), Option::Some(-1)); - assert!(BoundedInt::::max().checked_sub(-1).is_none()); + assert!(Bounded::::MAX.checked_sub(-1).is_none()); assert_eq!((-3_i16).checked_sub(-2), Option::Some(-1)); - assert!(BoundedInt::::max().checked_sub(-1).is_none()); + assert!(Bounded::::MAX.checked_sub(-1).is_none()); assert_eq!((-3_i32).checked_sub(-2), Option::Some(-1)); - assert!(BoundedInt::::max().checked_sub(-1).is_none()); + assert!(Bounded::::MAX.checked_sub(-1).is_none()); assert_eq!((-3_i64).checked_sub(-2), Option::Some(-1)); - assert!(BoundedInt::::max().checked_sub(-1).is_none()); + assert!(Bounded::::MAX.checked_sub(-1).is_none()); assert_eq!((-3_i128).checked_sub(-2), Option::Some(-1)); - assert!(BoundedInt::::max().checked_sub(-1).is_none()); + assert!(Bounded::::MAX.checked_sub(-1).is_none()); } #[test] fn test_checked_mul_unsigned_integers() { assert_eq!(2_u8.checked_mul(3), Option::Some(6)); - assert!(BoundedInt::::max().checked_mul(2).is_none()); + assert!(Bounded::::MAX.checked_mul(2).is_none()); assert_eq!(2_u16.checked_mul(3), Option::Some(6)); - assert!(BoundedInt::::max().checked_mul(2).is_none()); + assert!(Bounded::::MAX.checked_mul(2).is_none()); assert_eq!(2_u32.checked_mul(3), Option::Some(6)); - assert!(BoundedInt::::max().checked_mul(2).is_none()); + assert!(Bounded::::MAX.checked_mul(2).is_none()); assert_eq!(2_u64.checked_mul(3), Option::Some(6)); - assert!(BoundedInt::::max().checked_mul(2).is_none()); + assert!(Bounded::::MAX.checked_mul(2).is_none()); assert_eq!(2_u128.checked_mul(3), Option::Some(6)); - assert!(BoundedInt::::max().checked_mul(2).is_none()); + assert!(Bounded::::MAX.checked_mul(2).is_none()); assert_eq!(2_u256.checked_mul(3), Option::Some(6)); - assert!(BoundedInt::::max().checked_mul(2).is_none()); + assert!(Bounded::::MAX.checked_mul(2).is_none()); } #[test] fn test_saturating_add_unsigned_integers() { assert_eq!(1_u8.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_u16.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_u32.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_u64.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_u128.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_u256.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); } #[test] fn test_saturating_add_signed_integers() { assert_eq!(1_i8.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_i16.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_i32.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_i64.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); assert_eq!(1_i128.saturating_add(2), 3); - assert_eq!(BoundedInt::::max().saturating_add(1), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_add(1), Bounded::::MAX); } #[test] @@ -391,29 +387,29 @@ fn test_saturating_sub_unsigned_integers() { #[test] fn test_saturating_sub_signed_integers() { assert_eq!(3_i8.saturating_sub(2), 1); - assert_eq!(BoundedInt::::min().saturating_sub(1), BoundedInt::::min()); + assert_eq!(Bounded::::MIN.saturating_sub(1), Bounded::::MIN); assert_eq!(3_i16.saturating_sub(2), 1); - assert_eq!(BoundedInt::::min().saturating_sub(1), BoundedInt::::min()); + assert_eq!(Bounded::::MIN.saturating_sub(1), Bounded::::MIN); assert_eq!(3_i32.saturating_sub(2), 1); - assert_eq!(BoundedInt::::min().saturating_sub(1), BoundedInt::::min()); + assert_eq!(Bounded::::MIN.saturating_sub(1), Bounded::::MIN); assert_eq!(3_i64.saturating_sub(2), 1); - assert_eq!(BoundedInt::::min().saturating_sub(1), BoundedInt::::min()); + assert_eq!(Bounded::::MIN.saturating_sub(1), Bounded::::MIN); assert_eq!(3_i128.saturating_sub(2), 1); - assert_eq!(BoundedInt::::min().saturating_sub(1), BoundedInt::::min()); + assert_eq!(Bounded::::MIN.saturating_sub(1), Bounded::::MIN); } #[test] fn test_saturating_mul_unsigned_integers() { assert_eq!(2_u8.saturating_mul(3), 6); - assert_eq!(BoundedInt::::max().saturating_mul(2), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_mul(2), Bounded::::MAX); assert_eq!(2_u16.saturating_mul(3), 6); - assert_eq!(BoundedInt::::max().saturating_mul(2), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_mul(2), Bounded::::MAX); assert_eq!(2_u32.saturating_mul(3), 6); - assert_eq!(BoundedInt::::max().saturating_mul(2), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_mul(2), Bounded::::MAX); assert_eq!(2_u64.saturating_mul(3), 6); - assert_eq!(BoundedInt::::max().saturating_mul(2), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_mul(2), Bounded::::MAX); assert_eq!(2_u128.saturating_mul(3), 6); - assert_eq!(BoundedInt::::max().saturating_mul(2), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_mul(2), Bounded::::MAX); assert_eq!(2_u256.saturating_mul(3), 6); - assert_eq!(BoundedInt::::max().saturating_mul(2), BoundedInt::::max()); + assert_eq!(Bounded::::MAX.saturating_mul(2), Bounded::::MAX); }