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 2 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
36 changes: 36 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,11 @@ impl core::ops::Subtract for I128 {
res
}
}

impl I128 {
pub fn flip(self) -> Self {
self * Self {
underlying: Self::indent() - self.underlying,
}
}
}
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.

🫡

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(10000000), None);
Copy link

Choose a reason for hiding this comment

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

Same suggestion for other instances of this, for readability purposes.

Suggested change
let params = TxParameters::new(Some(1), Some(10000000), None);
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);
Copy link

Choose a reason for hiding this comment

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

The underscore is used for unused variables.
Same suggestion for other instances of this usage.

Suggested change
let _result = instance.main().tx_params(params).call().await;
assert_eq!(_result.is_err(), false);
let result = instance.main().tx_params(params).call().await.unwrap();
assert!(result.value);

}
}
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(10000000), None);
let _result = instance.main().tx_params(params).call().await;
assert_eq!(_result.is_err(), false);
}
}
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(10000000), None);
let _result = instance.main().tx_params(params).call().await;
assert_eq!(_result.is_err(), false);
}
}
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(10000000), None);
let _result = instance.main().tx_params(params).call().await;
assert_eq!(_result.is_err(), false);
}
}
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(10000000), None);
let _result = instance.main().tx_params(params).call().await;
assert_eq!(_result.is_err(), false);
}
}
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(10000000), None);
let _result = instance.main().tx_params(params).call().await;
assert_eq!(_result.is_err(), false);
}
}