Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility functions and tests to signed integers #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions sway_libs/src/signed_integers/i128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ impl I128 {
lower: 0,
}
}

pub fn zero() -> Self {
Self {
underlying: U128::from((0, 0)),
}
}
}

impl From<U128> for I128 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)))
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫡

14 changes: 14 additions & 0 deletions sway_libs/src/signed_integers/i16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -159,3 +167,9 @@ impl core::ops::Subtract for I16 {
res
}
}

impl I16{
pub fn flip(self) -> Self {
self * Self::neg_from(1)
}
}
34 changes: 34 additions & 0 deletions sway_libs/src/signed_integers/i256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ impl I256 {
d: 0,
}
}

pub fn zero() -> Self {
Self {
underlying: U256::from((0, 0, 0, 0)),
}
}
}

impl From<U256> for I256 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)))
}
}
14 changes: 14 additions & 0 deletions sway_libs/src/signed_integers/i32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -158,3 +166,9 @@ impl core::ops::Divide for I32 {
res
}
}

impl I32{
pub fn flip(self) -> Self {
self * Self::neg_from(1)
}
}
14 changes: 14 additions & 0 deletions sway_libs/src/signed_integers/i64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -158,3 +166,9 @@ impl core::ops::Divide for I64 {
res
}
}

impl I64{
pub fn flip(self) -> Self {
self * Self::neg_from(1)
}
}
14 changes: 14 additions & 0 deletions sway_libs/src/signed_integers/i8.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -155,3 +163,9 @@ impl core::ops::Subtract for I8 {
res
}
}

impl I8{
pub fn flip(self) -> Self {
self * Self::neg_from(1)
}
}
19 changes: 19 additions & 0 deletions tests/src/test_projects/signed_i128/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion tests/src/test_projects/signed_i128/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions tests/src/test_projects/signed_i16/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion tests/src/test_projects/signed_i16/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
19 changes: 19 additions & 0 deletions tests/src/test_projects/signed_i256/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion tests/src/test_projects/signed_i256/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions tests/src/test_projects/signed_i32/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion tests/src/test_projects/signed_i32/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions tests/src/test_projects/signed_i64/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion tests/src/test_projects/signed_i64/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions tests/src/test_projects/signed_i8/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion tests/src/test_projects/signed_i8/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}