diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index 78ee1b87..4bbef72e 100644 --- a/sway_libs/src/signed_integers/i128.sw +++ b/sway_libs/src/signed_integers/i128.sw @@ -19,6 +19,12 @@ impl I128 { lower: 0, } } + + pub fn zero() -> Self { + Self { + underlying: U128::from((0, 0)), + } + } } impl From for I128 { @@ -51,6 +57,28 @@ impl core::ops::Ord for I128 { } impl I128 { + pub fn ge(self, other: Self) -> bool { + self > other || self == other + } + + pub fn le(self, other: Self) -> bool { + self < other || self == other + } + + pub fn from_u64(value: u64) -> I128 { + Self { + underlying: U128::from((0, value)), + } + } + + pub fn as_u64(self) -> u64 { + if self.underlying < Self::indent() { + revert(0) + } else { + self.underlying.as_u64().unwrap() + } + } + /// The size of this type in bits. pub fn bits() -> u32 { 128 @@ -169,3 +197,9 @@ impl core::ops::Subtract for I128 { res } } + +impl I128 { + pub fn flip(self) -> Self { + self * Self::neg_from(U128::from((0, 1))) + } +} \ No newline at end of file diff --git a/sway_libs/src/signed_integers/i16.sw b/sway_libs/src/signed_integers/i16.sw index 38f7a972..72bafc0f 100644 --- a/sway_libs/src/signed_integers/i16.sw +++ b/sway_libs/src/signed_integers/i16.sw @@ -47,6 +47,14 @@ impl core::ops::Ord for I16 { } impl I16 { + pub fn ge(self, other: Self) -> bool { + self > other || self == other + } + + pub fn le(self, other: Self) -> bool { + self < other || self == other + } + /// The size of this type in bits. pub fn bits() -> u32 { 16 @@ -159,3 +167,9 @@ impl core::ops::Subtract for I16 { res } } + +impl I16{ + pub fn flip(self) -> Self { + self * Self::neg_from(1) + } +} \ No newline at end of file diff --git a/sway_libs/src/signed_integers/i256.sw b/sway_libs/src/signed_integers/i256.sw index 9e847b20..89f92e3c 100644 --- a/sway_libs/src/signed_integers/i256.sw +++ b/sway_libs/src/signed_integers/i256.sw @@ -21,6 +21,12 @@ impl I256 { d: 0, } } + + pub fn zero() -> Self { + Self { + underlying: U256::from((0, 0, 0, 0)), + } + } } impl From for I256 { @@ -52,6 +58,28 @@ impl core::ops::Ord for I256 { } impl I256 { + pub fn ge(self, other: Self) -> bool { + self > other || self == other + } + + pub fn le(self, other: Self) -> bool { + self < other || self == other + } + + pub fn from_u64(value: u64) -> I256 { + Self { + underlying: U256::from((0, 0, 0, value)), + } + } + + pub fn as_u64(self) -> u64 { + if self.underlying < Self::indent() { + revert(0) + } else { + self.underlying.as_u64().unwrap() + } + } + /// The size of this type in bits. pub fn bits() -> u32 { 128 @@ -170,3 +198,9 @@ impl core::ops::Subtract for I256 { res } } + +impl I256 { + pub fn flip(self) -> Self { + self * Self::neg_from(U256::from((0, 0, 0, 1))) + } +} \ No newline at end of file diff --git a/sway_libs/src/signed_integers/i32.sw b/sway_libs/src/signed_integers/i32.sw index 038d5485..c1eaa3df 100644 --- a/sway_libs/src/signed_integers/i32.sw +++ b/sway_libs/src/signed_integers/i32.sw @@ -46,6 +46,14 @@ impl core::ops::Ord for I32 { } impl I32 { + pub fn ge(self, other: Self) -> bool { + self > other || self == other + } + + pub fn le(self, other: Self) -> bool { + self < other || self == other + } + /// The size of this type in bits. pub fn bits() -> u32 { 32 @@ -158,3 +166,9 @@ impl core::ops::Divide for I32 { res } } + +impl I32{ + pub fn flip(self) -> Self { + self * Self::neg_from(1) + } +} \ No newline at end of file diff --git a/sway_libs/src/signed_integers/i64.sw b/sway_libs/src/signed_integers/i64.sw index a53ad78a..d75404a7 100644 --- a/sway_libs/src/signed_integers/i64.sw +++ b/sway_libs/src/signed_integers/i64.sw @@ -46,6 +46,14 @@ impl core::ops::Ord for I64 { } impl I64 { + pub fn ge(self, other: Self) -> bool { + self > other || self == other + } + + pub fn le(self, other: Self) -> bool { + self < other || self == other + } + /// The size of this type in bits. pub fn bits() -> u32 { 64 @@ -158,3 +166,9 @@ impl core::ops::Divide for I64 { res } } + +impl I64{ + pub fn flip(self) -> Self { + self * Self::neg_from(1) + } +} \ No newline at end of file diff --git a/sway_libs/src/signed_integers/i8.sw b/sway_libs/src/signed_integers/i8.sw index 628abe5b..04d0a703 100644 --- a/sway_libs/src/signed_integers/i8.sw +++ b/sway_libs/src/signed_integers/i8.sw @@ -46,6 +46,14 @@ impl core::ops::Ord for I8 { } impl I8 { + pub fn ge(self, other: Self) -> bool { + self > other || self == other + } + + pub fn le(self, other: Self) -> bool { + self < other || self == other + } + /// The size of this type in bits. pub fn bits() -> u32 { 8 @@ -155,3 +163,9 @@ impl core::ops::Subtract for I8 { res } } + +impl I8{ + pub fn flip(self) -> Self { + self * Self::neg_from(1) + } +} \ No newline at end of file diff --git a/tests/src/test_projects/signed_i128/src/main.sw b/tests/src/test_projects/signed_i128/src/main.sw index 46994ff7..58b60480 100644 --- a/tests/src/test_projects/signed_i128/src/main.sw +++ b/tests/src/test_projects/signed_i128/src/main.sw @@ -58,5 +58,24 @@ fn main() -> bool { res = I128::from(u128_10) / I128::from(u128_5); assert(res == I128::from(u128_2)); + //from_u64 test + assert(one == I128::from_u64(1)); + //as_u64 test + assert(1 == one.as_u64()); + //flip test + assert(one.flip() == I128::neg_from(u128_one)); + //ge test + assert(one >= one); + assert(I128::from_u64(2) >= one); + assert(one >= one.flip()); + assert(one.flip() >= I128::from_u64(2).flip()); + //le test + assert(one <= one); + assert(one <= I128::from_u64(2)); + assert(one.flip() <= one); + assert(I128::from_u64(2).flip() <= one.flip()); + //zero test + assert(I128::zero() == I128::from_u64(0)); + true } diff --git a/tests/src/test_projects/signed_i128/tests/mod.rs b/tests/src/test_projects/signed_i128/tests/mod.rs index 3184be9e..1d259cf1 100644 --- a/tests/src/test_projects/signed_i128/tests/mod.rs +++ b/tests/src/test_projects/signed_i128/tests/mod.rs @@ -16,6 +16,8 @@ mod success { let instance = Testi128::new(wallet, path_to_bin); - let _result = instance.main().call().await; + let params = TxParameters::new(Some(1), Some(10_000_000), None); + let result = instance.main().tx_params(params).call().await; + assert_eq!(result.is_err(), false); } } diff --git a/tests/src/test_projects/signed_i16/src/main.sw b/tests/src/test_projects/signed_i16/src/main.sw index 189e057f..c76aafc0 100644 --- a/tests/src/test_projects/signed_i16/src/main.sw +++ b/tests/src/test_projects/signed_i16/src/main.sw @@ -22,5 +22,18 @@ fn main() -> bool { res = I16::from(10u16) / I16::from(5u16); assert(res == I16::from(2u16)); + //flip test + assert(one.flip() == I16::neg_from(1)); + //ge test + assert(one >= one); + assert(I16::from(2) >= one); + assert(one >= one.flip()); + assert(one.flip() >= I16::from(2).flip()); + //le test + assert(one <= one); + assert(one <= I16::from(2)); + assert(one.flip() <= one); + assert(I16::from(2).flip() <= one.flip()); + true } diff --git a/tests/src/test_projects/signed_i16/tests/mod.rs b/tests/src/test_projects/signed_i16/tests/mod.rs index 774715db..7c99a118 100644 --- a/tests/src/test_projects/signed_i16/tests/mod.rs +++ b/tests/src/test_projects/signed_i16/tests/mod.rs @@ -16,6 +16,8 @@ mod success { let instance = Testi16::new(wallet, path_to_bin); - let _result = instance.main().call().await; + let params = TxParameters::new(Some(1), Some(10_000_000), None); + let result = instance.main().tx_params(params).call().await; + assert_eq!(result.is_err(), false); } } diff --git a/tests/src/test_projects/signed_i256/src/main.sw b/tests/src/test_projects/signed_i256/src/main.sw index 27e3ca49..c14a4a4d 100644 --- a/tests/src/test_projects/signed_i256/src/main.sw +++ b/tests/src/test_projects/signed_i256/src/main.sw @@ -75,5 +75,24 @@ fn main() -> bool { res = I256::from(u128_10) / I256::from(u128_5); assert(res == I256::from(u128_2)); + //from_u64 test + assert(one == I256::from_u64(1)); + //as_u64 test + assert(1 == one.as_u64()); + //flip test + assert(one.flip() == I256::neg_from(u128_one)); + //ge test + assert(one >= one); + assert(I256::from_u64(2) >= one); + assert(one >= one.flip()); + assert(one.flip() >= I256::from_u64(2).flip()); + //le test + assert(one <= one); + assert(one <= I256::from_u64(2)); + assert(one.flip() <= one); + assert(I256::from_u64(2).flip() <= one.flip()); + //zero test + assert(I256::zero() == I256::from_u64(0)); + true } diff --git a/tests/src/test_projects/signed_i256/tests/mod.rs b/tests/src/test_projects/signed_i256/tests/mod.rs index 6456338e..624c91d6 100644 --- a/tests/src/test_projects/signed_i256/tests/mod.rs +++ b/tests/src/test_projects/signed_i256/tests/mod.rs @@ -16,6 +16,8 @@ mod success { let instance = Testi256::new(wallet, path_to_bin); - let _result = instance.main().call().await; + let params = TxParameters::new(Some(1), Some(10_000_000), None); + let result = instance.main().tx_params(params).call().await; + assert_eq!(result.is_err(), false); } } diff --git a/tests/src/test_projects/signed_i32/src/main.sw b/tests/src/test_projects/signed_i32/src/main.sw index 55197ded..eeaa1724 100644 --- a/tests/src/test_projects/signed_i32/src/main.sw +++ b/tests/src/test_projects/signed_i32/src/main.sw @@ -22,5 +22,18 @@ fn main() -> bool { res = I32::from_uint(10u32) / I32::from_uint(5u32); assert(res == I32::from_uint(2u32)); + //flip test + assert(one.flip() == I32::neg_from(1)); + //ge test + assert(one >= one); + assert(I32::from(2) >= one); + assert(one >= one.flip()); + assert(one.flip() >= I32::from(2).flip()); + //le test + assert(one <= one); + assert(one <= I32::from(2)); + assert(one.flip() <= one); + assert(I32::from(2).flip() <= one.flip()); + true } diff --git a/tests/src/test_projects/signed_i32/tests/mod.rs b/tests/src/test_projects/signed_i32/tests/mod.rs index 99e37958..41876b1d 100644 --- a/tests/src/test_projects/signed_i32/tests/mod.rs +++ b/tests/src/test_projects/signed_i32/tests/mod.rs @@ -16,6 +16,8 @@ mod success { let instance = Testi32::new(wallet, path_to_bin); - let _result = instance.main().call().await; + let params = TxParameters::new(Some(1), Some(10_000_000), None); + let result = instance.main().tx_params(params).call().await; + assert_eq!(result.is_err(), false); } } diff --git a/tests/src/test_projects/signed_i64/src/main.sw b/tests/src/test_projects/signed_i64/src/main.sw index d4908517..b5695066 100644 --- a/tests/src/test_projects/signed_i64/src/main.sw +++ b/tests/src/test_projects/signed_i64/src/main.sw @@ -22,5 +22,18 @@ fn main() -> bool { res = I64::from(10u64) / I64::from(5u64); assert(res == I64::from(2u64)); + //flip test + assert(one.flip() == I64::neg_from(1)); + //ge test + assert(one >= one); + assert(I64::from(2) >= one); + assert(one >= one.flip()); + assert(one.flip() >= I64::from(2).flip()); + //le test + assert(one <= one); + assert(one <= I64::from(2)); + assert(one.flip() <= one); + assert(I64::from(2).flip() <= one.flip()); + true } diff --git a/tests/src/test_projects/signed_i64/tests/mod.rs b/tests/src/test_projects/signed_i64/tests/mod.rs index ca8a00e0..7c06b29b 100644 --- a/tests/src/test_projects/signed_i64/tests/mod.rs +++ b/tests/src/test_projects/signed_i64/tests/mod.rs @@ -16,6 +16,8 @@ mod success { let instance = Testi64::new(wallet, path_to_bin); - let _result = instance.main().call().await; + let params = TxParameters::new(Some(1), Some(10_000_000), None); + let result = instance.main().tx_params(params).call().await; + assert_eq!(result.is_err(), false); } } diff --git a/tests/src/test_projects/signed_i8/src/main.sw b/tests/src/test_projects/signed_i8/src/main.sw index 38074bcc..81cf5b94 100644 --- a/tests/src/test_projects/signed_i8/src/main.sw +++ b/tests/src/test_projects/signed_i8/src/main.sw @@ -22,5 +22,18 @@ fn main() -> bool { res = I8::from(10u8) / I8::from(5u8); assert(res == I8::from(2u8)); + //flip test + assert(one.flip() == I8::neg_from(1)); + //ge test + assert(one >= one); + assert(I8::from(2) >= one); + assert(one >= one.flip()); + assert(one.flip() >= I8::from(2).flip()); + //le test + assert(one <= one); + assert(one <= I8::from(2)); + assert(one.flip() <= one); + assert(I8::from(2).flip() <= one.flip()); + true } diff --git a/tests/src/test_projects/signed_i8/tests/mod.rs b/tests/src/test_projects/signed_i8/tests/mod.rs index 1417d86b..1feace96 100644 --- a/tests/src/test_projects/signed_i8/tests/mod.rs +++ b/tests/src/test_projects/signed_i8/tests/mod.rs @@ -13,6 +13,8 @@ mod success { let instance = Testi8::new(wallet, path_to_bin); - let _result = instance.main().call().await; + let params = TxParameters::new(Some(1), Some(10_000_000), None); + let result = instance.main().tx_params(params).call().await; + assert_eq!(result.is_err(), false); } }