Skip to content

Commit

Permalink
adds tests for priority queue
Browse files Browse the repository at this point in the history
  • Loading branch information
r0wdy1 committed Dec 1, 2023
1 parent effa3ee commit 799b765
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/zkbob/utils/PriorityQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 83 additions & 0 deletions test/libraries/PriorityQueue.t.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 799b765

Please sign in to comment.