Move the Checkpoints library to utils/structs (#4275)
This commit is contained in:
224
test/utils/structs/Checkpoints.t.sol
Normal file
224
test/utils/structs/Checkpoints.t.sol
Normal file
@ -0,0 +1,224 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// This file was procedurally generated from scripts/generate/templates/Checkpoints.t.js.
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "forge-std/Test.sol";
|
||||
import "../../../contracts/utils/math/SafeCast.sol";
|
||||
import "../../../contracts/utils/structs/Checkpoints.sol";
|
||||
|
||||
contract CheckpointsTrace224Test is Test {
|
||||
using Checkpoints for Checkpoints.Trace224;
|
||||
|
||||
// Maximum gap between keys used during the fuzzing tests: the `_prepareKeys` function with make sure that
|
||||
// key#n+1 is in the [key#n, key#n + _KEY_MAX_GAP] range.
|
||||
uint8 internal constant _KEY_MAX_GAP = 64;
|
||||
|
||||
Checkpoints.Trace224 internal _ckpts;
|
||||
|
||||
// helpers
|
||||
function _boundUint32(uint32 x, uint32 min, uint32 max) internal view returns (uint32) {
|
||||
return SafeCast.toUint32(bound(uint256(x), uint256(min), uint256(max)));
|
||||
}
|
||||
|
||||
function _prepareKeys(uint32[] memory keys, uint32 maxSpread) internal view {
|
||||
uint32 lastKey = 0;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
uint32 key = _boundUint32(keys[i], lastKey, lastKey + maxSpread);
|
||||
keys[i] = key;
|
||||
lastKey = key;
|
||||
}
|
||||
}
|
||||
|
||||
function _assertLatestCheckpoint(bool exist, uint32 key, uint224 value) internal {
|
||||
(bool _exist, uint32 _key, uint224 _value) = _ckpts.latestCheckpoint();
|
||||
assertEq(_exist, exist);
|
||||
assertEq(_key, key);
|
||||
assertEq(_value, value);
|
||||
}
|
||||
|
||||
// tests
|
||||
function testPush(uint32[] memory keys, uint224[] memory values, uint32 pastKey) public {
|
||||
vm.assume(values.length > 0 && values.length <= keys.length);
|
||||
_prepareKeys(keys, _KEY_MAX_GAP);
|
||||
|
||||
// initial state
|
||||
assertEq(_ckpts.length(), 0);
|
||||
assertEq(_ckpts.latest(), 0);
|
||||
_assertLatestCheckpoint(false, 0, 0);
|
||||
|
||||
uint256 duplicates = 0;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
uint32 key = keys[i];
|
||||
uint224 value = values[i % values.length];
|
||||
if (i > 0 && key == keys[i - 1]) ++duplicates;
|
||||
|
||||
// push
|
||||
_ckpts.push(key, value);
|
||||
|
||||
// check length & latest
|
||||
assertEq(_ckpts.length(), i + 1 - duplicates);
|
||||
assertEq(_ckpts.latest(), value);
|
||||
_assertLatestCheckpoint(true, key, value);
|
||||
}
|
||||
|
||||
if (keys.length > 0) {
|
||||
uint32 lastKey = keys[keys.length - 1];
|
||||
if (lastKey > 0) {
|
||||
pastKey = _boundUint32(pastKey, 0, lastKey - 1);
|
||||
|
||||
vm.expectRevert();
|
||||
this.push(pastKey, values[keys.length % values.length]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// used to test reverts
|
||||
function push(uint32 key, uint224 value) external {
|
||||
_ckpts.push(key, value);
|
||||
}
|
||||
|
||||
function testLookup(uint32[] memory keys, uint224[] memory values, uint32 lookup) public {
|
||||
vm.assume(values.length > 0 && values.length <= keys.length);
|
||||
_prepareKeys(keys, _KEY_MAX_GAP);
|
||||
|
||||
uint32 lastKey = keys.length == 0 ? 0 : keys[keys.length - 1];
|
||||
lookup = _boundUint32(lookup, 0, lastKey + _KEY_MAX_GAP);
|
||||
|
||||
uint224 upper = 0;
|
||||
uint224 lower = 0;
|
||||
uint32 lowerKey = type(uint32).max;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
uint32 key = keys[i];
|
||||
uint224 value = values[i % values.length];
|
||||
|
||||
// push
|
||||
_ckpts.push(key, value);
|
||||
|
||||
// track expected result of lookups
|
||||
if (key <= lookup) {
|
||||
upper = value;
|
||||
}
|
||||
// find the first key that is not smaller than the lookup key
|
||||
if (key >= lookup && (i == 0 || keys[i - 1] < lookup)) {
|
||||
lowerKey = key;
|
||||
}
|
||||
if (key == lowerKey) {
|
||||
lower = value;
|
||||
}
|
||||
}
|
||||
|
||||
// check lookup
|
||||
assertEq(_ckpts.lowerLookup(lookup), lower);
|
||||
assertEq(_ckpts.upperLookup(lookup), upper);
|
||||
assertEq(_ckpts.upperLookupRecent(lookup), upper);
|
||||
}
|
||||
}
|
||||
|
||||
contract CheckpointsTrace160Test is Test {
|
||||
using Checkpoints for Checkpoints.Trace160;
|
||||
|
||||
// Maximum gap between keys used during the fuzzing tests: the `_prepareKeys` function with make sure that
|
||||
// key#n+1 is in the [key#n, key#n + _KEY_MAX_GAP] range.
|
||||
uint8 internal constant _KEY_MAX_GAP = 64;
|
||||
|
||||
Checkpoints.Trace160 internal _ckpts;
|
||||
|
||||
// helpers
|
||||
function _boundUint96(uint96 x, uint96 min, uint96 max) internal view returns (uint96) {
|
||||
return SafeCast.toUint96(bound(uint256(x), uint256(min), uint256(max)));
|
||||
}
|
||||
|
||||
function _prepareKeys(uint96[] memory keys, uint96 maxSpread) internal view {
|
||||
uint96 lastKey = 0;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
uint96 key = _boundUint96(keys[i], lastKey, lastKey + maxSpread);
|
||||
keys[i] = key;
|
||||
lastKey = key;
|
||||
}
|
||||
}
|
||||
|
||||
function _assertLatestCheckpoint(bool exist, uint96 key, uint160 value) internal {
|
||||
(bool _exist, uint96 _key, uint160 _value) = _ckpts.latestCheckpoint();
|
||||
assertEq(_exist, exist);
|
||||
assertEq(_key, key);
|
||||
assertEq(_value, value);
|
||||
}
|
||||
|
||||
// tests
|
||||
function testPush(uint96[] memory keys, uint160[] memory values, uint96 pastKey) public {
|
||||
vm.assume(values.length > 0 && values.length <= keys.length);
|
||||
_prepareKeys(keys, _KEY_MAX_GAP);
|
||||
|
||||
// initial state
|
||||
assertEq(_ckpts.length(), 0);
|
||||
assertEq(_ckpts.latest(), 0);
|
||||
_assertLatestCheckpoint(false, 0, 0);
|
||||
|
||||
uint256 duplicates = 0;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
uint96 key = keys[i];
|
||||
uint160 value = values[i % values.length];
|
||||
if (i > 0 && key == keys[i - 1]) ++duplicates;
|
||||
|
||||
// push
|
||||
_ckpts.push(key, value);
|
||||
|
||||
// check length & latest
|
||||
assertEq(_ckpts.length(), i + 1 - duplicates);
|
||||
assertEq(_ckpts.latest(), value);
|
||||
_assertLatestCheckpoint(true, key, value);
|
||||
}
|
||||
|
||||
if (keys.length > 0) {
|
||||
uint96 lastKey = keys[keys.length - 1];
|
||||
if (lastKey > 0) {
|
||||
pastKey = _boundUint96(pastKey, 0, lastKey - 1);
|
||||
|
||||
vm.expectRevert();
|
||||
this.push(pastKey, values[keys.length % values.length]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// used to test reverts
|
||||
function push(uint96 key, uint160 value) external {
|
||||
_ckpts.push(key, value);
|
||||
}
|
||||
|
||||
function testLookup(uint96[] memory keys, uint160[] memory values, uint96 lookup) public {
|
||||
vm.assume(values.length > 0 && values.length <= keys.length);
|
||||
_prepareKeys(keys, _KEY_MAX_GAP);
|
||||
|
||||
uint96 lastKey = keys.length == 0 ? 0 : keys[keys.length - 1];
|
||||
lookup = _boundUint96(lookup, 0, lastKey + _KEY_MAX_GAP);
|
||||
|
||||
uint160 upper = 0;
|
||||
uint160 lower = 0;
|
||||
uint96 lowerKey = type(uint96).max;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
uint96 key = keys[i];
|
||||
uint160 value = values[i % values.length];
|
||||
|
||||
// push
|
||||
_ckpts.push(key, value);
|
||||
|
||||
// track expected result of lookups
|
||||
if (key <= lookup) {
|
||||
upper = value;
|
||||
}
|
||||
// find the first key that is not smaller than the lookup key
|
||||
if (key >= lookup && (i == 0 || keys[i - 1] < lookup)) {
|
||||
lowerKey = key;
|
||||
}
|
||||
if (key == lowerKey) {
|
||||
lower = value;
|
||||
}
|
||||
}
|
||||
|
||||
// check lookup
|
||||
assertEq(_ckpts.lowerLookup(lookup), lower);
|
||||
assertEq(_ckpts.upperLookup(lookup), upper);
|
||||
assertEq(_ckpts.upperLookupRecent(lookup), upper);
|
||||
}
|
||||
}
|
||||
137
test/utils/structs/Checkpoints.test.js
Normal file
137
test/utils/structs/Checkpoints.test.js
Normal file
@ -0,0 +1,137 @@
|
||||
const { expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const { VALUE_SIZES } = require('../../../scripts/generate/templates/Checkpoints.opts.js');
|
||||
|
||||
const $Checkpoints = artifacts.require('$Checkpoints');
|
||||
|
||||
// The library name may be 'Checkpoints' or 'CheckpointsUpgradeable'
|
||||
const libraryName = $Checkpoints._json.contractName.replace(/^\$/, '');
|
||||
|
||||
const first = array => (array.length ? array[0] : undefined);
|
||||
const last = array => (array.length ? array[array.length - 1] : undefined);
|
||||
|
||||
contract('Checkpoints', function () {
|
||||
beforeEach(async function () {
|
||||
this.mock = await $Checkpoints.new();
|
||||
});
|
||||
|
||||
for (const length of VALUE_SIZES) {
|
||||
describe(`Trace${length}`, function () {
|
||||
beforeEach(async function () {
|
||||
this.methods = {
|
||||
latest: (...args) => this.mock.methods[`$latest_${libraryName}_Trace${length}(uint256)`](0, ...args),
|
||||
latestCheckpoint: (...args) =>
|
||||
this.mock.methods[`$latestCheckpoint_${libraryName}_Trace${length}(uint256)`](0, ...args),
|
||||
length: (...args) => this.mock.methods[`$length_${libraryName}_Trace${length}(uint256)`](0, ...args),
|
||||
push: (...args) => this.mock.methods[`$push(uint256,uint${256 - length},uint${length})`](0, ...args),
|
||||
lowerLookup: (...args) => this.mock.methods[`$lowerLookup(uint256,uint${256 - length})`](0, ...args),
|
||||
upperLookup: (...args) => this.mock.methods[`$upperLookup(uint256,uint${256 - length})`](0, ...args),
|
||||
upperLookupRecent: (...args) =>
|
||||
this.mock.methods[`$upperLookupRecent(uint256,uint${256 - length})`](0, ...args),
|
||||
};
|
||||
});
|
||||
|
||||
describe('without checkpoints', function () {
|
||||
it('returns zero as latest value', async function () {
|
||||
expect(await this.methods.latest()).to.be.bignumber.equal('0');
|
||||
|
||||
const ckpt = await this.methods.latestCheckpoint();
|
||||
expect(ckpt[0]).to.be.equal(false);
|
||||
expect(ckpt[1]).to.be.bignumber.equal('0');
|
||||
expect(ckpt[2]).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('lookup returns 0', async function () {
|
||||
expect(await this.methods.lowerLookup(0)).to.be.bignumber.equal('0');
|
||||
expect(await this.methods.upperLookup(0)).to.be.bignumber.equal('0');
|
||||
expect(await this.methods.upperLookupRecent(0)).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with checkpoints', function () {
|
||||
beforeEach('pushing checkpoints', async function () {
|
||||
this.checkpoints = [
|
||||
{ key: '2', value: '17' },
|
||||
{ key: '3', value: '42' },
|
||||
{ key: '5', value: '101' },
|
||||
{ key: '7', value: '23' },
|
||||
{ key: '11', value: '99' },
|
||||
];
|
||||
for (const { key, value } of this.checkpoints) {
|
||||
await this.methods.push(key, value);
|
||||
}
|
||||
});
|
||||
|
||||
it('length', async function () {
|
||||
expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
|
||||
});
|
||||
|
||||
it('returns latest value', async function () {
|
||||
expect(await this.methods.latest()).to.be.bignumber.equal(last(this.checkpoints).value);
|
||||
|
||||
const ckpt = await this.methods.latestCheckpoint();
|
||||
expect(ckpt[0]).to.be.equal(true);
|
||||
expect(ckpt[1]).to.be.bignumber.equal(last(this.checkpoints).key);
|
||||
expect(ckpt[2]).to.be.bignumber.equal(last(this.checkpoints).value);
|
||||
});
|
||||
|
||||
it('cannot push values in the past', async function () {
|
||||
await expectRevert(this.methods.push(last(this.checkpoints).key - 1, '0'), 'Checkpoint: decreasing keys');
|
||||
});
|
||||
|
||||
it('can update last value', async function () {
|
||||
const newValue = '42';
|
||||
|
||||
// check length before the update
|
||||
expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
|
||||
|
||||
// update last key
|
||||
await this.methods.push(last(this.checkpoints).key, newValue);
|
||||
expect(await this.methods.latest()).to.be.bignumber.equal(newValue);
|
||||
|
||||
// check that length did not change
|
||||
expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
|
||||
});
|
||||
|
||||
it('lower lookup', async function () {
|
||||
for (let i = 0; i < 14; ++i) {
|
||||
const value = first(this.checkpoints.filter(x => i <= x.key))?.value || '0';
|
||||
|
||||
expect(await this.methods.lowerLookup(i)).to.be.bignumber.equal(value);
|
||||
}
|
||||
});
|
||||
|
||||
it('upper lookup & upperLookupRecent', async function () {
|
||||
for (let i = 0; i < 14; ++i) {
|
||||
const value = last(this.checkpoints.filter(x => i >= x.key))?.value || '0';
|
||||
|
||||
expect(await this.methods.upperLookup(i)).to.be.bignumber.equal(value);
|
||||
expect(await this.methods.upperLookupRecent(i)).to.be.bignumber.equal(value);
|
||||
}
|
||||
});
|
||||
|
||||
it('upperLookupRecent with more than 5 checkpoints', async function () {
|
||||
const moreCheckpoints = [
|
||||
{ key: '12', value: '22' },
|
||||
{ key: '13', value: '131' },
|
||||
{ key: '17', value: '45' },
|
||||
{ key: '19', value: '31452' },
|
||||
{ key: '21', value: '0' },
|
||||
];
|
||||
const allCheckpoints = [].concat(this.checkpoints, moreCheckpoints);
|
||||
|
||||
for (const { key, value } of moreCheckpoints) {
|
||||
await this.methods.push(key, value);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 25; ++i) {
|
||||
const value = last(allCheckpoints.filter(x => i >= x.key))?.value || '0';
|
||||
expect(await this.methods.upperLookup(i)).to.be.bignumber.equal(value);
|
||||
expect(await this.methods.upperLookupRecent(i)).to.be.bignumber.equal(value);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user