Disallow empty CircularBuffer setup (#5214)
This commit is contained in:
@ -36,6 +36,11 @@ import {Panic} from "../Panic.sol";
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
library CircularBuffer {
|
library CircularBuffer {
|
||||||
|
/**
|
||||||
|
* @dev Error emitted when trying to setup a buffer with a size of 0.
|
||||||
|
*/
|
||||||
|
error InvalidBufferSize();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Counts the number of items that have been pushed to the buffer. The residuo modulo _data.length indicates
|
* @dev Counts the number of items that have been pushed to the buffer. The residuo modulo _data.length indicates
|
||||||
* where the next value should be stored.
|
* where the next value should be stored.
|
||||||
@ -61,6 +66,7 @@ library CircularBuffer {
|
|||||||
* Consider a large buffer size may render the function unusable.
|
* Consider a large buffer size may render the function unusable.
|
||||||
*/
|
*/
|
||||||
function setup(Bytes32CircularBuffer storage self, uint256 size) internal {
|
function setup(Bytes32CircularBuffer storage self, uint256 size) internal {
|
||||||
|
if (size == 0) revert InvalidBufferSize();
|
||||||
clear(self);
|
clear(self);
|
||||||
Arrays.unsafeSetLength(self._data, size);
|
Arrays.unsafeSetLength(self._data, size);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,10 @@ describe('CircularBuffer', function () {
|
|||||||
Object.assign(this, await loadFixture(fixture));
|
Object.assign(this, await loadFixture(fixture));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('reverts on invalid setup', async function () {
|
||||||
|
await expect(this.mock.$setup(0, 0)).to.be.revertedWithCustomError(this.mock, 'InvalidBufferSize');
|
||||||
|
});
|
||||||
|
|
||||||
it('starts empty', async function () {
|
it('starts empty', async function () {
|
||||||
expect(await this.mock.$count(0)).to.equal(0n);
|
expect(await this.mock.$count(0)).to.equal(0n);
|
||||||
expect(await this.mock.$length(0)).to.equal(LENGTH);
|
expect(await this.mock.$length(0)).to.equal(LENGTH);
|
||||||
|
|||||||
Reference in New Issue
Block a user