diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d511549..b350bcbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,9 +22,25 @@ Description of the upcoming release here. - [#258](https://github.com/FuelLabs/sway-libs/pull/258) Fixes incorrect instructions on how to run tests in README and docs hub. - [#262](https://github.com/FuelLabs/sway-libs/pull/262) Fixes incorrect ordering comparison for IFP64, IFP128 and IFP256. +- [#263](https://github.com/FuelLabs/sway-libs/pull/263) Fixes `I256`'s returned bits. +- [#263](https://github.com/FuelLabs/sway-libs/pull/263) Fixes `I128` and `I256`'s zero or "indent" value. - [#268](https://github.com/FuelLabs/sway-libs/pull/268) Fixes subtraction involving negative numbers for `I8`, `I16`, `I32`, `I64`, `I128`, and `I256`. + #### Breaking -- Some breaking change here 1 -- Some breaking change here 2 +- [#263](https://github.com/FuelLabs/sway-libs/pull/263) Removes the `TwosComplement` trait in favor of `WrappingNeg`. + +The following demonstrates the breaking change. While this example code uses the `I8` type, the same logic may be applied to the `I16`, `I32`, `I64`, `I128`, and `I256` types. + +Before: +```sway +let my_i8 = i8::zero(); +let twos_complement = my_i8.twos_complement(); +``` + +After: +```sway +let my_i8 = i8::zero(); +let wrapping_neg = my_i8.wrapping_neg(); +``` diff --git a/libs/src/signed_integers/common.sw b/libs/src/signed_integers/common.sw index 8fade82b..acd8fe7d 100644 --- a/libs/src/signed_integers/common.sw +++ b/libs/src/signed_integers/common.sw @@ -1,11 +1,15 @@ library; -/// Trait for the Two's Complement of a value. -pub trait TwosComplement { - /// Returns the two's complement of a value. +/// Wrapping (modular) negation. Computes -self, wrapping around at the boundary of the type. +pub trait WrappingNeg { + /// Negates a signed number. + /// + /// # Additional Information + /// + /// * The only case where such wrapping can occur is when one negates self::min(). In such a case, this function returns self::min() itself. /// /// # Returns /// /// * [Self] - The value as two's complement. - fn twos_complement(self) -> Self; + fn wrapping_neg(self) -> Self; } diff --git a/libs/src/signed_integers/i128.sw b/libs/src/signed_integers/i128.sw index 859eab72..b7b5ba08 100644 --- a/libs/src/signed_integers/i128.sw +++ b/libs/src/signed_integers/i128.sw @@ -1,7 +1,7 @@ library; use std::u128::U128; -use ::signed_integers::common::TwosComplement; +use ::signed_integers::common::WrappingNeg; use ::signed_integers::errors::Error; /// The 128-bit signed integer type. @@ -31,11 +31,11 @@ impl I128 { /// /// fn foo() { /// let zero = I128::indent(); - /// assert(zero == U128::from((1, 0))); + /// assert(zero == (U128::max() / (U128::from(0, 2)) - U128::from(0,1)); /// } /// ``` pub fn indent() -> U128 { - U128::from((1, 0)) + U128::from((9223372036854775808, 0)) } } @@ -189,7 +189,7 @@ impl I128 { /// /// # Additional Information /// - /// The zero value of I128 is U128::from((1, 0)). + /// The zero value of I128 is U128::from((9223372036854775808, 0)). /// /// # Returns /// @@ -269,7 +269,7 @@ impl I128 { /// /// fn foo() { /// let i128 = I128::zero(); - /// assert(i128.underlying() == U128::from((1, 0))); + /// assert(i128.underlying() == U128::from((9223372036854775808, 0))); /// } /// ``` pub fn underlying(self) -> U128 { @@ -342,10 +342,8 @@ impl core::ops::Multiply for I128 { /// Multiply a I128 with a I128. Panics of overflow. fn multiply(self, other: Self) -> Self { let mut res = Self::new(); - if (self.underlying > Self::indent() - || self.underlying == Self::indent()) - && (other.underlying > Self::indent() - || other.underlying == Self::indent()) + if self.underlying >= Self::indent() + && other.underlying >= Self::indent() { res = Self::from_uint( (self.underlying - Self::indent()) * (other.underlying - Self::indent()) + Self::indent(), @@ -356,16 +354,14 @@ impl core::ops::Multiply for I128 { res = Self::from_uint( (Self::indent() - self.underlying) * (Self::indent() - other.underlying) + Self::indent(), ); - } else if (self.underlying > Self::indent() - || self.underlying == Self::indent()) + } else if self.underlying >= Self::indent() && other.underlying < Self::indent() { res = Self::from_uint( Self::indent() - (self.underlying - Self::indent()) * (Self::indent() - other.underlying), ); } else if self.underlying < Self::indent() - && (other.underlying > Self::indent() - || other.underlying == Self::indent()) + && other.underlying >= Self::indent() { res = Self::from_uint( Self::indent() - (other.underlying - Self::indent()) * (Self::indent() - self.underlying), @@ -400,15 +396,11 @@ impl core::ops::Subtract for I128 { } } -impl TwosComplement for I128 { - fn twos_complement(self) -> Self { - if self.underlying == Self::indent() - || self.underlying > Self::indent() - { - return self; +impl WrappingNeg for I128 { + fn wrapping_neg(self) -> Self { + if self == self::min() { + return self::min() } - let u_one = U128::from((0, 1)); - let res = I128::from_uint(!self.underlying + u_one); - res + self * Self::neg_from(U128::from((0, 1))) } } diff --git a/libs/src/signed_integers/i16.sw b/libs/src/signed_integers/i16.sw index 5a087a2f..9589900e 100644 --- a/libs/src/signed_integers/i16.sw +++ b/libs/src/signed_integers/i16.sw @@ -1,7 +1,7 @@ library; use ::signed_integers::errors::Error; -use ::signed_integers::common::TwosComplement; +use ::signed_integers::common::WrappingNeg; /// The 16-bit signed integer type. /// @@ -383,12 +383,11 @@ impl core::ops::Subtract for I16 { } } -impl TwosComplement for I16 { - fn twos_complement(self) -> Self { - if self.underlying >= Self::indent() { - return self; +impl WrappingNeg for I16 { + fn wrapping_neg(self) -> Self { + if self == self::min() { + return self::min() } - let res = Self::from_uint(!self.underlying + 1u16); - res + self * Self::neg_from(1u16) } } diff --git a/libs/src/signed_integers/i256.sw b/libs/src/signed_integers/i256.sw index 7e95b023..94c8da9d 100644 --- a/libs/src/signed_integers/i256.sw +++ b/libs/src/signed_integers/i256.sw @@ -1,6 +1,6 @@ library; -use ::signed_integers::common::TwosComplement; +use ::signed_integers::common::WrappingNeg; use ::signed_integers::errors::Error; /// The 256-bit signed integer type. @@ -19,7 +19,7 @@ impl I256 { /// /// # Additional Information /// - /// The zero value for I256 is 0x0000000000000000000000000000001000000000000000000000000000000000u256. + /// The zero value for I256 is 0x8000000000000000000000000000000000000000000000000000000000000000u256. /// /// # Returns /// @@ -32,14 +32,11 @@ impl I256 { /// /// fn foo() { /// let zero = I256::indent(); - /// assert(zero == 0x0000000000000000000000000000001000000000000000000000000000000000u256); + /// assert(zero == 0x8000000000000000000000000000000000000000000000000000000000000000u256); /// } /// ``` pub fn indent() -> u256 { - let parts = (0, 1, 0, 0); - asm(r1: parts) { - r1: u256 - } + 0x8000000000000000000000000000000000000000000000000000000000000000u256 } } @@ -81,11 +78,11 @@ impl I256 { /// /// fn foo() { /// let bits = I256::bits(); - /// assert(bits == 128); + /// assert(bits == 256); /// } /// ``` pub fn bits() -> u64 { - 128 + 256 } /// Helper function to get a signed number from with an underlying. @@ -173,9 +170,9 @@ impl I256 { /// use sway_libs::signed_integers::i256::I256; /// /// fn foo() { - /// let underlying = 0x0000000000000000000000000000001000000000000000000000000000000000u256; + /// let underlying = 0x0000000000000000000000000000000000000000000000000000000000000000u256; /// let i256 = I256::neg_from(underlying); - /// assert(i256.underlying() == 0x0000000000000000000000000000000000000000000000000000000000000000u256); + /// assert(i256.underlying() == 0x8000000000000000000000000000000000000000000000000000000000000000u256); /// } /// ``` pub fn neg_from(value: u256) -> Self { @@ -188,7 +185,7 @@ impl I256 { /// /// # Additional Information /// - /// The zero value of I256 is 0x0000000000000000000000000000001000000000000000000000000000000000u256. + /// The zero value of I256 is 0x8000000000000000000000000000000000000000000000000000000000000000u256. /// /// # Returns /// @@ -201,7 +198,7 @@ impl I256 { /// /// fn foo() { /// let i256 = I256::new(); - /// assert(i256.underlying() == 0x0000000000000000000000000000001000000000000000000000000000000000u256); + /// assert(i256.underlying() == 0x8000000000000000000000000000000000000000000000000000000000000000u256); /// } /// ``` pub fn new() -> Self { @@ -223,7 +220,7 @@ impl I256 { /// /// fn foo() { /// let i256 = I256::zero(); - /// assert(i256.underlying() == 0x0000000000000000000000000000001000000000000000000000000000000000u256); + /// assert(i256.underlying() == 0x8000000000000000000000000000000000000000000000000000000000000000u256); /// } /// ``` pub fn zero() -> Self { @@ -265,7 +262,7 @@ impl I256 { /// /// fn foo() { /// let i256 = I256::zero(); - /// assert(i256.underlying() == 0x0000000000000000000000000000001000000000000000000000000000000000u256); + /// assert(i256.underlying() == 0x8000000000000000000000000000000000000000000000000000000000000000u256); /// } /// ``` pub fn underlying(self) -> u256 { @@ -277,18 +274,17 @@ impl core::ops::Add for I256 { /// Add a I256 to a I256. Panics on overflow. fn add(self, other: Self) -> Self { // subtract 1 << 63 to avoid double move + let indent = Self::indent(); let mut res = Self::new(); - if (self.underlying > Self::indent() || self.underlying == Self::indent()) { - res = Self::from_uint(self.underlying - Self::indent() + other.underlying) // subtract 1 << 31 to avoid double move - } else if self.underlying < Self::indent() - && other.underlying < Self::indent() + if (self.underlying > indent || self.underlying == indent) { + res = Self::from_uint(self.underlying - indent + other.underlying) // subtract 1 << 31 to avoid double move + } else if self.underlying < indent && other.underlying < indent { + res = Self::from_uint(self.underlying + other.underlying - indent); + } else if self.underlying < indent + && (other.underlying > indent + || other.underlying == indent) { - res = Self::from_uint(self.underlying + other.underlying - Self::indent()); - } else if self.underlying < Self::indent() - && (other.underlying > Self::indent() - || other.underlying == Self::indent()) - { - res = Self::from_uint(other.underlying - Self::indent() + self.underlying); + res = Self::from_uint(other.underlying - indent + self.underlying); } res } @@ -299,35 +295,21 @@ impl core::ops::Divide for I256 { fn divide(self, divisor: Self) -> Self { require(divisor != Self::new(), Error::ZeroDivisor); let mut res = Self::new(); - let self_ge_indent = self.underlying > Self::indent() || self.underlying == Self::indent(); - let divisor_gt_indent = divisor.underlying > Self::indent(); + let indent = Self::indent(); + + let self_ge_indent = self.underlying > indent || self.underlying == indent; + let divisor_gt_indent = divisor.underlying > indent; if self_ge_indent && divisor_gt_indent { - res = Self::from_uint( - (self.underlying - Self::indent()) / (divisor - .underlying - Self::indent()) + Self::indent(), - ); - } else if self.underlying < Self::indent() - && divisor.underlying < Self::indent() + res = Self::from_uint((self.underlying - indent) / (divisor.underlying - indent) + indent); + } else if self.underlying < indent && divisor.underlying < indent { + res = Self::from_uint((indent - self.underlying) / (indent - divisor.underlying) + indent); + } else if (self.underlying > indent + || self.underlying == indent) + && divisor.underlying < indent { - res = Self::from_uint( - (Self::indent() - self.underlying) / (Self::indent() - divisor - .underlying) + Self::indent(), - ); - } else if (self.underlying > Self::indent() - || self.underlying == Self::indent()) - && divisor.underlying < Self::indent() - { - res = Self::from_uint( - Self::indent() - (self.underlying - Self::indent()) / (Self::indent() - divisor - .underlying), - ); - } else if self.underlying < Self::indent() - && divisor.underlying > Self::indent() - { - res = Self::from_uint( - Self::indent() - (Self::indent() - self.underlying) / (divisor - .underlying - Self::indent()), - ); + res = Self::from_uint(indent - (self.underlying - indent) / (indent - divisor.underlying)); + } else if self.underlying < indent && divisor.underlying > indent { + res = Self::from_uint(indent - (indent - self.underlying) / (divisor.underlying - indent)); } res } @@ -337,10 +319,8 @@ impl core::ops::Multiply for I256 { /// Multiply a I256 with a I256. Panics of overflow. fn multiply(self, other: Self) -> Self { let mut res = Self::new(); - if (self.underlying > Self::indent() - || self.underlying == Self::indent()) - && (other.underlying > Self::indent() - || other.underlying == Self::indent()) + if self.underlying >= Self::indent() + && other.underlying >= Self::indent() { res = Self::from_uint( (self.underlying - Self::indent()) * (other.underlying - Self::indent()) + Self::indent(), @@ -351,16 +331,14 @@ impl core::ops::Multiply for I256 { res = Self::from_uint( (Self::indent() - self.underlying) * (Self::indent() - other.underlying) + Self::indent(), ); - } else if (self.underlying > Self::indent() - || self.underlying == Self::indent()) + } else if self.underlying >= Self::indent() && other.underlying < Self::indent() { res = Self::from_uint( Self::indent() - (self.underlying - Self::indent()) * (Self::indent() - other.underlying), ); } else if self.underlying < Self::indent() - && (other.underlying > Self::indent() - || other.underlying == Self::indent()) + && other.underlying >= Self::indent() { res = Self::from_uint( Self::indent() - (other.underlying - Self::indent()) * (Self::indent() - self.underlying), @@ -376,7 +354,7 @@ impl core::ops::Subtract for I256 { let mut res = Self::new(); if self.underlying >= Self::indent() && other.underlying >= Self::indent() { // Both Positive if self.underlying > other.underlying { - res = Self::from_uint(self.underlying - other.underlying + Self::indent()); + res = Self::from_uint(self.underlying - other.underlying + indent); } else { res = Self::from_uint(self.underlying - (other.underlying - Self::indent())); } @@ -395,15 +373,11 @@ impl core::ops::Subtract for I256 { } } -impl TwosComplement for I256 { - fn twos_complement(self) -> Self { - if self.underlying == Self::indent() - || self.underlying > Self::indent() - { - return self; +impl WrappingNeg for I256 { + fn wrapping_neg(self) -> Self { + if self == self::min() { + return self::min() } - let u_one = 0x0000000000000000000000000000000000000000000000000000000000000001u256; - let res = I256::from_uint(!self.underlying + u_one); - res + self * Self::neg_from(0x0000000000000000000000000000000000000000000000000000000000000001u256) } } diff --git a/libs/src/signed_integers/i32.sw b/libs/src/signed_integers/i32.sw index 2e21fe3c..c2e721b8 100644 --- a/libs/src/signed_integers/i32.sw +++ b/libs/src/signed_integers/i32.sw @@ -1,6 +1,6 @@ library; -use ::signed_integers::common::TwosComplement; +use ::signed_integers::common::WrappingNeg; use ::signed_integers::errors::Error; /// The 32-bit signed integer type. @@ -382,12 +382,11 @@ impl core::ops::Divide for I32 { } } -impl TwosComplement for I32 { - fn twos_complement(self) -> Self { - if self.underlying >= Self::indent() { - return self; +impl WrappingNeg for I32 { + fn wrapping_neg(self) -> Self { + if self == self::min() { + return self::min() } - let res = Self::from_uint(!self.underlying + 1u32); - res + self * Self::neg_from(1u32) } } diff --git a/libs/src/signed_integers/i64.sw b/libs/src/signed_integers/i64.sw index 7f1f8191..04331b2e 100644 --- a/libs/src/signed_integers/i64.sw +++ b/libs/src/signed_integers/i64.sw @@ -1,6 +1,6 @@ library; -use ::signed_integers::common::TwosComplement; +use ::signed_integers::common::WrappingNeg; use ::signed_integers::errors::Error; /// The 64-bit signed integer type. @@ -383,12 +383,11 @@ impl core::ops::Divide for I64 { } } -impl TwosComplement for I64 { - fn twos_complement(self) -> Self { - if self.underlying >= Self::indent() { - return self; +impl WrappingNeg for I64 { + fn wrapping_neg(self) -> Self { + if self == self::min() { + return self::min() } - let res = Self::from_uint(!self.underlying + 1); - res + self * Self::neg_from(1) } } diff --git a/libs/src/signed_integers/i8.sw b/libs/src/signed_integers/i8.sw index 4cb0822c..9fbd535c 100644 --- a/libs/src/signed_integers/i8.sw +++ b/libs/src/signed_integers/i8.sw @@ -1,7 +1,7 @@ library; use ::signed_integers::errors::Error; -use ::signed_integers::common::TwosComplement; +use ::signed_integers::common::WrappingNeg; /// The 8-bit signed integer type. /// @@ -382,12 +382,11 @@ impl core::ops::Subtract for I8 { } } -impl TwosComplement for I8 { - fn twos_complement(self) -> Self { - if self.underlying >= Self::indent() { - return self; +impl WrappingNeg for I8 { + fn wrapping_neg(self) -> Self { + if self == self::min() { + return self::min() } - let res = Self::from_uint(!self.underlying + 1u8); - res + self * Self::neg_from(1u8) } } diff --git a/tests/Forc.lock b/tests/Forc.lock index b8345450..893e5942 100644 --- a/tests/Forc.lock +++ b/tests/Forc.lock @@ -32,6 +32,14 @@ dependencies = [ "sway_libs", ] +[[package]] +name = "i128_wrapping_neg_test" +source = "member" +dependencies = [ + "std", + "sway_libs", +] + [[package]] name = "i16_test" source = "member" @@ -41,7 +49,7 @@ dependencies = [ ] [[package]] -name = "i16_twos_complement_test" +name = "i16_wrapping_neg_test" source = "member" dependencies = [ "std", @@ -56,6 +64,14 @@ dependencies = [ "sway_libs", ] +[[package]] +name = "i256_wrapping_neg_test" +source = "member" +dependencies = [ + "std", + "sway_libs", +] + [[package]] name = "i32_test" source = "member" @@ -65,7 +81,7 @@ dependencies = [ ] [[package]] -name = "i32_twos_complement_test" +name = "i32_wrapping_neg_test" source = "member" dependencies = [ "std", @@ -81,7 +97,7 @@ dependencies = [ ] [[package]] -name = "i64_twos_complement_test" +name = "i64_wrapping_neg_test" source = "member" dependencies = [ "std", @@ -97,7 +113,7 @@ dependencies = [ ] [[package]] -name = "i8_twos_complement_test" +name = "i8_wrapping_neg_test" source = "member" dependencies = [ "std", diff --git a/tests/Forc.toml b/tests/Forc.toml index 45393470..a683239a 100644 --- a/tests/Forc.toml +++ b/tests/Forc.toml @@ -44,9 +44,11 @@ members = [ "./src/signed_integers/signed_i64", "./src/signed_integers/signed_i128", "./src/signed_integers/signed_i256", - "./src/signed_integers/signed_i8_twos_complement", - "./src/signed_integers/signed_i16_twos_complement", - "./src/signed_integers/signed_i32_twos_complement", - "./src/signed_integers/signed_i64_twos_complement", + "./src/signed_integers/signed_i8_wrapping_neg", + "./src/signed_integers/signed_i16_wrapping_neg", + "./src/signed_integers/signed_i32_wrapping_neg", + "./src/signed_integers/signed_i64_wrapping_neg", + "./src/signed_integers/signed_i128_wrapping_neg", + "./src/signed_integers/signed_i256_wrapping_neg", "./src/upgradability", ] diff --git a/tests/src/signed_integers/mod.rs b/tests/src/signed_integers/mod.rs index 019485d5..e969e9d5 100644 --- a/tests/src/signed_integers/mod.rs +++ b/tests/src/signed_integers/mod.rs @@ -1,11 +1,12 @@ mod signed_i128; +mod signed_i128_wrapping_neg; mod signed_i16; +mod signed_i16_wrapping_neg; mod signed_i256; +mod signed_i256_wrapping_neg; mod signed_i32; +mod signed_i32_wrapping_neg; mod signed_i64; +mod signed_i64_wrapping_neg; mod signed_i8; - -mod signed_i16_twos_complement; -mod signed_i32_twos_complement; -mod signed_i64_twos_complement; -mod signed_i8_twos_complement; +mod signed_i8_wrapping_neg; diff --git a/tests/src/signed_integers/signed_i128_twos_complement/Forc.toml b/tests/src/signed_integers/signed_i128_twos_complement/Forc.toml deleted file mode 100644 index 981e3107..00000000 --- a/tests/src/signed_integers/signed_i128_twos_complement/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "i128_twos_complement_test" - -[dependencies] -sway_libs = { path = "../../../../libs" } diff --git a/tests/src/signed_integers/signed_i128_twos_complement/src/main.sw b/tests/src/signed_integers/signed_i128_twos_complement/src/main.sw deleted file mode 100644 index b08bbac4..00000000 --- a/tests/src/signed_integers/signed_i128_twos_complement/src/main.sw +++ /dev/null @@ -1,31 +0,0 @@ -script; - -use sway_libs::signed_integers::i128::I128; -use std::u128::U128; - -fn main() -> bool { - let u_one = U128::from((0, 1)); - let one = I128::from(u_one); - let mut res = one + I128::from(U128::from((0, 1))); - assert(res.twos_complement() == I128::from(U128::from((0, 2)))); - - res = I128::from(U128::from((0, 10))); - assert(res.twos_complement() == I128::from(U128::from((0, 10)))); - - res = I128::neg_from(U128::from((0, 5))); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U128::max()); - - res = I128::neg_from(U128::from((0, 27))); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U128::max()); - - res = I128::neg_from(U128::from((0, 110))); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U128::max()); - - res = I128::neg_from(U128::from((0, 93))); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U128::max()); - - res = I128::neg_from(U128::from((0, 78))); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U128::max()); - - true -} diff --git a/tests/src/signed_integers/signed_i128_twos_complement/tests/mod.rs b/tests/src/signed_integers/signed_i128_twos_complement/tests/mod.rs deleted file mode 100644 index 1f4c3b4d..00000000 --- a/tests/src/signed_integers/signed_i128_twos_complement/tests/mod.rs +++ /dev/null @@ -1,22 +0,0 @@ -use fuels::prelude::{abigen, launch_provider_and_get_wallet}; - -abigen!(Script( - name = "Testi128TwosComplement", - abi = "src/signed_integers/signed_i128_twos_complement/out/release/i128_twos_complement_test-abi.json" -),); - -mod success { - - use super::*; - - #[tokio::test] - async fn runs_i128_twos_complement_test_script() { - let path_to_bin = - "src/signed_integers/signed_i128_twos_complement/out/release/i128_twos_complement_test.bin"; - let wallet = launch_provider_and_get_wallet().await; - - let instance = Testi128TwosComplement::new(wallet, path_to_bin); - - let _result = instance.main().call().await; - } -} diff --git a/tests/src/signed_integers/signed_i128_twos_complement/.gitignore b/tests/src/signed_integers/signed_i128_wrapping_neg/.gitignore similarity index 100% rename from tests/src/signed_integers/signed_i128_twos_complement/.gitignore rename to tests/src/signed_integers/signed_i128_wrapping_neg/.gitignore diff --git a/tests/src/signed_integers/signed_i8_twos_complement/Forc.toml b/tests/src/signed_integers/signed_i128_wrapping_neg/Forc.toml similarity index 82% rename from tests/src/signed_integers/signed_i8_twos_complement/Forc.toml rename to tests/src/signed_integers/signed_i128_wrapping_neg/Forc.toml index 461e6513..97be834a 100644 --- a/tests/src/signed_integers/signed_i8_twos_complement/Forc.toml +++ b/tests/src/signed_integers/signed_i128_wrapping_neg/Forc.toml @@ -2,7 +2,7 @@ authors = ["Fuel Labs "] entry = "main.sw" license = "Apache-2.0" -name = "i8_twos_complement_test" +name = "i128_wrapping_neg_test" [dependencies] sway_libs = { path = "../../../../libs" } diff --git a/tests/src/signed_integers/signed_i128_twos_complement/mod.rs b/tests/src/signed_integers/signed_i128_wrapping_neg/mod.rs similarity index 100% rename from tests/src/signed_integers/signed_i128_twos_complement/mod.rs rename to tests/src/signed_integers/signed_i128_wrapping_neg/mod.rs diff --git a/tests/src/signed_integers/signed_i128_wrapping_neg/src/main.sw b/tests/src/signed_integers/signed_i128_wrapping_neg/src/main.sw new file mode 100644 index 00000000..30b95d4b --- /dev/null +++ b/tests/src/signed_integers/signed_i128_wrapping_neg/src/main.sw @@ -0,0 +1,61 @@ +script; + +use sway_libs::signed_integers::i128::I128; +use std::u128::U128; + +fn main() -> bool { + let one = I128::from(U128::from(1u64)); + let neg_one = I128::neg_from(U128::from(1u64)); + + let two = I128::from(U128::from(2u64)); + let neg_two = I128::neg_from(U128::from(2u64)); + + let ten = I128::from(U128::from(10u64)); + let neg_ten = I128::neg_from(U128::from(10u64)); + + let twenty_seven = I128::from(U128::from(27u64)); + let neg_twenty_seven = I128::neg_from(U128::from(27u64)); + + let ninty_three = I128::from(U128::from(93u64)); + let neg_ninty_three = I128::neg_from(U128::from(93u64)); + + let zero = I128::from(U128::zero()); + let max = I128::max(); + let min = I128::min(); + let neg_min_plus_one = I128::min() + I128::from(U128::from((0, 1))); + + let res1 = one.wrapping_neg(); + let res2 = neg_one.wrapping_neg(); + assert(res1 == neg_one); + assert(res2 == one); + + let res3 = two.wrapping_neg(); + let res4 = neg_two.wrapping_neg(); + assert(res3 == neg_two); + assert(res4 == two); + + let res5 = ten.wrapping_neg(); + let res6 = neg_ten.wrapping_neg(); + assert(res5 == neg_ten); + assert(res6 == ten); + + let res7 = twenty_seven.wrapping_neg(); + let res8 = neg_twenty_seven.wrapping_neg(); + assert(res7 == neg_twenty_seven); + assert(res8 == twenty_seven); + + let res9 = ninty_three.wrapping_neg(); + let res10 = neg_ninty_three.wrapping_neg(); + assert(res9 == neg_ninty_three); + assert(res10 == ninty_three); + + let res11 = zero.wrapping_neg(); + let res12 = max.wrapping_neg(); + let res13 = min.wrapping_neg(); + + assert(res11 == zero); + assert(res12 == neg_min_plus_one); + assert(res13 == min); + + true +} diff --git a/tests/src/signed_integers/signed_i128_wrapping_neg/tests/mod.rs b/tests/src/signed_integers/signed_i128_wrapping_neg/tests/mod.rs new file mode 100644 index 00000000..00132f65 --- /dev/null +++ b/tests/src/signed_integers/signed_i128_wrapping_neg/tests/mod.rs @@ -0,0 +1,23 @@ +use fuels::prelude::{abigen, launch_provider_and_get_wallet}; + +abigen!(Script( + name = "Testi128WrappingNeg", + abi = + "src/signed_integers/signed_i128_wrapping_neg/out/release/i128_wrapping_neg_test-abi.json" +),); + +mod success { + + use super::*; + + #[tokio::test] + async fn runs_i128_wrapping_neg_test_script() { + let path_to_bin = + "src/signed_integers/signed_i128_wrapping_neg/out/release/i128_wrapping_neg_test.bin"; + let wallet = launch_provider_and_get_wallet().await.unwrap(); + + let instance = Testi128WrappingNeg::new(wallet, path_to_bin); + + let _result = instance.main().call().await; + } +} diff --git a/tests/src/signed_integers/signed_i16_twos_complement/src/main.sw b/tests/src/signed_integers/signed_i16_twos_complement/src/main.sw deleted file mode 100644 index 9029b999..00000000 --- a/tests/src/signed_integers/signed_i16_twos_complement/src/main.sw +++ /dev/null @@ -1,29 +0,0 @@ -script; - -use sway_libs::signed_integers::i16::I16; - -fn main() -> bool { - let one = I16::from(1u16); - let mut res = one + I16::from(1u16); - assert(res.twos_complement() == I16::from(2u16)); - - res = I16::from(10u16); - assert(res.twos_complement() == I16::from(10u16)); - - res = I16::neg_from(5); - assert(res.twos_complement().underlying() + res.underlying() == u16::max() + 1); - - res = I16::neg_from(27u16); - assert(res.twos_complement().underlying() + res.underlying() == u16::max() + 1); - - res = I16::neg_from(110u16); - assert(res.twos_complement().underlying() + res.underlying() == u16::max() + 1); - - res = I16::neg_from(93u16); - assert(res.twos_complement().underlying() + res.underlying() == u16::max() + 1); - - res = I16::neg_from(78u16); - assert(res.twos_complement().underlying() + res.underlying() == u16::max() + 1); - - true -} diff --git a/tests/src/signed_integers/signed_i16_twos_complement/tests/mod.rs b/tests/src/signed_integers/signed_i16_twos_complement/tests/mod.rs deleted file mode 100644 index deaee06b..00000000 --- a/tests/src/signed_integers/signed_i16_twos_complement/tests/mod.rs +++ /dev/null @@ -1,22 +0,0 @@ -use fuels::prelude::{abigen, launch_provider_and_get_wallet}; - -abigen!(Script( - name = "Testi16TwosComplement", - abi = "src/signed_integers/signed_i16_twos_complement/out/release/i16_twos_complement_test-abi.json" -),); - -mod success { - - use super::*; - - #[tokio::test] - async fn runs_i16_twos_complement_test_script() { - let path_to_bin = - "src/signed_integers/signed_i16_twos_complement/out/release/i16_twos_complement_test.bin"; - let wallet = launch_provider_and_get_wallet().await.unwrap(); - - let instance = Testi16TwosComplement::new(wallet, path_to_bin); - - let _result = instance.main().call().await; - } -} diff --git a/tests/src/signed_integers/signed_i16_twos_complement/.gitignore b/tests/src/signed_integers/signed_i16_wrapping_neg/.gitignore similarity index 100% rename from tests/src/signed_integers/signed_i16_twos_complement/.gitignore rename to tests/src/signed_integers/signed_i16_wrapping_neg/.gitignore diff --git a/tests/src/signed_integers/signed_i64_twos_complement/Forc.toml b/tests/src/signed_integers/signed_i16_wrapping_neg/Forc.toml similarity index 81% rename from tests/src/signed_integers/signed_i64_twos_complement/Forc.toml rename to tests/src/signed_integers/signed_i16_wrapping_neg/Forc.toml index b6211748..b06e023a 100644 --- a/tests/src/signed_integers/signed_i64_twos_complement/Forc.toml +++ b/tests/src/signed_integers/signed_i16_wrapping_neg/Forc.toml @@ -2,7 +2,7 @@ authors = ["Fuel Labs "] entry = "main.sw" license = "Apache-2.0" -name = "i64_twos_complement_test" +name = "i16_wrapping_neg_test" [dependencies] sway_libs = { path = "../../../../libs" } diff --git a/tests/src/signed_integers/signed_i16_twos_complement/mod.rs b/tests/src/signed_integers/signed_i16_wrapping_neg/mod.rs similarity index 100% rename from tests/src/signed_integers/signed_i16_twos_complement/mod.rs rename to tests/src/signed_integers/signed_i16_wrapping_neg/mod.rs diff --git a/tests/src/signed_integers/signed_i16_wrapping_neg/src/main.sw b/tests/src/signed_integers/signed_i16_wrapping_neg/src/main.sw new file mode 100644 index 00000000..0d1dc920 --- /dev/null +++ b/tests/src/signed_integers/signed_i16_wrapping_neg/src/main.sw @@ -0,0 +1,60 @@ +script; + +use sway_libs::signed_integers::i16::I16; + +fn main() -> bool { + let one = I16::from(1u16); + let neg_one = I16::from(0u16) - I16::from(1u16); + + let two = I16::from(2u16); + let neg_two = I16::from(0u16) - I16::from(2u16); + + let ten = I16::from(10u16); + let neg_ten = I16::from(0u16) - I16::from(10u16); + + let twenty_seven = I16::from(27u16); + let neg_twenty_seven = I16::from(0u16) - I16::from(27u16); + + let ninty_three = I16::from(93u16); + let neg_ninty_three = I16::from(0u16) - I16::from(93u16); + + let zero = I16::from(0u16); + let max = I16::max(); + let min = I16::min(); + let neg_min_plus_one = I16::min() + I16::from(1u16); + + let res1 = one.wrapping_neg(); + let res2 = neg_one.wrapping_neg(); + assert(res1 == neg_one); + assert(res2 == one); + + let res3 = two.wrapping_neg(); + let res4 = neg_two.wrapping_neg(); + assert(res3 == neg_two); + assert(res4 == two); + + let res5 = ten.wrapping_neg(); + let res6 = neg_ten.wrapping_neg(); + assert(res5 == neg_ten); + assert(res6 == ten); + + let res7 = twenty_seven.wrapping_neg(); + let res8 = neg_twenty_seven.wrapping_neg(); + assert(res7 == neg_twenty_seven); + assert(res8 == twenty_seven); + + let res9 = ninty_three.wrapping_neg(); + let res10 = neg_ninty_three.wrapping_neg(); + assert(res9 == neg_ninty_three); + assert(res10 == ninty_three); + + let res11 = zero.wrapping_neg(); + let res12 = max.wrapping_neg(); + let res13 = min.wrapping_neg(); + + assert(res11 == zero); + assert(res12 == neg_min_plus_one); + assert(res13 == min); + + true +} diff --git a/tests/src/signed_integers/signed_i16_wrapping_neg/tests/mod.rs b/tests/src/signed_integers/signed_i16_wrapping_neg/tests/mod.rs new file mode 100644 index 00000000..9a00a624 --- /dev/null +++ b/tests/src/signed_integers/signed_i16_wrapping_neg/tests/mod.rs @@ -0,0 +1,22 @@ +use fuels::prelude::{abigen, launch_provider_and_get_wallet}; + +abigen!(Script( + name = "Testi16WrappingNeg", + abi = "src/signed_integers/signed_i16_wrapping_neg/out/release/i16_wrapping_neg_test-abi.json" +),); + +mod success { + + use super::*; + + #[tokio::test] + async fn runs_signed_i16_wrapping_neg_test_script() { + let path_to_bin = + "src/signed_integers/signed_i16_wrapping_neg/out/release/i16_wrapping_neg_test.bin"; + let wallet = launch_provider_and_get_wallet().await.unwrap(); + + let instance = Testi16WrappingNeg::new(wallet, path_to_bin); + + let _result = instance.main().call().await; + } +} diff --git a/tests/src/signed_integers/signed_i256_twos_complement/Forc.toml b/tests/src/signed_integers/signed_i256_twos_complement/Forc.toml deleted file mode 100644 index 60815b83..00000000 --- a/tests/src/signed_integers/signed_i256_twos_complement/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "i256_twos_complement_test" - -[dependencies] -sway_libs = { path = "../../../../libs" } diff --git a/tests/src/signed_integers/signed_i256_twos_complement/src/main.sw b/tests/src/signed_integers/signed_i256_twos_complement/src/main.sw deleted file mode 100644 index d9199934..00000000 --- a/tests/src/signed_integers/signed_i256_twos_complement/src/main.sw +++ /dev/null @@ -1,61 +0,0 @@ -script; - -use sway_libs::signed_integers::i256::I256; - -fn main() -> bool { - let parts_one = (0, 0, 0, 1); - let parts_two = (0, 0, 0, 2); - let u_one = asm(r1: parts_one) { - r1: u256 - }; - let u_two = asm(r1: parts_two) { - r1: u256 - }; - let one = I256::from(u_one); - let mut res = one + I256::from(u_one); - assert(res.twos_complement() == I256::from(u_two)); - - let parts_10 = (0, 0, 0, 10); - let u_10 = asm(r1: parts_10) { - r1: u256 - }; - res = I256::from(u_10); - assert(res.twos_complement() == I256::from(u_10)); - - let parts_5 = (0, 0, 0, 5); - let u_5 = asm(r1: parts_5) { - r1: u256 - }; - res = I256::neg_from(u_5); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U256::max()); - - let parts_27 = (0, 0, 0, 27); - let u_27 = asm(r1: parts_27) { - r1: u256 - }; - res = I256::neg_from(u_27); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U256::max()); - - let parts_110 = (0, 0, 0, 110); - let u_110 = asm(r1: parts_110) { - r1: u256 - }; - res = I256::neg_from(u_110); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U256::max()); - - let parts_93 = (0, 0, 0, 93); - let u_93 = asm(r1: parts_93) { - r1: u256 - }; - res = I256::neg_from(parts_93); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U256::max()); - - let parts_78 = (0, 0, 0, 78); - let u_78 = asm(r1: parts_78) { - r1: u256 - }; - res = I256::neg_from(u_78); - assert(res.twos_complement().underlying() - u_one + res.underlying() == U256::max()); - - true -} diff --git a/tests/src/signed_integers/signed_i256_twos_complement/tests/mod.rs b/tests/src/signed_integers/signed_i256_twos_complement/tests/mod.rs deleted file mode 100644 index 86b5d93d..00000000 --- a/tests/src/signed_integers/signed_i256_twos_complement/tests/mod.rs +++ /dev/null @@ -1,22 +0,0 @@ -use fuels::prelude::{abigen, launch_provider_and_get_wallet}; - -abigen!(Script( - name = "Testi256TwosComplement", - abi = "src/signed_integers/signed_i256_twos_complement/out/release/i256_twos_complement_test-abi.json" -),); - -mod success { - - use super::*; - - #[tokio::test] - async fn runs_i256_twos_complement_test_script() { - let path_to_bin = - "src/signed_integers/signed_i256_twos_complement/out/release/i256_twos_complement_test.bin"; - let wallet = launch_provider_and_get_wallet().await; - - let instance = Testi256TwosComplement::new(wallet, path_to_bin); - - let _result = instance.main().call().await; - } -} diff --git a/tests/src/signed_integers/signed_i256_twos_complement/.gitignore b/tests/src/signed_integers/signed_i256_wrapping_neg/.gitignore similarity index 100% rename from tests/src/signed_integers/signed_i256_twos_complement/.gitignore rename to tests/src/signed_integers/signed_i256_wrapping_neg/.gitignore diff --git a/tests/src/signed_integers/signed_i32_twos_complement/Forc.toml b/tests/src/signed_integers/signed_i256_wrapping_neg/Forc.toml similarity index 81% rename from tests/src/signed_integers/signed_i32_twos_complement/Forc.toml rename to tests/src/signed_integers/signed_i256_wrapping_neg/Forc.toml index 0ce180aa..85795249 100644 --- a/tests/src/signed_integers/signed_i32_twos_complement/Forc.toml +++ b/tests/src/signed_integers/signed_i256_wrapping_neg/Forc.toml @@ -2,7 +2,7 @@ authors = ["Fuel Labs "] entry = "main.sw" license = "Apache-2.0" -name = "i32_twos_complement_test" +name = "i256_wrapping_neg_test" [dependencies] sway_libs = { path = "../../../../libs" } diff --git a/tests/src/signed_integers/signed_i256_twos_complement/mod.rs b/tests/src/signed_integers/signed_i256_wrapping_neg/mod.rs similarity index 100% rename from tests/src/signed_integers/signed_i256_twos_complement/mod.rs rename to tests/src/signed_integers/signed_i256_wrapping_neg/mod.rs diff --git a/tests/src/signed_integers/signed_i256_wrapping_neg/src/main.sw b/tests/src/signed_integers/signed_i256_wrapping_neg/src/main.sw new file mode 100644 index 00000000..0e7fcdfd --- /dev/null +++ b/tests/src/signed_integers/signed_i256_wrapping_neg/src/main.sw @@ -0,0 +1,81 @@ +script; + +use sway_libs::signed_integers::i256::I256; + +fn main() -> bool { + let parts_one = (0, 0, 0, 1); + let parts_two = (0, 0, 0, 2); + let parts_ten = (0, 0, 0, 10); + let parts_twenty_seven = (0, 0, 0, 27); + let parts_ninty_three = (0, 0, 0, 93); + let u_one = asm(r1: parts_one) { + r1: u256 + }; + let u_two = asm(r1: parts_two) { + r1: u256 + }; + let u_ten = asm(r1: parts_ten) { + r1: u256 + }; + let u_twenty_seven = asm(r1: parts_twenty_seven) { + r1: u256 + }; + let u_ninty_three = asm(r1: parts_ninty_three) { + r1: u256 + }; + + let one = I256::from(u_one); + let neg_one = I256::neg_from(u_one); + + let two = I256::from(u_two); + let neg_two = I256::neg_from(u_two); + + let ten = I256::from(u_ten); + let neg_ten = I256::neg_from(u_ten); + + let twenty_seven = I256::from(u_twenty_seven); + let neg_twenty_seven = I256::neg_from(u_twenty_seven); + + let ninty_three = I256::from(u_ninty_three); + let neg_ninty_three = I256::neg_from(u_ninty_three); + + let zero = I256::from(u256::zero()); + let max = I256::max(); + let min = I256::min(); + let neg_min_plus_one = I256::min() + I256::from(u_one); + + let res1 = one.wrapping_neg(); + let res2 = neg_one.wrapping_neg(); + assert(res1 == neg_one); + assert(res2 == one); + + let res3 = two.wrapping_neg(); + let res4 = neg_two.wrapping_neg(); + assert(res3 == neg_two); + assert(res4 == two); + + let res5 = ten.wrapping_neg(); + let res6 = neg_ten.wrapping_neg(); + assert(res5 == neg_ten); + assert(res6 == ten); + + let res7 = twenty_seven.wrapping_neg(); + let res8 = neg_twenty_seven.wrapping_neg(); + assert(res7 == neg_twenty_seven); + assert(res8 == twenty_seven); + + let res9 = ninty_three.wrapping_neg(); + let res10 = neg_ninty_three.wrapping_neg(); + assert(res9 == neg_ninty_three); + assert(res10 == ninty_three); + + let res11 = zero.wrapping_neg(); + let res12 = max.wrapping_neg(); + let res13 = min.wrapping_neg(); + + assert(res11 == zero); + assert(res12 == neg_min_plus_one); + assert(res13 == min); + + true +} diff --git a/tests/src/signed_integers/signed_i256_wrapping_neg/tests/mod.rs b/tests/src/signed_integers/signed_i256_wrapping_neg/tests/mod.rs new file mode 100644 index 00000000..6bdf83b7 --- /dev/null +++ b/tests/src/signed_integers/signed_i256_wrapping_neg/tests/mod.rs @@ -0,0 +1,23 @@ +use fuels::prelude::{abigen, launch_provider_and_get_wallet}; + +abigen!(Script( + name = "Testi256WrappingNeg", + abi = + "src/signed_integers/signed_i256_wrapping_neg/out/release/i256_wrapping_neg_test-abi.json" +),); + +mod success { + + use super::*; + + #[tokio::test] + async fn runs_i256_wrapping_neg_test_script() { + let path_to_bin = + "src/signed_integers/signed_i256_wrapping_neg/out/release/i256_wrapping_neg_test.bin"; + let wallet = launch_provider_and_get_wallet().await.unwrap(); + + let instance = Testi256WrappingNeg::new(wallet, path_to_bin); + + let _result = instance.main().call().await; + } +} diff --git a/tests/src/signed_integers/signed_i32_twos_complement/src/main.sw b/tests/src/signed_integers/signed_i32_twos_complement/src/main.sw deleted file mode 100644 index d32e4af6..00000000 --- a/tests/src/signed_integers/signed_i32_twos_complement/src/main.sw +++ /dev/null @@ -1,29 +0,0 @@ -script; - -use sway_libs::signed_integers::i32::I32; - -fn main() -> bool { - let one = I32::from(1u32); - let mut res = one + I32::from(1u32); - assert(res.twos_complement() == I32::from(2u32)); - - res = I32::from(10u32); - assert(res.twos_complement() == I32::from(10u32)); - - res = I32::neg_from(5); - assert(res.twos_complement().underlying() + res.underlying() == u32::max() + 1); - - res = I32::neg_from(27u32); - assert(res.twos_complement().underlying() + res.underlying() == u32::max() + 1); - - res = I32::neg_from(110u32); - assert(res.twos_complement().underlying() + res.underlying() == u32::max() + 1); - - res = I32::neg_from(93u32); - assert(res.twos_complement().underlying() + res.underlying() == u32::max() + 1); - - res = I32::neg_from(78u32); - assert(res.twos_complement().underlying() + res.underlying() == u32::max() + 1); - - true -} diff --git a/tests/src/signed_integers/signed_i32_twos_complement/tests/mod.rs b/tests/src/signed_integers/signed_i32_twos_complement/tests/mod.rs deleted file mode 100644 index 1974e72b..00000000 --- a/tests/src/signed_integers/signed_i32_twos_complement/tests/mod.rs +++ /dev/null @@ -1,22 +0,0 @@ -use fuels::prelude::{abigen, launch_provider_and_get_wallet}; - -abigen!(Script( - name = "Testi32TwosComplement", - abi = "src/signed_integers/signed_i32_twos_complement/out/release/i32_twos_complement_test-abi.json" -),); - -mod success { - - use super::*; - - #[tokio::test] - async fn runs_i32_twos_complement_test_script() { - let path_to_bin = - "src/signed_integers/signed_i32_twos_complement/out/release/i32_twos_complement_test.bin"; - let wallet = launch_provider_and_get_wallet().await.unwrap(); - - let instance = Testi32TwosComplement::new(wallet, path_to_bin); - - let _result = instance.main().call().await; - } -} diff --git a/tests/src/signed_integers/signed_i32_twos_complement/.gitignore b/tests/src/signed_integers/signed_i32_wrapping_neg/.gitignore similarity index 100% rename from tests/src/signed_integers/signed_i32_twos_complement/.gitignore rename to tests/src/signed_integers/signed_i32_wrapping_neg/.gitignore diff --git a/tests/src/signed_integers/signed_i16_twos_complement/Forc.toml b/tests/src/signed_integers/signed_i32_wrapping_neg/Forc.toml similarity index 81% rename from tests/src/signed_integers/signed_i16_twos_complement/Forc.toml rename to tests/src/signed_integers/signed_i32_wrapping_neg/Forc.toml index ea5c3227..77f6b6fc 100644 --- a/tests/src/signed_integers/signed_i16_twos_complement/Forc.toml +++ b/tests/src/signed_integers/signed_i32_wrapping_neg/Forc.toml @@ -2,7 +2,7 @@ authors = ["Fuel Labs "] entry = "main.sw" license = "Apache-2.0" -name = "i16_twos_complement_test" +name = "i32_wrapping_neg_test" [dependencies] sway_libs = { path = "../../../../libs" } diff --git a/tests/src/signed_integers/signed_i32_twos_complement/mod.rs b/tests/src/signed_integers/signed_i32_wrapping_neg/mod.rs similarity index 100% rename from tests/src/signed_integers/signed_i32_twos_complement/mod.rs rename to tests/src/signed_integers/signed_i32_wrapping_neg/mod.rs diff --git a/tests/src/signed_integers/signed_i32_wrapping_neg/src/main.sw b/tests/src/signed_integers/signed_i32_wrapping_neg/src/main.sw new file mode 100644 index 00000000..99d28b10 --- /dev/null +++ b/tests/src/signed_integers/signed_i32_wrapping_neg/src/main.sw @@ -0,0 +1,60 @@ +script; + +use sway_libs::signed_integers::i32::I32; + +fn main() -> bool { + let one = I32::from(1u32); + let neg_one = I32::from(0u32) - I32::from(1u32); + + let two = I32::from(2u32); + let neg_two = I32::from(0u32) - I32::from(2u32); + + let ten = I32::from(10u32); + let neg_ten = I32::from(0u32) - I32::from(10u32); + + let twenty_seven = I32::from(27u32); + let neg_twenty_seven = I32::from(0u32) - I32::from(27u32); + + let ninty_three = I32::from(93u32); + let neg_ninty_three = I32::from(0u32) - I32::from(93u32); + + let zero = I32::from(0u32); + let max = I32::max(); + let min = I32::min(); + let neg_min_plus_one = I32::min() + I32::from(1u32); + + let res1 = one.wrapping_neg(); + let res2 = neg_one.wrapping_neg(); + assert(res1 == neg_one); + assert(res2 == one); + + let res3 = two.wrapping_neg(); + let res4 = neg_two.wrapping_neg(); + assert(res3 == neg_two); + assert(res4 == two); + + let res5 = ten.wrapping_neg(); + let res6 = neg_ten.wrapping_neg(); + assert(res5 == neg_ten); + assert(res6 == ten); + + let res7 = twenty_seven.wrapping_neg(); + let res8 = neg_twenty_seven.wrapping_neg(); + assert(res7 == neg_twenty_seven); + assert(res8 == twenty_seven); + + let res9 = ninty_three.wrapping_neg(); + let res10 = neg_ninty_three.wrapping_neg(); + assert(res9 == neg_ninty_three); + assert(res10 == ninty_three); + + let res11 = zero.wrapping_neg(); + let res12 = max.wrapping_neg(); + let res13 = min.wrapping_neg(); + + assert(res11 == zero); + assert(res12 == neg_min_plus_one); + assert(res13 == min); + + true +} diff --git a/tests/src/signed_integers/signed_i32_wrapping_neg/tests/mod.rs b/tests/src/signed_integers/signed_i32_wrapping_neg/tests/mod.rs new file mode 100644 index 00000000..cbf3f824 --- /dev/null +++ b/tests/src/signed_integers/signed_i32_wrapping_neg/tests/mod.rs @@ -0,0 +1,22 @@ +use fuels::prelude::{abigen, launch_provider_and_get_wallet}; + +abigen!(Script( + name = "Testi32WrappingNeg", + abi = "src/signed_integers/signed_i32_wrapping_neg/out/release/i32_wrapping_neg_test-abi.json" +),); + +mod success { + + use super::*; + + #[tokio::test] + async fn runs_i32_wrapping_neg_test_script() { + let path_to_bin = + "src/signed_integers/signed_i32_wrapping_neg/out/release/i32_wrapping_neg_test.bin"; + let wallet = launch_provider_and_get_wallet().await.unwrap(); + + let instance = Testi32WrappingNeg::new(wallet, path_to_bin); + + let _result = instance.main().call().await; + } +} diff --git a/tests/src/signed_integers/signed_i64_twos_complement/src/main.sw b/tests/src/signed_integers/signed_i64_twos_complement/src/main.sw deleted file mode 100644 index 7d8a5c43..00000000 --- a/tests/src/signed_integers/signed_i64_twos_complement/src/main.sw +++ /dev/null @@ -1,29 +0,0 @@ -script; - -use sway_libs::signed_integers::i64::I64; - -fn main() -> bool { - let one = I64::from(1); - let mut res = one + I64::from(1); - assert(res.twos_complement() == I64::from(2)); - - res = I64::from(10); - assert(res.twos_complement() == I64::from(10)); - - res = I64::neg_from(5); - assert(res.twos_complement().underlying() + res.underlying() == u64::max() + 1); - - res = I64::neg_from(27); - assert(res.twos_complement().underlying() + res.underlying() == u64::max() + 1); - - res = I64::neg_from(110); - assert(res.twos_complement().underlying() + res.underlying() == u64::max() + 1); - - res = I64::neg_from(93); - assert(res.twos_complement().underlying() + res.underlying() == u64::max() + 1); - - res = I64::neg_from(78); - assert(res.twos_complement().underlying() + res.underlying() == u64::max() + 1); - - true -} diff --git a/tests/src/signed_integers/signed_i64_twos_complement/tests/mod.rs b/tests/src/signed_integers/signed_i64_twos_complement/tests/mod.rs deleted file mode 100644 index fbdddc90..00000000 --- a/tests/src/signed_integers/signed_i64_twos_complement/tests/mod.rs +++ /dev/null @@ -1,22 +0,0 @@ -use fuels::prelude::{abigen, launch_provider_and_get_wallet}; - -abigen!(Script( - name = "Testi64TwosComplement", - abi = "src/signed_integers/signed_i64_twos_complement/out/release/i64_twos_complement_test-abi.json" -),); - -mod success { - - use super::*; - - #[tokio::test] - async fn runs_i64_twos_complement_test_script() { - let path_to_bin = - "src/signed_integers/signed_i64_twos_complement/out/release/i64_twos_complement_test.bin"; - let wallet = launch_provider_and_get_wallet().await.unwrap(); - - let instance = Testi64TwosComplement::new(wallet, path_to_bin); - - let _result = instance.main().call().await; - } -} diff --git a/tests/src/signed_integers/signed_i64_twos_complement/.gitignore b/tests/src/signed_integers/signed_i64_wrapping_neg/.gitignore similarity index 100% rename from tests/src/signed_integers/signed_i64_twos_complement/.gitignore rename to tests/src/signed_integers/signed_i64_wrapping_neg/.gitignore diff --git a/tests/src/signed_integers/signed_i64_wrapping_neg/Forc.toml b/tests/src/signed_integers/signed_i64_wrapping_neg/Forc.toml new file mode 100644 index 00000000..ffeb52f3 --- /dev/null +++ b/tests/src/signed_integers/signed_i64_wrapping_neg/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "i64_wrapping_neg_test" + +[dependencies] +sway_libs = { path = "../../../../libs" } diff --git a/tests/src/signed_integers/signed_i64_twos_complement/mod.rs b/tests/src/signed_integers/signed_i64_wrapping_neg/mod.rs similarity index 100% rename from tests/src/signed_integers/signed_i64_twos_complement/mod.rs rename to tests/src/signed_integers/signed_i64_wrapping_neg/mod.rs diff --git a/tests/src/signed_integers/signed_i64_wrapping_neg/src/main.sw b/tests/src/signed_integers/signed_i64_wrapping_neg/src/main.sw new file mode 100644 index 00000000..065cbf85 --- /dev/null +++ b/tests/src/signed_integers/signed_i64_wrapping_neg/src/main.sw @@ -0,0 +1,60 @@ +script; + +use sway_libs::signed_integers::i64::I64; + +fn main() -> bool { + let one = I64::from(1u64); + let neg_one = I64::from(0u64) - I64::from(1u64); + + let two = I64::from(2u64); + let neg_two = I64::from(0u64) - I64::from(2u64); + + let ten = I64::from(10u64); + let neg_ten = I64::from(0u64) - I64::from(10u64); + + let twenty_seven = I64::from(27u64); + let neg_twenty_seven = I64::from(0u64) - I64::from(27u64); + + let ninty_three = I64::from(93u64); + let neg_ninty_three = I64::from(0u64) - I64::from(93u64); + + let zero = I64::from(0u64); + let max = I64::max(); + let min = I64::min(); + let neg_min_plus_one = I64::min() + I64::from(1); + + let res1 = one.wrapping_neg(); + let res2 = neg_one.wrapping_neg(); + assert(res1 == neg_one); + assert(res2 == one); + + let res3 = two.wrapping_neg(); + let res4 = neg_two.wrapping_neg(); + assert(res3 == neg_two); + assert(res4 == two); + + let res5 = ten.wrapping_neg(); + let res6 = neg_ten.wrapping_neg(); + assert(res5 == neg_ten); + assert(res6 == ten); + + let res7 = twenty_seven.wrapping_neg(); + let res8 = neg_twenty_seven.wrapping_neg(); + assert(res7 == neg_twenty_seven); + assert(res8 == twenty_seven); + + let res9 = ninty_three.wrapping_neg(); + let res10 = neg_ninty_three.wrapping_neg(); + assert(res9 == neg_ninty_three); + assert(res10 == ninty_three); + + let res11 = zero.wrapping_neg(); + let res12 = max.wrapping_neg(); + let res13 = min.wrapping_neg(); + + assert(res11 == zero); + assert(res12 == neg_min_plus_one); + assert(res13 == min); + + true +} diff --git a/tests/src/signed_integers/signed_i64_wrapping_neg/tests/mod.rs b/tests/src/signed_integers/signed_i64_wrapping_neg/tests/mod.rs new file mode 100644 index 00000000..a2754493 --- /dev/null +++ b/tests/src/signed_integers/signed_i64_wrapping_neg/tests/mod.rs @@ -0,0 +1,22 @@ +use fuels::prelude::{abigen, launch_provider_and_get_wallet}; + +abigen!(Script( + name = "Testi64WrappingNeg", + abi = "src/signed_integers/signed_i64_wrapping_neg/out/release/i64_wrapping_neg_test-abi.json" +),); + +mod success { + + use super::*; + + #[tokio::test] + async fn runs_i64_wrapping_neg_test_script() { + let path_to_bin = + "src/signed_integers/signed_i64_wrapping_neg/out/release/i64_wrapping_neg_test.bin"; + let wallet = launch_provider_and_get_wallet().await.unwrap(); + + let instance = Testi64WrappingNeg::new(wallet, path_to_bin); + + let _result = instance.main().call().await; + } +} diff --git a/tests/src/signed_integers/signed_i8_twos_complement/src/main.sw b/tests/src/signed_integers/signed_i8_twos_complement/src/main.sw deleted file mode 100644 index 2e4bde0c..00000000 --- a/tests/src/signed_integers/signed_i8_twos_complement/src/main.sw +++ /dev/null @@ -1,29 +0,0 @@ -script; - -use sway_libs::signed_integers::i8::I8; - -fn main() -> bool { - let one = I8::from(1u8); - let mut res = one + I8::from(1u8); - assert(res.twos_complement() == I8::from(2u8)); - - res = I8::from(10u8); - assert(res.twos_complement() == I8::from(10u8)); - - res = I8::neg_from(5); - assert(res.twos_complement().underlying() + res.underlying() == u8::max() + 1); - - res = I8::neg_from(27u8); - assert(res.twos_complement().underlying() + res.underlying() == u8::max() + 1); - - res = I8::neg_from(110u8); - assert(res.twos_complement().underlying() + res.underlying() == u8::max() + 1); - - res = I8::neg_from(93u8); - assert(res.twos_complement().underlying() + res.underlying() == u8::max() + 1); - - res = I8::neg_from(78u8); - assert(res.twos_complement().underlying() + res.underlying() == u8::max() + 1); - - true -} diff --git a/tests/src/signed_integers/signed_i8_twos_complement/tests/mod.rs b/tests/src/signed_integers/signed_i8_twos_complement/tests/mod.rs deleted file mode 100644 index 1381185a..00000000 --- a/tests/src/signed_integers/signed_i8_twos_complement/tests/mod.rs +++ /dev/null @@ -1,23 +0,0 @@ -use fuels::prelude::{abigen, launch_provider_and_get_wallet}; - -abigen!(Script( - name = "Testi8TwosComplement", - abi = - "src/signed_integers/signed_i8_twos_complement/out/release/i8_twos_complement_test-abi.json" -),); - -mod success { - - use super::*; - - #[tokio::test] - async fn runs_i8_twos_complement_test_script() { - let path_to_bin = - "src/signed_integers/signed_i8_twos_complement/out/release/i8_twos_complement_test.bin"; - let wallet = launch_provider_and_get_wallet().await.unwrap(); - - let instance = Testi8TwosComplement::new(wallet, path_to_bin); - - let _result = instance.main().call().await; - } -} diff --git a/tests/src/signed_integers/signed_i8_twos_complement/.gitignore b/tests/src/signed_integers/signed_i8_wrapping_neg/.gitignore similarity index 100% rename from tests/src/signed_integers/signed_i8_twos_complement/.gitignore rename to tests/src/signed_integers/signed_i8_wrapping_neg/.gitignore diff --git a/tests/src/signed_integers/signed_i8_wrapping_neg/Forc.toml b/tests/src/signed_integers/signed_i8_wrapping_neg/Forc.toml new file mode 100644 index 00000000..1ac93c5d --- /dev/null +++ b/tests/src/signed_integers/signed_i8_wrapping_neg/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "i8_wrapping_neg_test" + +[dependencies] +sway_libs = { path = "../../../../libs" } diff --git a/tests/src/signed_integers/signed_i8_twos_complement/mod.rs b/tests/src/signed_integers/signed_i8_wrapping_neg/mod.rs similarity index 100% rename from tests/src/signed_integers/signed_i8_twos_complement/mod.rs rename to tests/src/signed_integers/signed_i8_wrapping_neg/mod.rs diff --git a/tests/src/signed_integers/signed_i8_wrapping_neg/src/main.sw b/tests/src/signed_integers/signed_i8_wrapping_neg/src/main.sw new file mode 100644 index 00000000..8f76f322 --- /dev/null +++ b/tests/src/signed_integers/signed_i8_wrapping_neg/src/main.sw @@ -0,0 +1,60 @@ +script; + +use sway_libs::signed_integers::i8::I8; + +fn main() -> bool { + let one = I8::from(1u8); + let neg_one = I8::from(0u8) - I8::from(1u8); + + let two = I8::from(2u8); + let neg_two = I8::from(0u8) - I8::from(2u8); + + let ten = I8::from(10u8); + let neg_ten = I8::from(0u8) - I8::from(10u8); + + let twenty_seven = I8::from(27u8); + let neg_twenty_seven = I8::from(0u8) - I8::from(27u8); + + let ninty_three = I8::from(93u8); + let neg_ninty_three = I8::from(0u8) - I8::from(93u8); + + let zero = I8::from(0u8); + let max = I8::max(); + let min = I8::min(); + let neg_min_plus_one = I8::min() + I8::from(1u8); + + let res1 = one.wrapping_neg(); + let res2 = neg_one.wrapping_neg(); + assert(res1 == neg_one); + assert(res2 == one); + + let res3 = two.wrapping_neg(); + let res4 = neg_two.wrapping_neg(); + assert(res3 == neg_two); + assert(res4 == two); + + let res5 = ten.wrapping_neg(); + let res6 = neg_ten.wrapping_neg(); + assert(res5 == neg_ten); + assert(res6 == ten); + + let res7 = twenty_seven.wrapping_neg(); + let res8 = neg_twenty_seven.wrapping_neg(); + assert(res7 == neg_twenty_seven); + assert(res8 == twenty_seven); + + let res9 = ninty_three.wrapping_neg(); + let res10 = neg_ninty_three.wrapping_neg(); + assert(res9 == neg_ninty_three); + assert(res10 == ninty_three); + + let res11 = zero.wrapping_neg(); + let res12 = max.wrapping_neg(); + let res13 = min.wrapping_neg(); + + assert(res11 == zero); + assert(res12 == neg_min_plus_one); + assert(res13 == min); + + true +} diff --git a/tests/src/signed_integers/signed_i8_wrapping_neg/tests/mod.rs b/tests/src/signed_integers/signed_i8_wrapping_neg/tests/mod.rs new file mode 100644 index 00000000..4046bb3a --- /dev/null +++ b/tests/src/signed_integers/signed_i8_wrapping_neg/tests/mod.rs @@ -0,0 +1,22 @@ +use fuels::prelude::{abigen, launch_provider_and_get_wallet}; + +abigen!(Script( + name = "Testi8WrappingNeg", + abi = "src/signed_integers/signed_i8_wrapping_neg/out/release/i8_wrapping_neg_test-abi.json" +),); + +mod success { + + use super::*; + + #[tokio::test] + async fn runs_signed_i8_wrapping_neg_test_script() { + let path_to_bin = + "src/signed_integers/signed_i8_wrapping_neg/out/release/i8_wrapping_neg_test.bin"; + let wallet = launch_provider_and_get_wallet().await.unwrap(); + + let instance = Testi8WrappingNeg::new(wallet, path_to_bin); + + let _result = instance.main().call().await; + } +}