From c43bc504ff664f4dc9d6e15cd9b133ae7a2a7d07 Mon Sep 17 00:00:00 2001 From: alexey Date: Wed, 4 Jan 2023 00:32:58 +0000 Subject: [PATCH 1/5] :sparkles: added some usefull functions and tests to I128 --- sway_libs/src/signed_integers/i128.sw | 36 +++++++++++++++++++ .../test_projects/signed_i128/tests/mod.rs | 6 +++- .../src/test_projects/signed_i16/tests/mod.rs | 4 ++- .../test_projects/signed_i256/tests/mod.rs | 4 ++- .../src/test_projects/signed_i32/tests/mod.rs | 4 ++- .../src/test_projects/signed_i64/tests/mod.rs | 4 ++- .../src/test_projects/signed_i8/tests/mod.rs | 4 ++- 7 files changed, 56 insertions(+), 6 deletions(-) diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index 78ee1b87..6d797c76 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,11 @@ impl core::ops::Subtract for I128 { res } } + +impl I128 { + pub fn flip(self) -> Self { + self * Self { + underlying: Self::indent() - self.underlying, + } + } +} \ No newline at end of file diff --git a/tests/src/test_projects/signed_i128/tests/mod.rs b/tests/src/test_projects/signed_i128/tests/mod.rs index 3184be9e..226f3f53 100644 --- a/tests/src/test_projects/signed_i128/tests/mod.rs +++ b/tests/src/test_projects/signed_i128/tests/mod.rs @@ -16,6 +16,10 @@ mod success { let instance = Testi128::new(wallet, path_to_bin); - let _result = instance.main().call().await; + let params = TxParameters::new(Some(1), Some(10000000), None); + let _result = instance.main().tx_params(params).call().await; + let logs = _result.as_ref().unwrap().get_logs().unwrap(); + println!("{:#?}", logs); + assert_eq!(_result.is_err(), false); } } diff --git a/tests/src/test_projects/signed_i16/tests/mod.rs b/tests/src/test_projects/signed_i16/tests/mod.rs index 774715db..39f31bf4 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(10000000), 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/tests/mod.rs b/tests/src/test_projects/signed_i256/tests/mod.rs index 6456338e..b9e1db51 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(10000000), 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/tests/mod.rs b/tests/src/test_projects/signed_i32/tests/mod.rs index 99e37958..e4fc3659 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(10000000), 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/tests/mod.rs b/tests/src/test_projects/signed_i64/tests/mod.rs index ca8a00e0..8ab6af8f 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(10000000), 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/tests/mod.rs b/tests/src/test_projects/signed_i8/tests/mod.rs index 1417d86b..f1982f49 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(10000000), None); + let _result = instance.main().tx_params(params).call().await; + assert_eq!(_result.is_err(), false); } } From 32a46a99aa8bb44e9c3aa010f53b42d47a95b412 Mon Sep 17 00:00:00 2001 From: alexey Date: Wed, 4 Jan 2023 00:53:25 +0000 Subject: [PATCH 2/5] :recycle: remove logs --- tests/src/test_projects/signed_i128/tests/mod.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/src/test_projects/signed_i128/tests/mod.rs b/tests/src/test_projects/signed_i128/tests/mod.rs index 226f3f53..83cdbd8e 100644 --- a/tests/src/test_projects/signed_i128/tests/mod.rs +++ b/tests/src/test_projects/signed_i128/tests/mod.rs @@ -18,8 +18,6 @@ mod success { let params = TxParameters::new(Some(1), Some(10000000), None); let _result = instance.main().tx_params(params).call().await; - let logs = _result.as_ref().unwrap().get_logs().unwrap(); - println!("{:#?}", logs); assert_eq!(_result.is_err(), false); } } From 070d38f32c11cd43008df61d378593cc7ec3ae23 Mon Sep 17 00:00:00 2001 From: alexey Date: Wed, 4 Jan 2023 20:56:29 +0000 Subject: [PATCH 3/5] :sparkles: added implementation of new methods in anothers ints and added tests --- .../src/test_projects/signed_i128/src/main.sw | 19 +++++++++++++++++++ .../test_projects/signed_i128/tests/mod.rs | 6 +++--- 2 files changed, 22 insertions(+), 3 deletions(-) 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 83cdbd8e..1d259cf1 100644 --- a/tests/src/test_projects/signed_i128/tests/mod.rs +++ b/tests/src/test_projects/signed_i128/tests/mod.rs @@ -16,8 +16,8 @@ mod success { let instance = Testi128::new(wallet, path_to_bin); - let params = TxParameters::new(Some(1), Some(10000000), None); - let _result = instance.main().tx_params(params).call().await; - assert_eq!(_result.is_err(), false); + 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); } } From a9d4fa6cb22ab95a3e1d50d71278c02b6322938a Mon Sep 17 00:00:00 2001 From: alexey Date: Wed, 4 Jan 2023 21:06:37 +0000 Subject: [PATCH 4/5] :zap: added some new files --- .../src/test_projects/signed_i16/src/main.sw | 13 +++++++++++++ .../src/test_projects/signed_i16/tests/mod.rs | 6 +++--- .../src/test_projects/signed_i256/src/main.sw | 19 +++++++++++++++++++ .../test_projects/signed_i256/tests/mod.rs | 6 +++--- .../src/test_projects/signed_i32/src/main.sw | 13 +++++++++++++ .../src/test_projects/signed_i32/tests/mod.rs | 6 +++--- .../src/test_projects/signed_i64/src/main.sw | 13 +++++++++++++ .../src/test_projects/signed_i64/tests/mod.rs | 6 +++--- tests/src/test_projects/signed_i8/src/main.sw | 13 +++++++++++++ .../src/test_projects/signed_i8/tests/mod.rs | 6 +++--- 10 files changed, 86 insertions(+), 15 deletions(-) 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 39f31bf4..7c99a118 100644 --- a/tests/src/test_projects/signed_i16/tests/mod.rs +++ b/tests/src/test_projects/signed_i16/tests/mod.rs @@ -16,8 +16,8 @@ mod success { let instance = Testi16::new(wallet, path_to_bin); - let params = TxParameters::new(Some(1), Some(10000000), None); - let _result = instance.main().tx_params(params).call().await; - assert_eq!(_result.is_err(), false); + 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 b9e1db51..624c91d6 100644 --- a/tests/src/test_projects/signed_i256/tests/mod.rs +++ b/tests/src/test_projects/signed_i256/tests/mod.rs @@ -16,8 +16,8 @@ mod success { let instance = Testi256::new(wallet, path_to_bin); - let params = TxParameters::new(Some(1), Some(10000000), None); - let _result = instance.main().tx_params(params).call().await; - assert_eq!(_result.is_err(), false); + 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 e4fc3659..41876b1d 100644 --- a/tests/src/test_projects/signed_i32/tests/mod.rs +++ b/tests/src/test_projects/signed_i32/tests/mod.rs @@ -16,8 +16,8 @@ mod success { let instance = Testi32::new(wallet, path_to_bin); - let params = TxParameters::new(Some(1), Some(10000000), None); - let _result = instance.main().tx_params(params).call().await; - assert_eq!(_result.is_err(), false); + 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 8ab6af8f..7c06b29b 100644 --- a/tests/src/test_projects/signed_i64/tests/mod.rs +++ b/tests/src/test_projects/signed_i64/tests/mod.rs @@ -16,8 +16,8 @@ mod success { let instance = Testi64::new(wallet, path_to_bin); - let params = TxParameters::new(Some(1), Some(10000000), None); - let _result = instance.main().tx_params(params).call().await; - assert_eq!(_result.is_err(), false); + 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 f1982f49..1feace96 100644 --- a/tests/src/test_projects/signed_i8/tests/mod.rs +++ b/tests/src/test_projects/signed_i8/tests/mod.rs @@ -13,8 +13,8 @@ mod success { let instance = Testi8::new(wallet, path_to_bin); - let params = TxParameters::new(Some(1), Some(10000000), None); - let _result = instance.main().tx_params(params).call().await; - assert_eq!(_result.is_err(), false); + 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); } } From 83a590c980ac535b43b64b33902f57b85791e932 Mon Sep 17 00:00:00 2001 From: alexey Date: Wed, 4 Jan 2023 21:08:16 +0000 Subject: [PATCH 5/5] :zap: added some new files --- sway_libs/src/signed_integers/i128.sw | 6 ++--- sway_libs/src/signed_integers/i16.sw | 14 +++++++++++ sway_libs/src/signed_integers/i256.sw | 34 +++++++++++++++++++++++++++ sway_libs/src/signed_integers/i32.sw | 14 +++++++++++ sway_libs/src/signed_integers/i64.sw | 14 +++++++++++ sway_libs/src/signed_integers/i8.sw | 14 +++++++++++ 6 files changed, 92 insertions(+), 4 deletions(-) diff --git a/sway_libs/src/signed_integers/i128.sw b/sway_libs/src/signed_integers/i128.sw index 6d797c76..4bbef72e 100644 --- a/sway_libs/src/signed_integers/i128.sw +++ b/sway_libs/src/signed_integers/i128.sw @@ -199,9 +199,7 @@ impl core::ops::Subtract for I128 { } impl I128 { - pub fn flip(self) -> Self { - self * Self { - underlying: Self::indent() - self.underlying, - } + 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