From 2659d239976b42ec540d261665bec4fb56f89d09 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Tue, 3 Sep 2024 16:47:56 +0200 Subject: [PATCH] added more tests for managed decimal var --- .../scenarios/managed_decimal.scen.json | 88 ++++++++++++++++- .../managed_decimal_logarithm.scen.json | 94 ++++++++++++++++++- .../src/managed_decimal_features.rs | 43 +++++++++ .../basic_features_managed_decimal_test.rs | 5 + .../basic-features/wasm/src/lib.rs | 9 +- framework/snippets/src/imports.rs | 5 +- 6 files changed, 236 insertions(+), 8 deletions(-) diff --git a/contracts/feature-tests/basic-features/scenarios/managed_decimal.scen.json b/contracts/feature-tests/basic-features/scenarios/managed_decimal.scen.json index b723c07b6d..e23747e4fb 100644 --- a/contracts/feature-tests/basic-features/scenarios/managed_decimal.scen.json +++ b/contracts/feature-tests/basic-features/scenarios/managed_decimal.scen.json @@ -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": "*" + } } ] -} +} \ No newline at end of file diff --git a/contracts/feature-tests/basic-features/scenarios/managed_decimal_logarithm.scen.json b/contracts/feature-tests/basic-features/scenarios/managed_decimal_logarithm.scen.json index 193517275c..6ee41eaa4d 100644 --- a/contracts/feature-tests/basic-features/scenarios/managed_decimal_logarithm.scen.json +++ b/contracts/feature-tests/basic-features/scenarios/managed_decimal_logarithm.scen.json @@ -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": "*" + } } ] -} +} \ No newline at end of file diff --git a/contracts/feature-tests/basic-features/src/managed_decimal_features.rs b/contracts/feature-tests/basic-features/src/managed_decimal_features.rs index 556641cdc9..4bb3233c70 100644 --- a/contracts/feature-tests/basic-features/src/managed_decimal_features.rs +++ b/contracts/feature-tests/basic-features/src/managed_decimal_features.rs @@ -56,4 +56,47 @@ pub trait ManagedDecimalFeatures { ) -> ManagedDecimalSigned> { x.log2().unwrap_or_else(|| sc_panic!("cannot be zero")) } + + #[endpoint] + fn managed_decimal_addition_var( + &self, + first: ManagedDecimal, + second: ManagedDecimal, + ) -> ManagedDecimal { + first + second + } + + #[endpoint] + fn managed_decimal_subtraction_var( + &self, + first: ManagedDecimal, + second: ManagedDecimal, + ) -> ManagedDecimal { + first - second + } + + #[endpoint] + fn managed_decimal_eq_var( + &self, + first: ManagedDecimal, + second: ManagedDecimal, + ) -> bool { + first.eq(&second) + } + + #[endpoint] + fn managed_decimal_ln_var( + &self, + x: ManagedDecimal, + ) -> ManagedDecimalSigned> { + x.ln().unwrap_or_else(|| sc_panic!("cannot be zero")) + } + + #[endpoint] + fn managed_decimal_log2_var( + &self, + x: ManagedDecimal, + ) -> ManagedDecimalSigned> { + x.log2().unwrap_or_else(|| sc_panic!("cannot be zero")) + } } diff --git a/contracts/feature-tests/basic-features/tests/basic_features_managed_decimal_test.rs b/contracts/feature-tests/basic-features/tests/basic_features_managed_decimal_test.rs index d4257fe078..238e4f0f4e 100644 --- a/contracts/feature-tests/basic-features/tests/basic_features_managed_decimal_test.rs +++ b/contracts/feature-tests/basic-features/tests/basic_features_managed_decimal_test.rs @@ -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"); +} diff --git a/contracts/feature-tests/basic-features/wasm/src/lib.rs b/contracts/feature-tests/basic-features/wasm/src/lib.rs index 887b75bcd5..7b988da222 100644 --- a/contracts/feature-tests/basic-features/wasm/src/lib.rs +++ b/contracts/feature-tests/basic-features/wasm/src/lib.rs @@ -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] @@ -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 ) } diff --git a/framework/snippets/src/imports.rs b/framework/snippets/src/imports.rs index a35e880c78..a22c969887 100644 --- a/framework/snippets/src/imports.rs +++ b/framework/snippets/src/imports.rs @@ -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;