Skip to content

Commit

Permalink
Merge pull request #1762 from multiversx/managed-decimal-var-tests
Browse files Browse the repository at this point in the history
added more tests for managed decimal var
  • Loading branch information
andrei-marinica authored Sep 4, 2024
2 parents 4ae95f1 + 1aa35d6 commit 896403a
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 26 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,92 @@
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "6",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_addition_var",
"arguments": [
"biguint:4|u32:2",
"biguint:5|u32:2"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"biguint:9|u32:2"
]
}
},
{
"step": "scCall",
"id": "7",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_subtraction_var",
"arguments": [
"biguint:9|u32:2",
"biguint:4|u32:2"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"biguint:5|u32:2"
]
}
},
{
"step": "scCall",
"id": "8",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_subtraction_var",
"arguments": [
"biguint:2|u32:2",
"biguint:8|u32:2"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "4",
"message": "str:cannot subtract because result would be negative",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "9",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_eq_var",
"arguments": [
"biguint:11|u32:2",
"biguint:11|u32:2"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"true"
],
"status": "",
"message": "*",
"gas": "*",
"refund": "*"
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,98 @@
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "managed_decimal_log2_var(23)",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_log2_var",
"arguments": [
"0x00000005055ae8260000000009"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"4,523648008"
],
"status": "",
"message": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "managed_decimal_ln_var(23)",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_ln_var",
"arguments": [
"0x00000005055ae8260000000009"
],
"gasLimit": "25,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"+3,135553845"
],
"status": "",
"message": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "managed_decimal_ln_var(378,298)",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_ln_var",
"arguments": [
"0x00000005581451628000000009"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x0161cc16aa"
],
"status": "",
"message": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "managed_decimal_log2_var(218,345)",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_log2_var",
"arguments": [
"0x0000000532d6604c4000000009"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"7,770385327"
],
"status": "",
"message": "*",
"gas": "*",
"refund": "*"
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,47 @@ pub trait ManagedDecimalFeatures {
) -> ManagedDecimalSigned<Self::Api, ConstDecimals<9>> {
x.log2().unwrap_or_else(|| sc_panic!("cannot be zero"))
}

#[endpoint]
fn managed_decimal_addition_var(
&self,
first: ManagedDecimal<Self::Api, NumDecimals>,
second: ManagedDecimal<Self::Api, NumDecimals>,
) -> ManagedDecimal<Self::Api, NumDecimals> {
first + second
}

#[endpoint]
fn managed_decimal_subtraction_var(
&self,
first: ManagedDecimal<Self::Api, NumDecimals>,
second: ManagedDecimal<Self::Api, NumDecimals>,
) -> ManagedDecimal<Self::Api, NumDecimals> {
first - second
}

#[endpoint]
fn managed_decimal_eq_var(
&self,
first: ManagedDecimal<Self::Api, NumDecimals>,
second: ManagedDecimal<Self::Api, NumDecimals>,
) -> bool {
first.eq(&second)
}

#[endpoint]
fn managed_decimal_ln_var(
&self,
x: ManagedDecimal<Self::Api, NumDecimals>,
) -> ManagedDecimalSigned<Self::Api, ConstDecimals<9>> {
x.ln().unwrap_or_else(|| sc_panic!("cannot be zero"))
}

#[endpoint]
fn managed_decimal_log2_var(
&self,
x: ManagedDecimal<Self::Api, NumDecimals>,
) -> ManagedDecimalSigned<Self::Api, ConstDecimals<9>> {
x.log2().unwrap_or_else(|| sc_panic!("cannot be zero"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ fn world() -> ScenarioWorld {
fn managed_decimal_test() {
world().run("scenarios/managed_decimal.scen.json");
}

#[test]
fn managed_decimal_logarithm_test() {
world().run("scenarios/managed_decimal_logarithm.scen.json");
}
9 changes: 7 additions & 2 deletions contracts/feature-tests/basic-features/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 404
// Endpoints: 408
// Async Callback: 1
// Total number of exported functions: 406
// Total number of exported functions: 410

#![no_std]

Expand Down Expand Up @@ -422,6 +422,11 @@ multiversx_sc_wasm_adapter::endpoints! {
managed_decimal_into_raw_units => managed_decimal_into_raw_units
managed_decimal_ln => managed_decimal_ln
managed_decimal_log2 => managed_decimal_log2
managed_decimal_addition_var => managed_decimal_addition_var
managed_decimal_subtraction_var => managed_decimal_subtraction_var
managed_decimal_eq_var => managed_decimal_eq_var
managed_decimal_ln_var => managed_decimal_ln_var
managed_decimal_log2_var => managed_decimal_log2_var
)
}

Expand Down
2 changes: 1 addition & 1 deletion data/codec-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ default = ["syn/full", "syn/parsing", "syn/extra-traits"]
[dependencies]
proc-macro2 = "=1.0.86"
quote = "=1.0.37"
syn = "=2.0.76"
syn = "=2.0.77"
hex = "=0.4.3"
2 changes: 1 addition & 1 deletion framework/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ categories = ["cryptography::cryptocurrencies", "development-tools::procedural-m
[dependencies]
proc-macro2 = "=1.0.86"
quote = "=1.0.37"
syn = "=2.0.76"
syn = "=2.0.77"
hex = "=0.4.3"
radix_trie = "=0.2.1"

Expand Down
5 changes: 1 addition & 4 deletions framework/snippets/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ pub use crate::{
dns_address_for_name, test_wallets, Interactor, InteractorPrepareAsync, StepBuffer,
};

pub use multiversx_sdk::{
wallet::Wallet,
data::keystore::InsertPassword,
};
pub use multiversx_sdk::{data::keystore::InsertPassword, wallet::Wallet};

pub use env_logger;
pub use tokio;

0 comments on commit 896403a

Please sign in to comment.