-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |