From 9d913eab9d51d0b4a054d465dd46db1359135023 Mon Sep 17 00:00:00 2001 From: aritkulova Date: Thu, 11 Jun 2026 15:24:09 +0300 Subject: [PATCH 1/2] renamed asserts functions to be more consistent; removed unsuported "pub" modifier --- simf/asserts.simf | 24 +++++++++++++----------- simf/mock/asserts_mock.simf | 24 +++++++++++++----------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/simf/asserts.simf b/simf/asserts.simf index a95ba99..7707a59 100644 --- a/simf/asserts.simf +++ b/simf/asserts.simf @@ -1,55 +1,57 @@ /// Asserts that two u8 are equal -pub fn assert_eq8(a: u8, b: u8) { +fn assert_eq_8(a: u8, b: u8) { assert!(jet::eq_8(a, b)); } /// Asserts that two u16 are equal -pub fn assert_eq16(a: u16, b: u16) { +fn assert_eq_16(a: u16, b: u16) { assert!(jet::eq_16(a, b)); } /// Asserts that two u32 are equal -pub fn assert_eq32(a: u32, b: u32) { +fn assert_eq_32(a: u32, b: u32) { assert!(jet::eq_32(a, b)); } /// Asserts that two u64 are equal -pub fn assert_eq64(a: u64, b: u64) { +fn assert_eq_64(a: u64, b: u64) { assert!(jet::eq_64(a, b)); } +// todo: assert_eq_128 + /// Asserts that two u256 are equal -pub fn assert_eq256(a: u256, b: u256) { +fn assert_eq_256(a: u256, b: u256) { assert!(jet::eq_256(a, b)); } /// Asserts that provided u8 Option value is a None -pub fn assert_none8(val: Option) { +fn assert_none_8(val: Option) { assert!(is_none::(val)); } /// Asserts that provided u16 Option value is a None -pub fn assert_none16(val: Option) { +fn assert_none_16(val: Option) { assert!(is_none::(val)); } /// Asserts that provided u32 Option value is a None -pub fn assert_none32(val: Option) { +fn assert_none_32(val: Option) { assert!(is_none::(val)); } /// Asserts that provided u64 Option value is a None -pub fn assert_none64(val: Option) { +fn assert_none_64(val: Option) { assert!(is_none::(val)); } /// Asserts that provided u128 Option value is a None -pub fn assert_none128(val: Option) { +fn assert_none_128(val: Option) { assert!(is_none::(val)); } /// Asserts that provided u256 Option value is a None -pub fn assert_none256(val: Option) { +fn assert_none_256(val: Option) { assert!(is_none::(val)); } diff --git a/simf/mock/asserts_mock.simf b/simf/mock/asserts_mock.simf index f20b8c8..6dea29e 100644 --- a/simf/mock/asserts_mock.simf +++ b/simf/mock/asserts_mock.simf @@ -1,45 +1,47 @@ // todo: switch to function import when available -pub fn assert_eq8(a: u8, b: u8) { // 1 +fn assert_eq_8(a: u8, b: u8) { // 0 assert!(jet::eq_8(a, b)); } -pub fn assert_eq16(a: u16, b: u16) { // 2 +fn assert_eq_16(a: u16, b: u16) { // 1 assert!(jet::eq_16(a, b)); } -pub fn assert_eq32(a: u32, b: u32) { // 3 +fn assert_eq_32(a: u32, b: u32) { // 2 assert!(jet::eq_32(a, b)); } -pub fn assert_eq64(a: u64, b: u64) { // 4 +fn assert_eq_64(a: u64, b: u64) { // 3 assert!(jet::eq_64(a, b)); } -pub fn assert_eq256(a: u256, b: u256) { // 5 +// todo: assert_eq_128 + +fn assert_eq_256(a: u256, b: u256) { // 4 assert!(jet::eq_256(a, b)); } -pub fn assert_none8(val: Option) { // 6 +fn assert_none_8(val: Option) { // 5 assert!(is_none::(val)); } -pub fn assert_none16(val: Option) { // 7 +fn assert_none_16(val: Option) { // 6 assert!(is_none::(val)); } -pub fn assert_none32(val: Option) { // 8 +fn assert_none_32(val: Option) { // 7 assert!(is_none::(val)); } -pub fn assert_none64(val: Option) { // 9 +fn assert_none_64(val: Option) { // 8 assert!(is_none::(val)); } -pub fn assert_none128(val: Option) { // 10 +fn assert_none_128(val: Option) { // 9 assert!(is_none::(val)); } -pub fn assert_none256(val: Option) { // 11 +fn assert_none_256(val: Option) { // 10 assert!(is_none::(val)); } From fa1984e2673fad78e29ec70e4c24c7755ae1f634 Mon Sep 17 00:00:00 2001 From: aritkulova Date: Thu, 11 Jun 2026 15:35:13 +0300 Subject: [PATCH 2/2] made asserts test more detailed; added random generation of test data --- Cargo.lock | 1 + Cargo.toml | 1 + simf/mock/asserts_mock.simf | 172 +++++++++------- tests/asserts_test.rs | 399 ++++++++++++++++++++++++++++++++---- tests/helper.rs | 32 +++ 5 files changed, 483 insertions(+), 122 deletions(-) create mode 100644 tests/helper.rs diff --git a/Cargo.lock b/Cargo.lock index 97599e2..c59c2ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1233,6 +1233,7 @@ name = "simplicityhl_std" version = "0.1.0" dependencies = [ "anyhow", + "rand", "smplx-std", ] diff --git a/Cargo.toml b/Cargo.toml index 074643e..c2cd2be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ version = "0.1.0" smplx-std = { version = ">=0.0.6, <0.1.0" } anyhow = { version = "1.0.101" } +rand = { version = "0.8.6" } diff --git a/simf/mock/asserts_mock.simf b/simf/mock/asserts_mock.simf index 6dea29e..fe2fb62 100644 --- a/simf/mock/asserts_mock.simf +++ b/simf/mock/asserts_mock.simf @@ -45,90 +45,106 @@ fn assert_none_256(val: Option) { // 10 assert!(is_none::(val)); } +// helper fn if_test_this_function(index: u8, flag: u8) -> bool { jet::eq_8(index, flag) } fn main() { - let flag: u8 = witness::FLAG; - let ifHappyPath: bool = if_test_this_function(0, flag); + let function_index: u8 = witness::FUNCTION_INDEX; - let some_u8: u8 = 255; - let some_u16: u16 = 65535; - let some_u32: u32 = 4294967295; - let some_u64: u64 = 18446744073709551615; - let some_u128: u128 = 340282366920938463463374607431768211455; - let some_u256: u256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935; + let first_arg_u8: Option = witness::FIRST_ARG_U8; + let second_arg_u8: Option = witness::SECOND_ARG_U8; - match ifHappyPath { + let first_arg_u16: Option = witness::FIRST_ARG_U16; + let second_arg_u16: Option = witness::SECOND_ARG_U16; + + let first_arg_u32: Option = witness::FIRST_ARG_U32; + let second_arg_u32: Option = witness::SECOND_ARG_U32; + + let first_arg_u64: Option = witness::FIRST_ARG_U64; + let second_arg_u64: Option = witness::SECOND_ARG_U64; + + let first_arg_u128: Option = witness::FIRST_ARG_U128; + let second_arg_u128: Option = witness::SECOND_ARG_U128; + + let first_arg_u256: Option = witness::FIRST_ARG_U256; + let second_arg_u256: Option = witness::SECOND_ARG_U256; + + match if_test_this_function(0, function_index) { + true => { + assert_eq_8(unwrap(first_arg_u8), unwrap(second_arg_u8)); + }, + false => (), + }; + + match if_test_this_function(1, function_index) { + true => { + assert_eq_16(unwrap(first_arg_u16), unwrap(second_arg_u16)); + }, + false => (), + }; + + match if_test_this_function(2, function_index) { + true => { + assert_eq_32(unwrap(first_arg_u32), unwrap(second_arg_u32)); + }, + false => (), + }; + + match if_test_this_function(3, function_index) { + true => { + assert_eq_64(unwrap(first_arg_u64), unwrap(second_arg_u64)); + }, + false => (), + }; + + match if_test_this_function(4, function_index) { + true => { + assert_eq_256(unwrap(first_arg_u256), unwrap(second_arg_u256)); + }, + false => (), + }; + + match if_test_this_function(5, function_index) { + true => { + assert_none_8(first_arg_u8); + }, + false => (), + }; + + match if_test_this_function(6, function_index) { + true => { + assert_none_16(first_arg_u16); + }, + false => (), + }; + + match if_test_this_function(7, function_index) { + true => { + assert_none_32(first_arg_u32); + }, + false => (), + }; + + match if_test_this_function(8, function_index) { + true => { + assert_none_64(first_arg_u64); + }, + false => (), + }; + + match if_test_this_function(9, function_index) { + true => { + assert_none_128(first_arg_u128); + }, + false => (), + }; + + match if_test_this_function(10, function_index) { true => { - assert_eq8(some_u8, some_u8); - assert_eq16(some_u16, some_u16); - assert_eq32(some_u32, some_u32); - assert_eq64(some_u64, some_u64); - assert_eq256(some_u256, some_u256); - - assert_none8(None); - assert_none16(None); - assert_none32(None); - assert_none64(None); - assert_none128(None); - assert_none256(None); - }, false => { - match if_test_this_function(1, flag) { - true => assert_eq8(1, 2), - false => (), - }; - - match if_test_this_function(2, flag) { - true => assert_eq16(1, 2), - false => (), - }; - - match if_test_this_function(3, flag) { - true => assert_eq32(1, 2), - false => (), - }; - - match if_test_this_function(4, flag) { - true => assert_eq64(1, 2), - false => (), - }; - - match if_test_this_function(5, flag) { - true => assert_eq256(1, 2), - false => (), - }; - - match if_test_this_function(6, flag) { - true => assert_none8(Some(some_u8)), - false => (), - }; - - match if_test_this_function(7, flag) { - true => assert_none16(Some(some_u16)), - false => (), - }; - - match if_test_this_function(8, flag) { - true => assert_none32(Some(some_u32)), - false => (), - }; - - match if_test_this_function(9, flag) { - true => assert_none64(Some(some_u64)), - false => (), - }; - - match if_test_this_function(10, flag) { - true => assert_none128(Some(some_u128)), - false => (), - }; - - match if_test_this_function(11, flag) { - true => assert_none256(Some(some_u256)), - false => (), - }; - } - } + assert_none_256(first_arg_u256); + }, + false => (), + }; } diff --git a/tests/asserts_test.rs b/tests/asserts_test.rs index 468340c..b593cee 100644 --- a/tests/asserts_test.rs +++ b/tests/asserts_test.rs @@ -1,9 +1,44 @@ -use simplex::simplicityhl::elements::{Script}; +use simplex::simplicityhl::elements::Script; use simplex::transaction::{FinalTransaction, PartialInput, ProgramInput, RequiredSignature}; use simplicityhl_std::artifacts::mock::asserts_mock::AssertsMockProgram; -use simplicityhl_std::artifacts::mock::asserts_mock::derived_asserts_mock::{AssertsMockWitness, AssertsMockArguments}; +use simplicityhl_std::artifacts::mock::asserts_mock::derived_asserts_mock::{ + AssertsMockArguments, AssertsMockWitness, +}; + +mod helper; +use crate::helper::{cast_to_bool, generate_uints_in_one_range}; + +enum FunctionToTest { + AssertEq8, + AssertEq16, + AssertEq32, + AssertEq64, + AssertEq256, + AssertNone8, + AssertNone16, + AssertNone32, + AssertNone64, + AssertNone128, + AssertNone256, +} + +const DEFAULT_SOME_U8: Option = Some(0); +const DEFAULT_SOME_U16: Option = Some(0); +const DEFAULT_SOME_U32: Option = Some(0); +const DEFAULT_SOME_U64: Option = Some(0); +const DEFAULT_SOME_U128: Option = Some(0); +const DEFAULT_SOME_U256: Option<[u8; 32]> = Some([0; 32]); + +pub enum IfTestSameValues { + DifferentValues, + SameValues, +} +pub enum IfTestNoneValue { + NotNoneValue, + NoneValue, +} fn get_asserts_test_script(context: &simplex::TestContext) -> (AssertsMockProgram, Script) { let arguments = AssertsMockArguments {}; @@ -23,10 +58,108 @@ fn fund_script(context: &simplex::TestContext) -> anyhow::Result<()> { let tx_receipt = signer.send(asserts_script.clone(), 50)?; println!("Broadcast: {}", tx_receipt); - Ok(()) + Ok(()) +} + +fn generate_test_witness( + function_index: FunctionToTest, + if_same_values: bool, + if_none_value: bool, +) -> AssertsMockWitness { + let mut witness: AssertsMockWitness = AssertsMockWitness { + function_index: 0, + first_arg_u8: DEFAULT_SOME_U8, + second_arg_u8: DEFAULT_SOME_U8, + first_arg_u16: DEFAULT_SOME_U16, + second_arg_u16: DEFAULT_SOME_U16, + first_arg_u32: DEFAULT_SOME_U32, + second_arg_u32: DEFAULT_SOME_U32, + first_arg_u64: DEFAULT_SOME_U64, + second_arg_u64: DEFAULT_SOME_U64, + first_arg_u128: DEFAULT_SOME_U128, + second_arg_u128: DEFAULT_SOME_U128, + first_arg_u256: DEFAULT_SOME_U256, + second_arg_u256: DEFAULT_SOME_U256, + }; + + match function_index { + FunctionToTest::AssertEq8 => { + let (first_arg, second_arg) = + generate_uints_in_one_range(if_same_values, 0, u8::MAX as u128); + + (witness.first_arg_u8, witness.second_arg_u8) = + (Some(first_arg as u8), Some(second_arg as u8)); + } + FunctionToTest::AssertEq16 => { + let (first_arg, second_arg) = + generate_uints_in_one_range(if_same_values, 0, u16::MAX as u128); + + (witness.first_arg_u16, witness.second_arg_u16) = + (Some(first_arg as u16), Some(second_arg as u16)); + } + FunctionToTest::AssertEq32 => { + let (first_arg, second_arg) = + generate_uints_in_one_range(if_same_values, 0, u32::MAX as u128); + + (witness.first_arg_u32, witness.second_arg_u32) = + (Some(first_arg as u32), Some(second_arg as u32)); + } + FunctionToTest::AssertEq64 => { + let (first_arg, second_arg) = + generate_uints_in_one_range(if_same_values, 0, u64::MAX as u128); + + (witness.first_arg_u64, witness.second_arg_u64) = + (Some(first_arg as u64), Some(second_arg as u64)); + } + FunctionToTest::AssertEq256 => { + let (first_arg, second_arg) = + generate_uints_in_one_range(if_same_values, 0, u8::MAX as u128); + + (witness.first_arg_u256, witness.second_arg_u256) = + (Some([first_arg as u8; 32]), Some([second_arg as u8; 32])); + } + FunctionToTest::AssertNone8 => { + if if_none_value { + witness.first_arg_u8 = None; + }; + } + FunctionToTest::AssertNone16 => { + if if_none_value { + witness.first_arg_u16 = None; + }; + } + FunctionToTest::AssertNone32 => { + if if_none_value { + witness.first_arg_u32 = None; + }; + } + FunctionToTest::AssertNone64 => { + if if_none_value { + witness.first_arg_u64 = None; + }; + } + FunctionToTest::AssertNone128 => { + if if_none_value { + witness.first_arg_u128 = None; + }; + } + FunctionToTest::AssertNone256 => { + if if_none_value { + witness.first_arg_u256 = None; + }; + } + } + + witness.function_index = function_index as u8; + witness } -fn spend_script(context: &simplex::TestContext, flag: u8) -> anyhow::Result<()> { +fn spend_script( + context: &simplex::TestContext, + function_index: FunctionToTest, + if_same_values: IfTestSameValues, + if_none_value: IfTestNoneValue, +) -> anyhow::Result<()> { let signer = context.get_default_signer(); let provider = context.get_default_provider(); @@ -36,11 +169,18 @@ fn spend_script(context: &simplex::TestContext, flag: u8) -> anyhow::Result<()> let mut ft = FinalTransaction::new(); - let witness = AssertsMockWitness {flag: flag}; + let witness: AssertsMockWitness = generate_test_witness( + function_index, + cast_to_bool(if_same_values as u8), + cast_to_bool(if_none_value as u8), + ); ft.add_program_input( PartialInput::new(asserts_utxos[0].clone()), - ProgramInput::new(Box::new(asserts_program.as_ref().clone()), Box::new(witness.clone())), + ProgramInput::new( + Box::new(asserts_program.as_ref().clone()), + Box::new(witness.clone()), + ), RequiredSignature::None, ); @@ -51,22 +191,28 @@ fn spend_script(context: &simplex::TestContext, flag: u8) -> anyhow::Result<()> } #[simplex::test] -fn asserts_test_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 0; - +fn assert_eq_8_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - spend_script(&context, flag)?; + spend_script( + &context, + FunctionToTest::AssertEq8, + IfTestSameValues::SameValues, + IfTestNoneValue::NotNoneValue, + )?; Ok(()) } #[simplex::test] -fn assert_eq8_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 1; - +fn assert_eq_8_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertEq8, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -80,12 +226,28 @@ fn assert_eq8_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> } #[simplex::test] -fn assert_eq16_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 2; +fn assert_eq_16_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + spend_script( + &context, + FunctionToTest::AssertEq16, + IfTestSameValues::SameValues, + IfTestNoneValue::NotNoneValue, + )?; + + Ok(()) +} +#[simplex::test] +fn assert_eq_16_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertEq16, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -99,12 +261,28 @@ fn assert_eq16_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> } #[simplex::test] -fn assert_eq32_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 3; +fn assert_eq_32_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + spend_script( + &context, + FunctionToTest::AssertEq32, + IfTestSameValues::SameValues, + IfTestNoneValue::NotNoneValue, + )?; + Ok(()) +} + +#[simplex::test] +fn assert_eq_32_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertEq32, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -118,12 +296,28 @@ fn assert_eq32_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> } #[simplex::test] -fn assert_eq64_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 4; +fn assert_eq_64_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + spend_script( + &context, + FunctionToTest::AssertEq64, + IfTestSameValues::SameValues, + IfTestNoneValue::NotNoneValue, + )?; + Ok(()) +} + +#[simplex::test] +fn assert_eq_64_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertEq64, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -137,12 +331,28 @@ fn assert_eq64_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> } #[simplex::test] -fn assert_eq256_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 5; +fn assert_eq_256_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + spend_script( + &context, + FunctionToTest::AssertEq256, + IfTestSameValues::SameValues, + IfTestNoneValue::NotNoneValue, + )?; + Ok(()) +} + +#[simplex::test] +fn assert_eq_256_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertEq256, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -156,12 +366,28 @@ fn assert_eq256_unhappy_path(context: simplex::TestContext) -> anyhow::Result<() } #[simplex::test] -fn assert_none8_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 6; +fn assert_none_8_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + spend_script( + &context, + FunctionToTest::AssertNone8, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NoneValue, + )?; + Ok(()) +} + +#[simplex::test] +fn assert_none_8_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertNone8, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -175,12 +401,29 @@ fn assert_none8_unhappy_path(context: simplex::TestContext) -> anyhow::Result<() } #[simplex::test] -fn assert_none16_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 7; +fn assert_none_16_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + + spend_script( + &context, + FunctionToTest::AssertNone16, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NoneValue, + )?; + Ok(()) +} + +#[simplex::test] +fn assert_none_16_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertNone16, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -194,12 +437,29 @@ fn assert_none16_unhappy_path(context: simplex::TestContext) -> anyhow::Result<( } #[simplex::test] -fn assert_none32_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 8; +fn assert_none_32_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + + spend_script( + &context, + FunctionToTest::AssertNone32, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NoneValue, + )?; + + Ok(()) +} +#[simplex::test] +fn assert_none_32_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertNone32, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -213,12 +473,29 @@ fn assert_none32_unhappy_path(context: simplex::TestContext) -> anyhow::Result<( } #[simplex::test] -fn assert_none64_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 9; +fn assert_none_64_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + + spend_script( + &context, + FunctionToTest::AssertNone64, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NoneValue, + )?; + Ok(()) +} + +#[simplex::test] +fn assert_none_64_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertNone64, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -232,12 +509,29 @@ fn assert_none64_unhappy_path(context: simplex::TestContext) -> anyhow::Result<( } #[simplex::test] -fn assert_none128_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 10; +fn assert_none_128_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + + spend_script( + &context, + FunctionToTest::AssertNone128, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NoneValue, + )?; + + Ok(()) +} +#[simplex::test] +fn assert_none_128_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertNone128, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), @@ -251,12 +545,29 @@ fn assert_none128_unhappy_path(context: simplex::TestContext) -> anyhow::Result< } #[simplex::test] -fn assert_none256_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - let flag: u8 = 11; +fn assert_none_256_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + spend_script( + &context, + FunctionToTest::AssertNone256, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NoneValue, + )?; + + Ok(()) +} + +#[simplex::test] +fn assert_none_256_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { fund_script(&context)?; - let txid_result = spend_script(&context, flag); + let txid_result = spend_script( + &context, + FunctionToTest::AssertNone256, + IfTestSameValues::DifferentValues, + IfTestNoneValue::NotNoneValue, + ); assert!( txid_result.is_err(), diff --git a/tests/helper.rs b/tests/helper.rs new file mode 100644 index 0000000..b714244 --- /dev/null +++ b/tests/helper.rs @@ -0,0 +1,32 @@ +use rand::Rng; + +#[allow(dead_code)] +pub enum IfTestOverflow { + NotOverflow, + Overflow, +} + +pub fn cast_to_bool(flag: u8) -> bool { + flag == 1 +} + +#[allow(dead_code)] +pub fn generate_uints_in_one_range(if_same: bool, min_val: u128, max_val: u128) -> (u128, u128) { + let some_u = rand::thread_rng().gen_range(min_val..=max_val); + + if if_same { + (some_u, some_u) + } else { + if min_val == max_val { + panic!("Can not generate different values when range is one number"); + } + + let mut other_u = rand::thread_rng().gen_range(min_val..=max_val); + + while other_u == some_u { + other_u = rand::thread_rng().gen_range(min_val..=max_val); + } + + (some_u, other_u) + } +}