Skip to content

Commit

Permalink
test: add tests for Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Mar 18, 2024
1 parent c29ca6a commit f0bf9b4
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/unit/libraries/Buffer.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "forge-std/Test.sol";

import { Buffer } from "src/libraries/Buffer.sol";

contract BufferTest is Test {
using Buffer for uint16;

function testPrev_AtLimit() external {
// arrange
uint16 lIndex = 0;

// act & assert
assertEq(lIndex.prev(), Buffer.SIZE - 1);
}

function testNext_AtLimit() external {
// arrange
uint16 lLimit = Buffer.SIZE - 1;

// act & assert
assertEq(lLimit.next(), 0);
}

function testNext_GreaterThanBufferSize(uint16 aStartingIndex) external {
// assume
uint16 lStartingIndex = uint16(bound(aStartingIndex, Buffer.SIZE, type(uint16).max));

// act & assert
unchecked {
assertEq(lStartingIndex.next(), (lStartingIndex + 1) % Buffer.SIZE);
}
assertLt(lStartingIndex.next(), Buffer.SIZE); // index returned always within bounds of Buffer.SIZE
}

function testAdd_IndexGreaterThanBufferSize(uint16 aStartingIndex, uint16 aOffset) external {
// assume
uint16 lStartingIndex = uint16(bound(aStartingIndex, Buffer.SIZE, type(uint16).max));

// act
uint16 lResult = lStartingIndex.add(aOffset);

// assert
assertLt(lResult, Buffer.SIZE);
}

function testSub_IndexGreaterThanBufferSize(uint16 aStartingIndex, uint16 aOffset) external {
// assume
uint16 lStartingIndex = uint16(bound(aStartingIndex, Buffer.SIZE, type(uint16).max));

// act
uint16 lResult = lStartingIndex.sub(aOffset);

// assert
assertLt(lResult, Buffer.SIZE);
}
}

0 comments on commit f0bf9b4

Please sign in to comment.