From b07b66d5578959f4b7ff0fe225df2e9b7bff1198 Mon Sep 17 00:00:00 2001 From: grandizzy Date: Fri, 9 Aug 2024 12:18:40 +0300 Subject: [PATCH] chore: simplify assert/require shrink test --- crates/forge/tests/it/invariant.rs | 2 + .../common/InvariantShrinkWithAssert.t.sol | 67 +------------------ 2 files changed, 4 insertions(+), 65 deletions(-) diff --git a/crates/forge/tests/it/invariant.rs b/crates/forge/tests/it/invariant.rs index e4bf14ecdc73..bd9286713b1d 100644 --- a/crates/forge/tests/it/invariant.rs +++ b/crates/forge/tests/it/invariant.rs @@ -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."), diff --git a/testdata/default/fuzz/invariant/common/InvariantShrinkWithAssert.t.sol b/testdata/default/fuzz/invariant/common/InvariantShrinkWithAssert.t.sol index 34d11ccb3ab3..fa4a6e945804 100644 --- a/testdata/default/fuzz/invariant/common/InvariantShrinkWithAssert.t.sol +++ b/testdata/default/fuzz/invariant/common/InvariantShrinkWithAssert.t.sol @@ -2,20 +2,10 @@ 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++; } @@ -23,73 +13,20 @@ contract Counter { 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"); } }