Skip to content

Commit

Permalink
fix: assertApproxEqRel edge case (#7630)
Browse files Browse the repository at this point in the history
fix: assertApproxEqRel edge case
  • Loading branch information
klkvr authored Apr 10, 2024
1 parent 43fb17b commit 440ec52
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
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));
}
}

0 comments on commit 440ec52

Please sign in to comment.