From 799b765ee6df29554e60a4f796c9bd998047196d Mon Sep 17 00:00:00 2001 From: vladimir Date: Fri, 1 Dec 2023 11:13:00 +0400 Subject: [PATCH] adds tests for priority queue --- src/zkbob/utils/PriorityQueue.sol | 8 +++ test/libraries/PriorityQueue.t.sol | 83 ++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 test/libraries/PriorityQueue.t.sol diff --git a/src/zkbob/utils/PriorityQueue.sol b/src/zkbob/utils/PriorityQueue.sol index 8d26c1e..1935731 100644 --- a/src/zkbob/utils/PriorityQueue.sol +++ b/src/zkbob/utils/PriorityQueue.sol @@ -61,6 +61,14 @@ library PriorityQueue { _queue.tail = tail + 1; } + function list(Queue storage _queue) external view returns ( PriorityOperation[] memory) { + PriorityOperation[] memory result = new PriorityOperation[] (_queue.getSize()); + for (uint256 index = _queue.head; index < _queue.tail; index++) { + result[index-_queue.head] = _queue.data[index]; + } + return result; + } + /// @return The first unprocessed priority operation from the queue function front(Queue storage _queue) internal view returns (PriorityOperation memory) { require(!_queue.isEmpty(), "D"); // priority queue is empty diff --git a/test/libraries/PriorityQueue.t.sol b/test/libraries/PriorityQueue.t.sol new file mode 100644 index 0000000..6f3076c --- /dev/null +++ b/test/libraries/PriorityQueue.t.sol @@ -0,0 +1,83 @@ +pragma solidity ^0.8.15; +import "forge-std/Test.sol"; +import "../../src/zkbob/utils/PriorityQueue.sol"; +import "forge-std/console.sol"; + +contract DummyQueue { + using PriorityQueue for PriorityQueue.Queue; + + PriorityQueue.Queue _queue; + address immutable prover1 = address(bytes20(new bytes(20))); + + function list() external view returns (PriorityOperation[] memory) { + return _queue.list(); + } + + function pushBack(PriorityOperation memory _operation) external { + _queue.pushBack(_operation); + } + + function head() external view returns (uint256) { + return _queue.head; + } + + function tail() external view returns (uint256) { + return _queue.tail; + } + + function popFront() external returns (PriorityOperation memory priorityOperation) { + return _queue.popFront(); + } +} + +contract PriorityQueueTest is Test { + using PriorityQueue for PriorityQueue.Queue; + + address immutable prover1 = makeAddr("Prover #1"); + DummyQueue _queueContract; + + function setUp() external { + _queueContract = new DummyQueue(); + } + + function newOp( + uint256 id + ) external view returns (PriorityOperation memory) { + return PriorityOperation(id, prover1, uint64(0), uint64(0)); + } + + function testEmptyQueue() external { + PriorityOperation[] memory ops = _queueContract.list(); + assertEq(0, ops.length); + } + + function testPushBack() external { + _queueContract.pushBack(this.newOp(0)); + + assertEq(0, _queueContract.head()); + assertEq(1, _queueContract.tail()); + + assertEq(1, _queueContract.list().length); + + _queueContract.pushBack(this.newOp(2)); + + assertEq(2, _queueContract.list().length); + } + function testPopFront() external { + _queueContract.pushBack(this.newOp(0)); + _queueContract.pushBack(this.newOp(1)); + _queueContract.pushBack(this.newOp(2)); + + assertEq(0, _queueContract.head()); + assertEq(3, _queueContract.tail()); + + PriorityOperation memory first = _queueContract.popFront(); + + assertEq(first.commitment, uint256(0)); + + assertEq(1, _queueContract.head()); + assertEq(3, _queueContract.tail()); + + assertEq(2, _queueContract.list().length); + } +}