From 8e20f97d51be695dd569bb97900192b4a8bc3503 Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:34:13 +0530 Subject: [PATCH] add overflow and unsafe math tests (#6469) ## Description Adds some in language tests for overflow and unsafe math operations. Ensuring the operations revert ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers. --- .../test_programs/math_inline_tests/Forc.toml | 1 + .../math_inline_tests/src/main.sw | 139 ++++++++++++++++-- 2 files changed, 130 insertions(+), 10 deletions(-) diff --git a/test/src/in_language_tests/test_programs/math_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/math_inline_tests/Forc.toml index 6ac992f8d23..c2c187517b7 100644 --- a/test/src/in_language_tests/test_programs/math_inline_tests/Forc.toml +++ b/test/src/in_language_tests/test_programs/math_inline_tests/Forc.toml @@ -5,4 +5,5 @@ license = "Apache-2.0" name = "math_inline_tests" [dependencies] +core = { path = "../../../../../sway-lib-core" } std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/in_language_tests/test_programs/math_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/math_inline_tests/src/main.sw index 4aec0b93213..c7ac059259b 100644 --- a/test/src/in_language_tests/test_programs/math_inline_tests/src/main.sw +++ b/test/src/in_language_tests/test_programs/math_inline_tests/src/main.sw @@ -655,48 +655,167 @@ fn math_test_parity_u256_log_with_ruint() { } } +// u256 log invalid operations tests #[test(should_revert)] fn math_u256_log_fail_base_0() { - let _result = u256::from(2_u64).log(u256::from(0_u64)); + let result = u256::from(2_u64).log(u256::from(0_u64)); + log(result); } #[test(should_revert)] fn math_u256_log_fail_base_1() { - let _result = u256::from(2_u64).log(u256::from(1_u64)); + let result = u256::from(2_u64).log(u256::from(1_u64)); + log(result); } #[test(should_revert)] fn math_u256_log_fail_x_0() { - let _result = u256::from(0_u64).log(u256::from(2_u64)); + let result = u256::from(0_u64).log(u256::from(2_u64)); + log(result); } #[test(should_revert)] fn math_u256_log2_fail_x_0() { - let _result = u256::from(0_u64).log2(); + let result = u256::from(0_u64).log2(); + log(result); +} + +// u64 log invalid operations tests +#[test(should_revert)] +fn math_u64_log_fail_base_0() { + let result = 2_u64.log(0_u64); + log(result); +} + +#[test(should_revert)] +fn math_u64_log_fail_base_1() { + let result = 2_u64.log(1_u64); + log(result); +} + +#[test(should_revert)] +fn math_u64_log_fail_x_0() { + let result = 0_u64.log(2_u64); + log(result); +} + +#[test(should_revert)] +fn math_u64_log2_fail_x_0() { + let result = 0_u64.log2(); + log(result); +} + +// u32 log invalid operations tests +#[test(should_revert)] +fn math_u32_log_fail_base_0() { + let result = 2_u32.log(0_u32); + log(result); +} + +#[test(should_revert)] +fn math_u32_log_fail_base_1() { + let result = 2_u32.log(1_u32); + log(result); +} + +#[test(should_revert)] +fn math_u32_log_fail_x_0() { + let result = 0_u32.log(2_u32); + log(result); +} + +#[test(should_revert)] +fn math_u32_log2_fail_x_0() { + let result = 0_u32.log2(); + log(result); +} + +// u16 log invalid operations tests +#[test(should_revert)] +fn math_u16_log_fail_base_0() { + let result = 2_u16.log(0_u16); + log(result); +} + +#[test(should_revert)] +fn math_u16_log_fail_base_1() { + let result = 2_u16.log(1_u16); + log(result); +} + +#[test(should_revert)] +fn math_u16_log_fail_x_0() { + let result = 0_u16.log(2_u16); + log(result); +} + +#[test(should_revert)] +fn math_u16_log2_fail_x_0() { + let result = 0_u16.log2(); + log(result); +} + +// u8 log invalid operations tests +#[test(should_revert)] +fn math_u8_log_fail_base_0() { + let result = 2_u8.log(0_u8); + log(result); +} + +#[test(should_revert)] +fn math_u8_log_fail_base_1() { + let result = 2_u8.log(1_u8); + log(result); +} + +#[test(should_revert)] +fn math_u8_log_fail_x_0() { + let result = 0_u8.log(2_u8); + log(result); +} + +#[test(should_revert)] +fn math_u8_log2_fail_x_0() { + let result = 0_u8.log2(); + log(result); } #[test(should_revert)] fn revert_math_u8_pow_overflow() { - let _result = 2_u8.pow(8); + let result = 2_u8.pow(8); + log(result); } +// pow overflow tests #[test(should_revert)] fn revert_math_u16_pow_overflow() { - let _result = 2_u16.pow(16); + let result = 2_u16.pow(16); + log(result); } #[test(should_revert)] fn revert_math_u32_pow_overflow() { - let _result = 2_u32.pow(32); + let result = 2_u32.pow(32); + log(result); } #[test(should_revert)] fn revert_math_u64_pow_overflow() { - let _result = 2_u64.pow(64); - log(_result); + let result = 2_u64.pow(64); + log(result); } #[test(should_revert)] fn revert_math_u256_pow_overflow() { - let _result = 2.as_u256().pow(256); + let result = 2.as_u256().pow(256); + log(result); +} + +#[test(should_revert)] +fn math_0th_root_fail() { + let res = asm(r1: 100, r2: 0, r3) { + mroo r3 r1 r2; + r3: u8 + }; + log(res); }