Skip to content

Commit

Permalink
added more tests for managed decimal var
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Sep 3, 2024
1 parent b26f964 commit 2659d23
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 8 deletions.
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": [
"0x000000010400000002",
"0x000000010500000002"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x000000010900000002"
]
}
},
{
"step": "scCall",
"id": "7",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_subtraction_var",
"arguments": [
"0x000000010900000002",
"0x000000010400000002"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x000000010500000002"
]
}
},
{
"step": "scCall",
"id": "8",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_subtraction_var",
"arguments": [
"0x000000010200000002",
"0x000000010800000002"
],
"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": [
"0x000000010d00000002",
"0x000000010d00000002"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x01"
],
"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: 403
// Endpoints: 408
// Async Callback: 1
// Total number of exported functions: 405
// Total number of exported functions: 410

#![no_std]

Expand Down Expand Up @@ -421,6 +421,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
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 2659d23

Please sign in to comment.