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

chore: simplify assert/require shrink test #8634

Merged
merged 1 commit into from
Aug 9, 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
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");
}
}
Loading