Skip to content

Commit

Permalink
add overflow and unsafe math tests (#6469)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
SwayStar123 authored Aug 29, 2024
1 parent 93cc212 commit 8e20f97
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ license = "Apache-2.0"
name = "math_inline_tests"

[dependencies]
core = { path = "../../../../../sway-lib-core" }
std = { path = "../../../../../sway-lib-std" }
139 changes: 129 additions & 10 deletions test/src/in_language_tests/test_programs/math_inline_tests/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 8e20f97

Please sign in to comment.