assume
Signature
function assume(bool) external;
Description
If the boolean expression evaluates to false, the fuzzer will discard the current fuzz inputs and start a new fuzz run.
The assume
cheatcode should mainly be used for very narrow checks.
Broad checks will slow down tests as it will take a while to find valid values, and the test may fail if you hit the max number of rejects.
You can configure the rejection thresholds by setting fuzz.max_test_rejects
in your foundry.toml
file.
For broad checks, such as ensuring a uint256
falls within a certain range, you can bound your input with the modulo operator or Forge Standard’s bound
method.
More information on filtering via assume
can be found here.
Examples
// Good example of using assume
function testSomething(uint256 a) public {
vm.assume(a != 1);
require(a != 1);
// [PASS]
}
// In this case assume is not a great fit, so you should bound inputs manually
function testSomethingElse(uint256 a) public {
a = bound(a, 100, 1e36);
require(a >= 100 && a <= 1e36);
// [PASS]
}
SEE ALSO
Forge Standard Library