@@ -5,41 +5,18 @@ import "ds-test/test.sol";
55import "forge-std/Vm.sol " ;
66import "forge-std/console.sol " ;
77
8- import "@contracts/HeapOrdering .sol " ;
8+ import "./helpers/ConcreteHeapOrdering .sol " ;
99
10- contract Heap {
10+ contract Heap is ConcreteHeapOrdering {
1111 using HeapOrdering for HeapOrdering.HeapArray;
1212
1313 uint256 public MAX_SORTED_USERS = 16 ;
14- HeapOrdering.HeapArray public heap;
1514
1615 /// Functions to fuzz ///
1716
1817 function update (address _id , uint96 _newValue ) public {
1918 heap.update (_id, heap.getValueOf (_id), _newValue, MAX_SORTED_USERS);
2019 }
21-
22- /// Helpers ///
23-
24- function length () public view returns (uint256 ) {
25- return heap.length ();
26- }
27-
28- function size () public view returns (uint256 ) {
29- return heap.size;
30- }
31-
32- function accountValue (uint256 i ) public view returns (uint256 ) {
33- return heap.accounts[i].value;
34- }
35-
36- function accountId (uint256 i ) public view returns (address ) {
37- return heap.accounts[i].id;
38- }
39-
40- function indexOf (address id ) public view returns (uint256 ) {
41- return heap.indexOf[id];
42- }
4320}
4421
4522contract TestHeapInvariant is DSTest {
@@ -49,18 +26,32 @@ contract TestHeapInvariant is DSTest {
4926 heap = new Heap ();
5027 }
5128
29+ struct FuzzSelector {
30+ address addr;
31+ bytes4 [] selectors;
32+ }
33+
34+ // Target specific selectors for invariant testing
35+ function targetSelectors () public view returns (FuzzSelector[] memory ) {
36+ FuzzSelector[] memory targets = new FuzzSelector [](1 );
37+ bytes4 [] memory selectors = new bytes4 [](1 );
38+ selectors[0 ] = Heap.update.selector ;
39+ targets[0 ] = FuzzSelector (address (heap), selectors);
40+ return targets;
41+ }
42+
5243 // Rule:
5344 // For all i in [[0, size]],
5445 // value[i] >= value[2i + 1] and value[i] >= value[2i + 2]
5546 function invariantHeap () public {
5647 uint256 length = heap.length ();
5748
58- for (uint256 i = 1 ; i < length; ++ i) {
49+ for (uint256 i; i < length; ++ i) {
5950 assertTrue (
60- (i * 2 + 1 >= length || i * 2 + 1 >= heap.size () || heap.accountValue (i) >= heap.accountValue (i * 2 + 1 ))
51+ (i * 2 + 1 >= length || i * 2 + 1 >= heap.size () || heap.accountsValue (i) >= heap.accountsValue (i * 2 + 1 ))
6152 ); // prettier-ignore
6253 assertTrue (
63- (i * 2 + 2 >= length || i * 2 + 2 >= heap.size () || heap.accountValue (i) >= heap.accountValue (i * 2 + 2 ))
54+ (i * 2 + 2 >= length || i * 2 + 2 >= heap.size () || heap.accountsValue (i) >= heap.accountsValue (i * 2 + 2 ))
6455 ); // prettier-ignore
6556 }
6657 }
@@ -70,8 +61,8 @@ contract TestHeapInvariant is DSTest {
7061 function invariantIndexOf () public {
7162 uint256 length = heap.length ();
7263
73- for (uint256 i = 1 ; i < length; ++ i) {
74- assertTrue (heap.indexOf (heap.accountId (i)) == i);
64+ for (uint256 i; i < length; ++ i) {
65+ assertTrue (heap.indexes (heap.accountsId (i)) == i);
7566 }
7667 }
7768
0 commit comments