Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assertApproxEqRel edge case #7630

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions crates/cheatcodes/src/test/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,13 +1122,17 @@ fn uint_assert_approx_eq_rel(
right: U256,
max_delta: U256,
) -> Result<Vec<u8>, EqRelAssertionError<U256>> {
if right.is_zero() && !left.is_zero() {
return Err(EqRelAssertionError::Failure(Box::new(EqRelAssertionFailure {
left,
right,
max_delta,
real_delta: EqRelDelta::Undefined,
})))
if right.is_zero() {
if left.is_zero() {
return Ok(Default::default())
} else {
return Err(EqRelAssertionError::Failure(Box::new(EqRelAssertionFailure {
left,
right,
max_delta,
real_delta: EqRelDelta::Undefined,
})))
};
}

let delta = get_delta_uint(left, right)
Expand All @@ -1153,13 +1157,17 @@ fn int_assert_approx_eq_rel(
right: I256,
max_delta: U256,
) -> Result<Vec<u8>, EqRelAssertionError<I256>> {
if right.is_zero() && !left.is_zero() {
return Err(EqRelAssertionError::Failure(Box::new(EqRelAssertionFailure {
left,
right,
max_delta,
real_delta: EqRelDelta::Undefined,
})))
if right.is_zero() {
if left.is_zero() {
return Ok(Default::default())
} else {
return Err(EqRelAssertionError::Failure(Box::new(EqRelAssertionFailure {
left,
right,
max_delta,
real_delta: EqRelDelta::Undefined,
})))
}
}

let (_, abs_right) = right.into_sign_and_abs();
Expand Down
2 changes: 2 additions & 0 deletions testdata/default/cheats/Assert.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -823,5 +823,7 @@ contract AssertionsTest is DSTest {
bytes("assertion failed: 1 !~= 0 (max delta: 0.0000000000000000%, real delta: undefined)")
);
vm.assertApproxEqRel(uint256(1), uint256(0), uint256(0));

vm.assertApproxEqRel(uint256(0), uint256(0), uint256(0));
}
}
Loading