Skip to content

Commit

Permalink
chore: simplify assert/require shrink test (#8634)
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy authored Aug 9, 2024
1 parent 1197fbe commit f6c6b35
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 65 deletions.
2 changes: 2 additions & 0 deletions crates/forge/tests/it/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ async fn check_shrink_sequence(test_pattern: &str, expected_len: usize) {
Filter::new(test_pattern, ".*", ".*fuzz/invariant/common/InvariantShrinkWithAssert.t.sol");
let mut runner = TEST_DATA_DEFAULT.runner();
runner.test_options.fuzz.seed = Some(U256::from(100u32));
runner.test_options.invariant.runs = 1;
runner.test_options.invariant.depth = 15;

match get_counterexample!(runner, &filter) {
CounterExample::Single(_) => panic!("CounterExample should be a sequence."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,31 @@
pragma solidity ^0.8.13;

import "ds-test/test.sol";
import "cheats/Vm.sol";

struct FuzzSelector {
address addr;
bytes4[] selectors;
}

contract Counter {
uint256 public number;

function setNumber(uint256 newNumber) public {
number = newNumber;
}

function increment() public {
number++;
}

function decrement() public {
number--;
}

function double() public {
number *= 2;
}

function half() public {
number /= 2;
}

function triple() public {
number *= 3;
}

function third() public {
number /= 3;
}

function quadruple() public {
number *= 4;
}

function quarter() public {
number /= 4;
}
}

contract Handler is DSTest {
Counter public counter;

constructor(Counter _counter) {
counter = _counter;
counter.setNumber(0);
}

function increment() public {
counter.increment();
}

function setNumber(uint256 x) public {
counter.setNumber(x);
}
}

contract InvariantShrinkWithAssert is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
Counter public counter;
Handler handler;

function setUp() public {
counter = new Counter();
handler = new Handler(counter);
}

function targetSelectors() public returns (FuzzSelector[] memory) {
FuzzSelector[] memory targets = new FuzzSelector[](1);
bytes4[] memory selectors = new bytes4[](2);
selectors[0] = handler.increment.selector;
selectors[1] = handler.setNumber.selector;
targets[0] = FuzzSelector(address(handler), selectors);
return targets;
}

function invariant_with_assert() public {
assertTrue(counter.number() != 3, "wrong counter");
assertTrue(counter.number() < 2, "wrong counter");
}

function invariant_with_require() public {
require(counter.number() != 3, "wrong counter");
require(counter.number() < 2, "wrong counter");
}
}

0 comments on commit f6c6b35

Please sign in to comment.