-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy pathFixedSizeMemoryStack.t.sol
More file actions
124 lines (88 loc) · 3.48 KB
/
FixedSizeMemoryStack.t.sol
File metadata and controls
124 lines (88 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Test} from "forge-std/Test.sol";
import {SymTest} from "halmos-cheatcodes/SymTest.sol";
import {FixedSizeMemoryStack} from "../../contracts/utils/FixedSizeMemoryStack.sol";
contract FixedSizeMemoryStackTest is Test, SymTest {
using FixedSizeMemoryStack for FixedSizeMemoryStack.Stack;
function testFuzzPush(bytes32 item) public pure {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
stack.push(item);
assertEq(stack._top, 1);
assertEq(stack._data[0], item);
}
/// forge-config: default.allow_internal_expect_revert = true
function testPop() public {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
vm.expectRevert((FixedSizeMemoryStack.StackUnderflow.selector));
stack.pop();
bytes32 item1 = svm.createBytes32("item1");
bytes32 item2 = svm.createBytes32("item2");
stack.push(item1);
stack.push(item2);
assertEq(stack.pop(), item2);
assertEq(stack._top, 1);
assertEq(stack.pop(), item1);
assertEq(stack._top, 0);
}
/// forge-config: default.allow_internal_expect_revert = true
function testPeekStackUnderflow() public {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
vm.expectRevert(abi.encodeWithSelector(FixedSizeMemoryStack.StackUnderflow.selector));
stack.peek();
assertEq(stack._top, 0);
}
function testPeek() public {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
stack.push(bytes32(uint256(1)));
stack.push(bytes32(uint256(2)));
assertEq(stack.peek(), bytes32(uint256(2)));
assertEq(stack._top, 2);
}
function testSize() public {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
assertEq(stack.size(), 0);
stack.push(bytes32(uint256(1)));
assertEq(stack.size(), 1);
stack.push(bytes32(uint256(2)));
assertEq(stack.size(), 2);
stack.pop();
assertEq(stack.size(), 1);
stack.pop();
assertEq(stack.size(), 0);
}
function testCapacity() public {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
assertEq(stack.capacity(), 10);
}
function testIsEmpty() public {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
assertEq(stack.isEmpty(), true);
stack.push(bytes32(uint256(1)));
assertEq(stack.isEmpty(), false);
stack.pop();
assertEq(stack.isEmpty(), true);
}
function testIsFull() public {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
assertEq(stack.isFull(), false);
stack.push(bytes32(uint256(1)));
assertEq(stack.isFull(), false);
for (uint256 i = 0; i < 9; i++) {
stack.push(bytes32(uint256(i)));
}
assertEq(stack.isFull(), true);
stack.pop();
assertEq(stack.isFull(), false);
}
function testClear() public {
FixedSizeMemoryStack.Stack memory stack = FixedSizeMemoryStack.init(10);
stack.push(bytes32(uint256(1)));
stack.push(bytes32(uint256(2)));
stack.clear();
assertEq(stack.size(), 0);
assertEq(stack.isEmpty(), true);
assertEq(stack.isFull(), false);
assertEq(stack._top, 0);
}
}