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