Skip to content

Commit 7ae00a9

Browse files
committed
test: use SymTest within FixedSizeMemoryStack
1 parent 516f1ec commit 7ae00a9

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

contracts/utils/FixedSizeMemoryStack.sol

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ library FixedSizeMemoryStack {
4949
uint256 t = stack._top;
5050
if (t >= stack._data.length) revert StackOverflow(stack._data.length);
5151
stack._data[t] = value;
52-
unchecked {
53-
stack._top = t + 1;
54-
}
52+
stack._top = t + 1;
5553
}
5654

5755
/// @notice Pop the top value from the stack.
@@ -60,9 +58,7 @@ library FixedSizeMemoryStack {
6058
function pop(Stack memory stack) internal pure returns (bytes32 value) {
6159
uint256 t = stack._top;
6260
if (t == 0) revert StackUnderflow();
63-
unchecked {
64-
t -= 1;
65-
}
61+
t -= 1;
6662
value = stack._data[t];
6763
stack._top = t;
6864
}

test/utils/FixedSizeMemoryStack.t.sol

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,24 @@ contract FixedSizeMemoryStackTest is Test, SymTest {
3636
}
3737

3838
/// forge-config: default.allow_internal_expect_revert = true
39-
function testPeek() public {
39+
function testPeekStackUnderflow() public {
4040
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
4141

4242
vm.expectRevert(abi.encodeWithSelector(FixedSizeMemoryStack.StackUnderflow.selector));
4343
stack.peek();
4444
assertEq(stack._top, 0);
45+
}
4546

46-
stack.push(bytes32(uint256(1)));
47-
stack.push(bytes32(uint256(2)));
47+
function testPeek() public {
48+
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
49+
50+
bytes32 item1 = svm.createBytes32("item1");
51+
bytes32 item2 = svm.createBytes32("item2");
4852

49-
assertEq(stack.peek(), bytes32(uint256(2)));
53+
stack.push(item1);
54+
stack.push(item2);
55+
56+
assertEq(stack.peek(), item2);
5057
assertEq(stack._top, 2);
5158
}
5259

@@ -55,10 +62,13 @@ contract FixedSizeMemoryStackTest is Test, SymTest {
5562

5663
assertEq(stack.size(), 0);
5764

58-
stack.push(bytes32(uint256(1)));
65+
bytes32 item1 = svm.createBytes32("item1");
66+
bytes32 item2 = svm.createBytes32("item2");
67+
68+
stack.push(item1);
5969
assertEq(stack.size(), 1);
6070

61-
stack.push(bytes32(uint256(2)));
71+
stack.push(item2);
6272
assertEq(stack.size(), 2);
6373

6474
stack.pop();
@@ -79,7 +89,9 @@ contract FixedSizeMemoryStackTest is Test, SymTest {
7989

8090
assertEq(stack.isEmpty(), true);
8191

82-
stack.push(bytes32(uint256(1)));
92+
bytes32 item1 = svm.createBytes32("item1");
93+
94+
stack.push(item1);
8395
assertEq(stack.isEmpty(), false);
8496

8597
stack.pop();
@@ -91,11 +103,14 @@ contract FixedSizeMemoryStackTest is Test, SymTest {
91103

92104
assertEq(stack.isFull(), false);
93105

94-
stack.push(bytes32(uint256(1)));
106+
bytes32 item1 = svm.createBytes32("item1");
107+
108+
stack.push(item1);
95109
assertEq(stack.isFull(), false);
96110

97111
for (uint256 i = 0; i < 9; i++) {
98-
stack.push(bytes32(uint256(i)));
112+
bytes32 item = svm.createBytes32(string(abi.encodePacked("item", i)));
113+
stack.push(item);
99114
}
100115

101116
assertEq(stack.isFull(), true);
@@ -107,8 +122,11 @@ contract FixedSizeMemoryStackTest is Test, SymTest {
107122
function testClear() public {
108123
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
109124

110-
stack.push(bytes32(uint256(1)));
111-
stack.push(bytes32(uint256(2)));
125+
bytes32 item1 = svm.createBytes32("item1");
126+
bytes32 item2 = svm.createBytes32("item2");
127+
128+
stack.push(item1);
129+
stack.push(item2);
112130

113131
stack.clear();
114132

0 commit comments

Comments
 (0)