Skip to content

Commit

Permalink
Merge pull request #681 from morpho-org/test/bound-improve
Browse files Browse the repository at this point in the history
Improve bounds in tests
  • Loading branch information
MathisGD authored May 14, 2024
2 parents d36719d + 6660947 commit 8f4c620
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
9 changes: 1 addition & 8 deletions test/forge/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,6 @@ contract BaseTest is Test {
return bound(blocks, 1, type(uint32).max);
}

/// @dev Bounds the fuzzing input to a non-zero address.
/// @dev This function should be used in place of `vm.assume` in invariant test handler functions:
/// https://github.com/foundry-rs/foundry/issues/4190.
function _boundAddressNotZero(address input) internal view virtual returns (address) {
return address(uint160(bound(uint256(uint160(input)), 1, type(uint160).max)));
}

function _supply(uint256 amount) internal {
loanToken.setBalance(address(this), amount);
morpho.supply(marketParams, amount, 0, address(this), hex"");
Expand Down Expand Up @@ -194,7 +187,7 @@ contract BaseTest is Test {

uint256 maxCollateral =
amountBorrowed.wDivDown(marketParams.lltv).mulDivDown(ORACLE_PRICE_SCALE, priceCollateral);
amountCollateral = bound(amountBorrowed, 0, Math.min(maxCollateral, MAX_COLLATERAL_ASSETS));
amountCollateral = bound(amountCollateral, 0, Math.min(maxCollateral, MAX_COLLATERAL_ASSETS));

vm.assume(amountCollateral > 0);
return (amountCollateral, amountBorrowed, priceCollateral);
Expand Down
6 changes: 3 additions & 3 deletions test/forge/invariant/MorphoInvariantTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ contract MorphoInvariantTest is InvariantTest {
function withdrawAssetsOnBehalfNoRevert(uint256 marketSeed, uint256 assets, uint256 onBehalfSeed, address receiver)
external
{
receiver = _boundAddressNotZero(receiver);
vm.assume(receiver != address(0));

MarketParams memory _marketParams = _randomMarket(marketSeed);

Expand All @@ -251,7 +251,7 @@ contract MorphoInvariantTest is InvariantTest {
function borrowAssetsOnBehalfNoRevert(uint256 marketSeed, uint256 assets, uint256 onBehalfSeed, address receiver)
external
{
receiver = _boundAddressNotZero(receiver);
vm.assume(receiver != address(0));

MarketParams memory _marketParams = _randomMarket(marketSeed);

Expand Down Expand Up @@ -305,7 +305,7 @@ contract MorphoInvariantTest is InvariantTest {
uint256 onBehalfSeed,
address receiver
) external {
receiver = _boundAddressNotZero(receiver);
vm.assume(receiver != address(0));

MarketParams memory _marketParams = _randomMarket(marketSeed);

Expand Down

0 comments on commit 8f4c620

Please sign in to comment.