-
Notifications
You must be signed in to change notification settings - Fork 13
Heap invariant testing #68
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
44daa58
test: heap invariant
MathisGD ac368cb
test: refactor heap invariant testing
MathisGD 42d4a29
test: use ConcreteHeap contracts
MathisGD 3925c02
test: first invariants 3-heap
MathisGD 017aa9d
test: refactor heap testing
MathisGD 6d1cbdc
test: improve heap invariant testing
MathisGD ef0d60a
test: reduce the depth for invariant testing
MathisGD f38e75f
test: modernize test dependancies
MathisGD 6e30ff0
chore: rename test files
MathisGD e3409ac
chore: remove old random test files
MathisGD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,7 @@ | |
| src = "contracts" | ||
| test = "test-foundry" | ||
| solc_version = "0.8.17" | ||
|
|
||
| [invariant] | ||
| runs = 256 | ||
| depth = 64 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // SPDX-License-Identifier: GNU AGPLv3 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import "forge-std/Test.sol"; | ||
| import "forge-std/console.sol"; | ||
|
|
||
| import "./helpers/ConcreteHeapOrdering.sol"; | ||
|
|
||
| contract Heap is ConcreteHeapOrdering { | ||
| using HeapOrdering for HeapOrdering.HeapArray; | ||
|
|
||
| uint256 public MAX_SORTED_USERS = 16; | ||
|
|
||
| /// Functions to fuzz /// | ||
|
|
||
| function updateCorrect(address _id, uint96 _newValue) public { | ||
| uint256 oldValue = heap.getValueOf(_id); | ||
| if (oldValue != 0 || _newValue != 0) | ||
| heap.update(_id, heap.getValueOf(_id), _newValue, MAX_SORTED_USERS); | ||
| } | ||
| } | ||
|
|
||
| contract TestHeapInvariant is Test { | ||
| Heap public heap; | ||
|
|
||
| function setUp() public { | ||
| heap = new Heap(); | ||
| } | ||
|
|
||
| struct FuzzSelector { | ||
| address addr; | ||
| bytes4[] selectors; | ||
| } | ||
|
|
||
| // Target specific selectors for invariant testing | ||
| function targetSelectors() public view returns (FuzzSelector[] memory) { | ||
| FuzzSelector[] memory targets = new FuzzSelector[](1); | ||
| bytes4[] memory selectors = new bytes4[](1); | ||
| selectors[0] = Heap.updateCorrect.selector; | ||
| targets[0] = FuzzSelector(address(heap), selectors); | ||
| return targets; | ||
| } | ||
|
|
||
| // Rule: | ||
| // For all i in [[0, size]], | ||
| // value[i] >= value[2i + 1] and value[i] >= value[2i + 2] | ||
| function invariantHeap() public { | ||
| uint256 length = heap.length(); | ||
|
|
||
| for (uint256 i; i < length; ++i) { | ||
| assertTrue((i * 2 + 1 >= length || i * 2 + 1 >= heap.size() || heap.accountsValue(i) >= heap.accountsValue(i * 2 + 1))); // prettier-ignore | ||
| assertTrue((i * 2 + 2 >= length || i * 2 + 2 >= heap.size() || heap.accountsValue(i) >= heap.accountsValue(i * 2 + 2))); // prettier-ignore | ||
| } | ||
| } | ||
MathisGD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Rule: | ||
| // For all i in [[0, length]], indexOf(account.id[i]) == i | ||
| function invariantIndexOf() public { | ||
| uint256 length = heap.length(); | ||
|
|
||
| for (uint256 i; i < length; ++i) { | ||
| assertTrue(heap.indexOf(heap.accountsId(i)) == i); | ||
| } | ||
| } | ||
|
|
||
| // Rule: | ||
| // size <= 2 * MAX_SORTED_USERS | ||
| function invariantSize() public { | ||
| assertTrue(heap.size() <= 2 * heap.MAX_SORTED_USERS()); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // SPDX-License-Identifier: GNU AGPLv3 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import "forge-std/Test.sol"; | ||
| import "forge-std/console.sol"; | ||
|
|
||
| import "./helpers/ConcreteThreeHeapOrdering.sol"; | ||
|
|
||
| contract Heap is ConcreteThreeHeapOrdering { | ||
| using ThreeHeapOrdering for ThreeHeapOrdering.HeapArray; | ||
|
|
||
| uint256 public MAX_SORTED_USERS = 16; | ||
|
|
||
| /// Functions to fuzz /// | ||
|
|
||
| function updateCorrect(address _id, uint96 _newValue) public { | ||
| uint256 oldValue = heap.getValueOf(_id); | ||
| if (oldValue != 0 || _newValue != 0) | ||
| heap.update(_id, heap.getValueOf(_id), _newValue, MAX_SORTED_USERS); | ||
| } | ||
| } | ||
|
|
||
| contract TestHeapInvariant is Test { | ||
| Heap public heap; | ||
|
|
||
| function setUp() public { | ||
| heap = new Heap(); | ||
| } | ||
|
|
||
| struct FuzzSelector { | ||
| address addr; | ||
| bytes4[] selectors; | ||
| } | ||
|
|
||
| // Target specific selectors for invariant testing | ||
| function targetSelectors() public view returns (FuzzSelector[] memory) { | ||
| FuzzSelector[] memory targets = new FuzzSelector[](1); | ||
| bytes4[] memory selectors = new bytes4[](1); | ||
| selectors[0] = Heap.updateCorrect.selector; | ||
| targets[0] = FuzzSelector(address(heap), selectors); | ||
| return targets; | ||
| } | ||
|
|
||
| // Rule: | ||
| // For all i in [[0, size]], | ||
| // value[i] >= value[3i + 1] and value[i] >= value[3i + 2] and value[i] >= value[3i + 3] | ||
| function invariantHeap() public { | ||
| uint256 length = heap.length(); | ||
|
|
||
| for (uint256 i; i < length; ++i) { | ||
| assertTrue((i * 3 + 1 >= length || i * 3 + 1 >= heap.size() || heap.accountsValue(i) >= heap.accountsValue(i * 3 + 1))); // prettier-ignore | ||
| assertTrue((i * 3 + 2 >= length || i * 3 + 2 >= heap.size() || heap.accountsValue(i) >= heap.accountsValue(i * 3 + 2))); // prettier-ignore | ||
| assertTrue((i * 3 + 3 >= length || i * 3 + 3 >= heap.size() || heap.accountsValue(i) >= heap.accountsValue(i * 3 + 3))); // prettier-ignore | ||
| } | ||
| } | ||
|
|
||
| // Rule: | ||
| // For all i in [[0, length]], indexOf(account.id[i]) == i | ||
| function invariantIndexOf() public { | ||
| uint256 length = heap.length(); | ||
|
|
||
| for (uint256 i; i < length; ++i) { | ||
| assertTrue(heap.indexOf(heap.accountsId(i)) == i); | ||
| } | ||
| } | ||
|
|
||
| // Rule: | ||
| // size <= 3 * MAX_SORTED_USERS | ||
| function invariantSize() public { | ||
| assertTrue(heap.size() <= 3 * heap.MAX_SORTED_USERS()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.