Migrate utils-structs tests to ethersjs (#4748)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
@ -1,15 +1,19 @@
|
||||
const { BN } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const { ethers } = require('hardhat');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const BitMap = artifacts.require('$BitMaps');
|
||||
async function fixture() {
|
||||
const bitmap = await ethers.deployContract('$BitMaps');
|
||||
return { bitmap };
|
||||
}
|
||||
|
||||
contract('BitMap', function () {
|
||||
const keyA = new BN('7891');
|
||||
const keyB = new BN('451');
|
||||
const keyC = new BN('9592328');
|
||||
describe('BitMap', function () {
|
||||
const keyA = 7891n;
|
||||
const keyB = 451n;
|
||||
const keyC = 9592328n;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.bitmap = await BitMap.new();
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('starts empty', async function () {
|
||||
@ -35,18 +39,18 @@ contract('BitMap', function () {
|
||||
});
|
||||
|
||||
it('set several consecutive keys', async function () {
|
||||
await this.bitmap.$setTo(0, keyA.addn(0), true);
|
||||
await this.bitmap.$setTo(0, keyA.addn(1), true);
|
||||
await this.bitmap.$setTo(0, keyA.addn(2), true);
|
||||
await this.bitmap.$setTo(0, keyA.addn(3), true);
|
||||
await this.bitmap.$setTo(0, keyA.addn(4), true);
|
||||
await this.bitmap.$setTo(0, keyA.addn(2), false);
|
||||
await this.bitmap.$setTo(0, keyA.addn(4), false);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(0))).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(1))).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(2))).to.equal(false);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(3))).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(4))).to.equal(false);
|
||||
await this.bitmap.$setTo(0, keyA + 0n, true);
|
||||
await this.bitmap.$setTo(0, keyA + 1n, true);
|
||||
await this.bitmap.$setTo(0, keyA + 2n, true);
|
||||
await this.bitmap.$setTo(0, keyA + 3n, true);
|
||||
await this.bitmap.$setTo(0, keyA + 4n, true);
|
||||
await this.bitmap.$setTo(0, keyA + 2n, false);
|
||||
await this.bitmap.$setTo(0, keyA + 4n, false);
|
||||
expect(await this.bitmap.$get(0, keyA + 0n)).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA + 1n)).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA + 2n)).to.equal(false);
|
||||
expect(await this.bitmap.$get(0, keyA + 3n)).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA + 4n)).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
@ -67,14 +71,14 @@ contract('BitMap', function () {
|
||||
});
|
||||
|
||||
it('adds several consecutive keys', async function () {
|
||||
await this.bitmap.$set(0, keyA.addn(0));
|
||||
await this.bitmap.$set(0, keyA.addn(1));
|
||||
await this.bitmap.$set(0, keyA.addn(3));
|
||||
expect(await this.bitmap.$get(0, keyA.addn(0))).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(1))).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(2))).to.equal(false);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(3))).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(4))).to.equal(false);
|
||||
await this.bitmap.$set(0, keyA + 0n);
|
||||
await this.bitmap.$set(0, keyA + 1n);
|
||||
await this.bitmap.$set(0, keyA + 3n);
|
||||
expect(await this.bitmap.$get(0, keyA + 0n)).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA + 1n)).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA + 2n)).to.equal(false);
|
||||
expect(await this.bitmap.$get(0, keyA + 3n)).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA + 4n)).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
@ -89,15 +93,15 @@ contract('BitMap', function () {
|
||||
});
|
||||
|
||||
it('removes consecutive added keys', async function () {
|
||||
await this.bitmap.$set(0, keyA.addn(0));
|
||||
await this.bitmap.$set(0, keyA.addn(1));
|
||||
await this.bitmap.$set(0, keyA.addn(3));
|
||||
await this.bitmap.$unset(0, keyA.addn(1));
|
||||
expect(await this.bitmap.$get(0, keyA.addn(0))).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(1))).to.equal(false);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(2))).to.equal(false);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(3))).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA.addn(4))).to.equal(false);
|
||||
await this.bitmap.$set(0, keyA + 0n);
|
||||
await this.bitmap.$set(0, keyA + 1n);
|
||||
await this.bitmap.$set(0, keyA + 3n);
|
||||
await this.bitmap.$unset(0, keyA + 1n);
|
||||
expect(await this.bitmap.$get(0, keyA + 0n)).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA + 1n)).to.equal(false);
|
||||
expect(await this.bitmap.$get(0, keyA + 2n)).to.equal(false);
|
||||
expect(await this.bitmap.$get(0, keyA + 3n)).to.equal(true);
|
||||
expect(await this.bitmap.$get(0, keyA + 4n)).to.equal(false);
|
||||
});
|
||||
|
||||
it('adds and removes multiple keys', async function () {
|
||||
|
||||
@ -1,71 +1,65 @@
|
||||
require('@openzeppelin/test-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
const { ethers } = require('hardhat');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { VALUE_SIZES } = require('../../../scripts/generate/templates/Checkpoints.opts.js');
|
||||
const { expectRevertCustomError } = require('../../helpers/customError.js');
|
||||
const { expectRevert } = require('@openzeppelin/test-helpers');
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
describe('Checkpoints', function () {
|
||||
for (const length of VALUE_SIZES) {
|
||||
describe(`Trace${length}`, function () {
|
||||
beforeEach(async function () {
|
||||
this.methods = {
|
||||
at: (...args) => this.mock.methods[`$at_${libraryName}_Trace${length}(uint256,uint32)`](0, ...args),
|
||||
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),
|
||||
const fixture = async () => {
|
||||
const mock = await ethers.deployContract('$Checkpoints');
|
||||
const methods = {
|
||||
at: (...args) => mock.getFunction(`$at_Checkpoints_Trace${length}`)(0, ...args),
|
||||
latest: (...args) => mock.getFunction(`$latest_Checkpoints_Trace${length}`)(0, ...args),
|
||||
latestCheckpoint: (...args) => mock.getFunction(`$latestCheckpoint_Checkpoints_Trace${length}`)(0, ...args),
|
||||
length: (...args) => mock.getFunction(`$length_Checkpoints_Trace${length}`)(0, ...args),
|
||||
push: (...args) => mock.getFunction(`$push(uint256,uint${256 - length},uint${length})`)(0, ...args),
|
||||
lowerLookup: (...args) => mock.getFunction(`$lowerLookup(uint256,uint${256 - length})`)(0, ...args),
|
||||
upperLookup: (...args) => mock.getFunction(`$upperLookup(uint256,uint${256 - length})`)(0, ...args),
|
||||
upperLookupRecent: (...args) =>
|
||||
this.mock.methods[`$upperLookupRecent(uint256,uint${256 - length})`](0, ...args),
|
||||
mock.getFunction(`$upperLookupRecent(uint256,uint${256 - length})`)(0, ...args),
|
||||
};
|
||||
|
||||
return { mock, methods };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('without checkpoints', function () {
|
||||
it('at zero reverts', async function () {
|
||||
// Reverts with array out of bound access, which is unspecified
|
||||
await expectRevert.unspecified(this.methods.at(0));
|
||||
await expect(this.methods.at(0)).to.be.reverted;
|
||||
});
|
||||
|
||||
it('returns zero as latest value', async function () {
|
||||
expect(await this.methods.latest()).to.be.bignumber.equal('0');
|
||||
expect(await this.methods.latest()).to.equal(0n);
|
||||
|
||||
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');
|
||||
expect(ckpt[0]).to.be.false;
|
||||
expect(ckpt[1]).to.equal(0n);
|
||||
expect(ckpt[2]).to.equal(0n);
|
||||
});
|
||||
|
||||
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');
|
||||
expect(await this.methods.lowerLookup(0)).to.equal(0n);
|
||||
expect(await this.methods.upperLookup(0)).to.equal(0n);
|
||||
expect(await this.methods.upperLookupRecent(0)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
|
||||
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' },
|
||||
{ key: 2n, value: 17n },
|
||||
{ key: 3n, value: 42n },
|
||||
{ key: 5n, value: 101n },
|
||||
{ key: 7n, value: 23n },
|
||||
{ key: 11n, value: 99n },
|
||||
];
|
||||
for (const { key, value } of this.checkpoints) {
|
||||
await this.methods.push(key, value);
|
||||
@ -75,70 +69,66 @@ contract('Checkpoints', function () {
|
||||
it('at keys', async function () {
|
||||
for (const [index, { key, value }] of this.checkpoints.entries()) {
|
||||
const at = await this.methods.at(index);
|
||||
expect(at._value).to.be.bignumber.equal(value);
|
||||
expect(at._key).to.be.bignumber.equal(key);
|
||||
expect(at._value).to.equal(value);
|
||||
expect(at._key).to.equal(key);
|
||||
}
|
||||
});
|
||||
|
||||
it('length', async function () {
|
||||
expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
|
||||
expect(await this.methods.length()).to.equal(this.checkpoints.length);
|
||||
});
|
||||
|
||||
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);
|
||||
const latest = this.checkpoints.at(-1);
|
||||
expect(await this.methods.latest()).to.equal(latest.value);
|
||||
expect(await this.methods.latestCheckpoint()).to.have.ordered.members([true, latest.key, latest.value]);
|
||||
});
|
||||
|
||||
it('cannot push values in the past', async function () {
|
||||
await expectRevertCustomError(
|
||||
this.methods.push(last(this.checkpoints).key - 1, '0'),
|
||||
await expect(this.methods.push(this.checkpoints.at(-1).key - 1n, 0n)).to.be.revertedWithCustomError(
|
||||
this.mock,
|
||||
'CheckpointUnorderedInsertion',
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
it('can update last value', async function () {
|
||||
const newValue = '42';
|
||||
const newValue = 42n;
|
||||
|
||||
// check length before the update
|
||||
expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
|
||||
expect(await this.methods.length()).to.equal(this.checkpoints.length);
|
||||
|
||||
// update last key
|
||||
await this.methods.push(last(this.checkpoints).key, newValue);
|
||||
expect(await this.methods.latest()).to.be.bignumber.equal(newValue);
|
||||
await this.methods.push(this.checkpoints.at(-1).key, newValue);
|
||||
expect(await this.methods.latest()).to.equal(newValue);
|
||||
|
||||
// check that length did not change
|
||||
expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
|
||||
expect(await this.methods.length()).to.equal(this.checkpoints.length);
|
||||
});
|
||||
|
||||
it('lower lookup', async function () {
|
||||
for (let i = 0; i < 14; ++i) {
|
||||
const value = first(this.checkpoints.filter(x => i <= x.key))?.value || '0';
|
||||
const value = this.checkpoints.find(x => i <= x.key)?.value || 0n;
|
||||
|
||||
expect(await this.methods.lowerLookup(i)).to.be.bignumber.equal(value);
|
||||
expect(await this.methods.lowerLookup(i)).to.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';
|
||||
const value = last(this.checkpoints.filter(x => i >= x.key))?.value || 0n;
|
||||
|
||||
expect(await this.methods.upperLookup(i)).to.be.bignumber.equal(value);
|
||||
expect(await this.methods.upperLookupRecent(i)).to.be.bignumber.equal(value);
|
||||
expect(await this.methods.upperLookup(i)).to.equal(value);
|
||||
expect(await this.methods.upperLookupRecent(i)).to.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' },
|
||||
{ key: 12n, value: 22n },
|
||||
{ key: 13n, value: 131n },
|
||||
{ key: 17n, value: 45n },
|
||||
{ key: 19n, value: 31452n },
|
||||
{ key: 21n, value: 0n },
|
||||
];
|
||||
const allCheckpoints = [].concat(this.checkpoints, moreCheckpoints);
|
||||
|
||||
@ -147,9 +137,9 @@ contract('Checkpoints', function () {
|
||||
}
|
||||
|
||||
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);
|
||||
const value = last(allCheckpoints.filter(x => i >= x.key))?.value || 0n;
|
||||
expect(await this.methods.upperLookup(i)).to.equal(value);
|
||||
expect(await this.methods.upperLookupRecent(i)).to.equal(value);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,99 +1,105 @@
|
||||
const { expectEvent } = require('@openzeppelin/test-helpers');
|
||||
const { expectRevertCustomError } = require('../../helpers/customError');
|
||||
const { expect } = require('chai');
|
||||
const { ethers } = require('hardhat');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const DoubleEndedQueue = artifacts.require('$DoubleEndedQueue');
|
||||
async function fixture() {
|
||||
const mock = await ethers.deployContract('$DoubleEndedQueue');
|
||||
|
||||
/** Rebuild the content of the deque as a JS array. */
|
||||
const getContent = deque =>
|
||||
deque.$length(0).then(bn =>
|
||||
Promise.all(
|
||||
Array(bn.toNumber())
|
||||
.fill()
|
||||
.map((_, i) => deque.$at(0, i)),
|
||||
),
|
||||
);
|
||||
/** Rebuild the content of the deque as a JS array. */
|
||||
const getContent = () =>
|
||||
mock.$length(0).then(length =>
|
||||
Promise.all(
|
||||
Array(Number(length))
|
||||
.fill()
|
||||
.map((_, i) => mock.$at(0, i)),
|
||||
),
|
||||
);
|
||||
|
||||
contract('DoubleEndedQueue', function () {
|
||||
const bytesA = '0xdeadbeef'.padEnd(66, '0');
|
||||
const bytesB = '0x0123456789'.padEnd(66, '0');
|
||||
const bytesC = '0x42424242'.padEnd(66, '0');
|
||||
const bytesD = '0x171717'.padEnd(66, '0');
|
||||
return { mock, getContent };
|
||||
}
|
||||
|
||||
describe('DoubleEndedQueue', function () {
|
||||
const coder = ethers.AbiCoder.defaultAbiCoder();
|
||||
const bytesA = coder.encode(['uint256'], [0xdeadbeef]);
|
||||
const bytesB = coder.encode(['uint256'], [0x0123456789]);
|
||||
const bytesC = coder.encode(['uint256'], [0x42424242]);
|
||||
const bytesD = coder.encode(['uint256'], [0x171717]);
|
||||
|
||||
beforeEach(async function () {
|
||||
this.deque = await DoubleEndedQueue.new();
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('when empty', function () {
|
||||
it('getters', async function () {
|
||||
expect(await this.deque.$empty(0)).to.be.equal(true);
|
||||
expect(await getContent(this.deque)).to.have.ordered.members([]);
|
||||
expect(await this.mock.$empty(0)).to.be.true;
|
||||
expect(await this.getContent()).to.have.ordered.members([]);
|
||||
});
|
||||
|
||||
it('reverts on accesses', async function () {
|
||||
await expectRevertCustomError(this.deque.$popBack(0), 'QueueEmpty', []);
|
||||
await expectRevertCustomError(this.deque.$popFront(0), 'QueueEmpty', []);
|
||||
await expectRevertCustomError(this.deque.$back(0), 'QueueEmpty', []);
|
||||
await expectRevertCustomError(this.deque.$front(0), 'QueueEmpty', []);
|
||||
await expect(this.mock.$popBack(0)).to.be.revertedWithCustomError(this.mock, 'QueueEmpty');
|
||||
await expect(this.mock.$popFront(0)).to.be.revertedWithCustomError(this.mock, 'QueueEmpty');
|
||||
await expect(this.mock.$back(0)).to.be.revertedWithCustomError(this.mock, 'QueueEmpty');
|
||||
await expect(this.mock.$front(0)).to.be.revertedWithCustomError(this.mock, 'QueueEmpty');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when not empty', function () {
|
||||
beforeEach(async function () {
|
||||
await this.deque.$pushBack(0, bytesB);
|
||||
await this.deque.$pushFront(0, bytesA);
|
||||
await this.deque.$pushBack(0, bytesC);
|
||||
await this.mock.$pushBack(0, bytesB);
|
||||
await this.mock.$pushFront(0, bytesA);
|
||||
await this.mock.$pushBack(0, bytesC);
|
||||
this.content = [bytesA, bytesB, bytesC];
|
||||
});
|
||||
|
||||
it('getters', async function () {
|
||||
expect(await this.deque.$empty(0)).to.be.equal(false);
|
||||
expect(await this.deque.$length(0)).to.be.bignumber.equal(this.content.length.toString());
|
||||
expect(await this.deque.$front(0)).to.be.equal(this.content[0]);
|
||||
expect(await this.deque.$back(0)).to.be.equal(this.content[this.content.length - 1]);
|
||||
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
|
||||
expect(await this.mock.$empty(0)).to.be.false;
|
||||
expect(await this.mock.$length(0)).to.equal(this.content.length);
|
||||
expect(await this.mock.$front(0)).to.equal(this.content[0]);
|
||||
expect(await this.mock.$back(0)).to.equal(this.content[this.content.length - 1]);
|
||||
expect(await this.getContent()).to.have.ordered.members(this.content);
|
||||
});
|
||||
|
||||
it('out of bounds access', async function () {
|
||||
await expectRevertCustomError(this.deque.$at(0, this.content.length), 'QueueOutOfBounds', []);
|
||||
await expect(this.mock.$at(0, this.content.length)).to.be.revertedWithCustomError(this.mock, 'QueueOutOfBounds');
|
||||
});
|
||||
|
||||
describe('push', function () {
|
||||
it('front', async function () {
|
||||
await this.deque.$pushFront(0, bytesD);
|
||||
await this.mock.$pushFront(0, bytesD);
|
||||
this.content.unshift(bytesD); // add element at the beginning
|
||||
|
||||
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
|
||||
expect(await this.getContent()).to.have.ordered.members(this.content);
|
||||
});
|
||||
|
||||
it('back', async function () {
|
||||
await this.deque.$pushBack(0, bytesD);
|
||||
await this.mock.$pushBack(0, bytesD);
|
||||
this.content.push(bytesD); // add element at the end
|
||||
|
||||
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
|
||||
expect(await this.getContent()).to.have.ordered.members(this.content);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pop', function () {
|
||||
it('front', async function () {
|
||||
const value = this.content.shift(); // remove first element
|
||||
expectEvent(await this.deque.$popFront(0), 'return$popFront', { value });
|
||||
await expect(this.mock.$popFront(0)).to.emit(this.mock, 'return$popFront').withArgs(value);
|
||||
|
||||
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
|
||||
expect(await this.getContent()).to.have.ordered.members(this.content);
|
||||
});
|
||||
|
||||
it('back', async function () {
|
||||
const value = this.content.pop(); // remove last element
|
||||
expectEvent(await this.deque.$popBack(0), 'return$popBack', { value });
|
||||
await expect(this.mock.$popBack(0)).to.emit(this.mock, 'return$popBack').withArgs(value);
|
||||
|
||||
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
|
||||
expect(await this.getContent()).to.have.ordered.members(this.content);
|
||||
});
|
||||
});
|
||||
|
||||
it('clear', async function () {
|
||||
await this.deque.$clear(0);
|
||||
await this.mock.$clear(0);
|
||||
|
||||
expect(await this.deque.$empty(0)).to.be.equal(true);
|
||||
expect(await getContent(this.deque)).to.have.ordered.members([]);
|
||||
expect(await this.mock.$empty(0)).to.be.true;
|
||||
expect(await this.getContent()).to.have.ordered.members([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,173 +1,146 @@
|
||||
const { expectEvent } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const { ethers } = require('hardhat');
|
||||
|
||||
const zip = require('lodash.zip');
|
||||
const { expectRevertCustomError } = require('../../helpers/customError');
|
||||
const zip = (array1, array2) => array1.map((item, index) => [item, array2[index]]);
|
||||
|
||||
function shouldBehaveLikeMap(keys, values, zeroValue, methods, events) {
|
||||
const [keyA, keyB, keyC] = keys;
|
||||
const [valueA, valueB, valueC] = values;
|
||||
|
||||
async function expectMembersMatch(map, keys, values) {
|
||||
function shouldBehaveLikeMap(zeroValue, keyType, events) {
|
||||
async function expectMembersMatch(methods, keys, values) {
|
||||
expect(keys.length).to.equal(values.length);
|
||||
expect(await methods.length()).to.equal(keys.length);
|
||||
expect([...(await methods.keys())]).to.have.members(keys);
|
||||
|
||||
await Promise.all(keys.map(async key => expect(await methods.contains(map, key)).to.equal(true)));
|
||||
for (const [key, value] of zip(keys, values)) {
|
||||
expect(await methods.contains(key)).to.be.true;
|
||||
expect(await methods.get(key)).to.equal(value);
|
||||
}
|
||||
|
||||
expect(await methods.length(map)).to.bignumber.equal(keys.length.toString());
|
||||
|
||||
expect((await Promise.all(keys.map(key => methods.get(map, key)))).map(k => k.toString())).to.have.same.members(
|
||||
values.map(value => value.toString()),
|
||||
);
|
||||
|
||||
// To compare key-value pairs, we zip keys and values, and convert BNs to
|
||||
// strings to workaround Chai limitations when dealing with nested arrays
|
||||
expect(
|
||||
await Promise.all(
|
||||
[...Array(keys.length).keys()].map(async index => {
|
||||
const entry = await methods.at(map, index);
|
||||
return [entry[0].toString(), entry[1].toString()];
|
||||
}),
|
||||
),
|
||||
).to.have.same.deep.members(
|
||||
zip(
|
||||
keys.map(k => k.toString()),
|
||||
values.map(v => v.toString()),
|
||||
),
|
||||
);
|
||||
|
||||
// This also checks that both arrays have the same length
|
||||
expect((await methods.keys(map)).map(k => k.toString())).to.have.same.members(keys.map(key => key.toString()));
|
||||
expect(await Promise.all(keys.map((_, index) => methods.at(index)))).to.have.deep.members(zip(keys, values));
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
expect(await methods.contains(this.map, keyA)).to.equal(false);
|
||||
expect(await this.methods.contains(this.keyA)).to.be.false;
|
||||
|
||||
await expectMembersMatch(this.map, [], []);
|
||||
await expectMembersMatch(this.methods, [], []);
|
||||
});
|
||||
|
||||
describe('set', function () {
|
||||
it('adds a key', async function () {
|
||||
const receipt = await methods.set(this.map, keyA, valueA);
|
||||
expectEvent(receipt, events.setReturn, { ret0: true });
|
||||
await expect(this.methods.set(this.keyA, this.valueA)).to.emit(this.mock, events.setReturn).withArgs(true);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [valueA]);
|
||||
await expectMembersMatch(this.methods, [this.keyA], [this.valueA]);
|
||||
});
|
||||
|
||||
it('adds several keys', async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.set(this.map, keyB, valueB);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyB, this.valueB);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA, keyB], [valueA, valueB]);
|
||||
expect(await methods.contains(this.map, keyC)).to.equal(false);
|
||||
await expectMembersMatch(this.methods, [this.keyA, this.keyB], [this.valueA, this.valueB]);
|
||||
expect(await this.methods.contains(this.keyC)).to.be.false;
|
||||
});
|
||||
|
||||
it('returns false when adding keys already in the set', async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
|
||||
const receipt = await methods.set(this.map, keyA, valueA);
|
||||
expectEvent(receipt, events.setReturn, { ret0: false });
|
||||
await expect(this.methods.set(this.keyA, this.valueA)).to.emit(this.mock, events.setReturn).withArgs(false);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [valueA]);
|
||||
await expectMembersMatch(this.methods, [this.keyA], [this.valueA]);
|
||||
});
|
||||
|
||||
it('updates values for keys already in the set', async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.set(this.map, keyA, valueB);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyA, this.valueB);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [valueB]);
|
||||
await expectMembersMatch(this.methods, [this.keyA], [this.valueB]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', function () {
|
||||
it('removes added keys', async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
|
||||
const receipt = await methods.remove(this.map, keyA);
|
||||
expectEvent(receipt, events.removeReturn, { ret0: true });
|
||||
await expect(this.methods.remove(this.keyA)).to.emit(this.mock, events.removeReturn).withArgs(true);
|
||||
|
||||
expect(await methods.contains(this.map, keyA)).to.equal(false);
|
||||
await expectMembersMatch(this.map, [], []);
|
||||
expect(await this.methods.contains(this.keyA)).to.be.false;
|
||||
await expectMembersMatch(this.methods, [], []);
|
||||
});
|
||||
|
||||
it('returns false when removing keys not in the set', async function () {
|
||||
const receipt = await methods.remove(this.map, keyA);
|
||||
expectEvent(receipt, events.removeReturn, { ret0: false });
|
||||
await expect(await this.methods.remove(this.keyA))
|
||||
.to.emit(this.mock, events.removeReturn)
|
||||
.withArgs(false);
|
||||
|
||||
expect(await methods.contains(this.map, keyA)).to.equal(false);
|
||||
expect(await this.methods.contains(this.keyA)).to.be.false;
|
||||
});
|
||||
|
||||
it('adds and removes multiple keys', async function () {
|
||||
// []
|
||||
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.set(this.map, keyC, valueC);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyC, this.valueC);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await methods.remove(this.map, keyA);
|
||||
await methods.remove(this.map, keyB);
|
||||
await this.methods.remove(this.keyA);
|
||||
await this.methods.remove(this.keyB);
|
||||
|
||||
// [C]
|
||||
|
||||
await methods.set(this.map, keyB, valueB);
|
||||
await this.methods.set(this.keyB, this.valueB);
|
||||
|
||||
// [C, B]
|
||||
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.remove(this.map, keyC);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.remove(this.keyC);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.set(this.map, keyB, valueB);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyB, this.valueB);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await methods.set(this.map, keyC, valueC);
|
||||
await methods.remove(this.map, keyA);
|
||||
await this.methods.set(this.keyC, this.valueC);
|
||||
await this.methods.remove(this.keyA);
|
||||
|
||||
// [B, C]
|
||||
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.remove(this.map, keyB);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.remove(this.keyB);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await expectMembersMatch(this.map, [keyA, keyC], [valueA, valueC]);
|
||||
await expectMembersMatch(this.methods, [this.keyA, this.keyC], [this.valueA, this.valueC]);
|
||||
|
||||
expect(await methods.contains(this.map, keyA)).to.equal(true);
|
||||
expect(await methods.contains(this.map, keyB)).to.equal(false);
|
||||
expect(await methods.contains(this.map, keyC)).to.equal(true);
|
||||
expect(await this.methods.contains(this.keyA)).to.be.true;
|
||||
expect(await this.methods.contains(this.keyB)).to.be.false;
|
||||
expect(await this.methods.contains(this.keyC)).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('read', function () {
|
||||
beforeEach(async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
it('existing value', async function () {
|
||||
expect(await methods.get(this.map, keyA).then(r => r.toString())).to.be.equal(valueA.toString());
|
||||
expect(await this.methods.get(this.keyA)).to.be.equal(this.valueA);
|
||||
});
|
||||
|
||||
it('missing value', async function () {
|
||||
const key = web3.utils.toHex(keyB);
|
||||
await expectRevertCustomError(methods.get(this.map, keyB), 'EnumerableMapNonexistentKey', [
|
||||
key.length == 66 ? key : web3.utils.padLeft(key, 64, '0'),
|
||||
]);
|
||||
await expect(this.methods.get(this.keyB))
|
||||
.to.be.revertedWithCustomError(this.mock, 'EnumerableMapNonexistentKey')
|
||||
.withArgs(ethers.AbiCoder.defaultAbiCoder().encode([keyType], [this.keyB]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('tryGet', function () {
|
||||
it('existing value', async function () {
|
||||
const result = await methods.tryGet(this.map, keyA);
|
||||
expect(result['0']).to.be.equal(true);
|
||||
expect(result['1'].toString()).to.be.equal(valueA.toString());
|
||||
expect(await this.methods.tryGet(this.keyA)).to.have.ordered.members([true, this.valueA]);
|
||||
});
|
||||
|
||||
it('missing value', async function () {
|
||||
const result = await methods.tryGet(this.map, keyB);
|
||||
expect(result['0']).to.be.equal(false);
|
||||
expect(result['1'].toString()).to.be.equal(zeroValue.toString());
|
||||
expect(await this.methods.tryGet(this.keyB)).to.have.ordered.members([false, zeroValue]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,149 +1,145 @@
|
||||
const { BN, constants } = require('@openzeppelin/test-helpers');
|
||||
const { ethers } = require('hardhat');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { mapValues } = require('../../helpers/iterate');
|
||||
|
||||
const EnumerableMap = artifacts.require('$EnumerableMap');
|
||||
const { randomArray, generators } = require('../../helpers/random');
|
||||
|
||||
const { shouldBehaveLikeMap } = require('./EnumerableMap.behavior');
|
||||
|
||||
const getMethods = ms => {
|
||||
const getMethods = (mock, fnSigs) => {
|
||||
return mapValues(
|
||||
ms,
|
||||
m =>
|
||||
(self, ...args) =>
|
||||
self.methods[m](0, ...args),
|
||||
fnSigs,
|
||||
fnSig =>
|
||||
(...args) =>
|
||||
mock.getFunction(fnSig)(0, ...args),
|
||||
);
|
||||
};
|
||||
|
||||
// Get the name of the library. In the transpiled code it will be EnumerableMapUpgradeable.
|
||||
const library = EnumerableMap._json.contractName.replace(/^\$/, '');
|
||||
|
||||
contract('EnumerableMap', function (accounts) {
|
||||
const [accountA, accountB, accountC] = accounts;
|
||||
|
||||
const keyA = new BN('7891');
|
||||
const keyB = new BN('451');
|
||||
const keyC = new BN('9592328');
|
||||
|
||||
const bytesA = '0xdeadbeef'.padEnd(66, '0');
|
||||
const bytesB = '0x0123456789'.padEnd(66, '0');
|
||||
const bytesC = '0x42424242'.padEnd(66, '0');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.map = await EnumerableMap.new();
|
||||
});
|
||||
|
||||
// AddressToUintMap
|
||||
describe('AddressToUintMap', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[accountA, accountB, accountC],
|
||||
[keyA, keyB, keyC],
|
||||
new BN('0'),
|
||||
getMethods({
|
||||
set: '$set(uint256,address,uint256)',
|
||||
get: '$get(uint256,address)',
|
||||
tryGet: '$tryGet(uint256,address)',
|
||||
remove: '$remove(uint256,address)',
|
||||
length: `$length_${library}_AddressToUintMap(uint256)`,
|
||||
at: `$at_${library}_AddressToUintMap(uint256,uint256)`,
|
||||
contains: '$contains(uint256,address)',
|
||||
keys: `$keys_${library}_AddressToUintMap(uint256)`,
|
||||
}),
|
||||
{
|
||||
setReturn: `return$set_${library}_AddressToUintMap_address_uint256`,
|
||||
removeReturn: `return$remove_${library}_AddressToUintMap_address`,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('EnumerableMap', function () {
|
||||
// UintToAddressMap
|
||||
describe('UintToAddressMap', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[keyA, keyB, keyC],
|
||||
[accountA, accountB, accountC],
|
||||
constants.ZERO_ADDRESS,
|
||||
getMethods({
|
||||
const fixture = async () => {
|
||||
const mock = await ethers.deployContract('$EnumerableMap');
|
||||
|
||||
const [keyA, keyB, keyC] = randomArray(generators.uint256);
|
||||
const [valueA, valueB, valueC] = randomArray(generators.address);
|
||||
|
||||
const methods = getMethods(mock, {
|
||||
set: '$set(uint256,uint256,address)',
|
||||
get: `$get_${library}_UintToAddressMap(uint256,uint256)`,
|
||||
tryGet: `$tryGet_${library}_UintToAddressMap(uint256,uint256)`,
|
||||
remove: `$remove_${library}_UintToAddressMap(uint256,uint256)`,
|
||||
length: `$length_${library}_UintToAddressMap(uint256)`,
|
||||
at: `$at_${library}_UintToAddressMap(uint256,uint256)`,
|
||||
contains: `$contains_${library}_UintToAddressMap(uint256,uint256)`,
|
||||
keys: `$keys_${library}_UintToAddressMap(uint256)`,
|
||||
}),
|
||||
{
|
||||
setReturn: `return$set_${library}_UintToAddressMap_uint256_address`,
|
||||
removeReturn: `return$remove_${library}_UintToAddressMap_uint256`,
|
||||
},
|
||||
);
|
||||
get: '$get_EnumerableMap_UintToAddressMap(uint256,uint256)',
|
||||
tryGet: '$tryGet_EnumerableMap_UintToAddressMap(uint256,uint256)',
|
||||
remove: '$remove_EnumerableMap_UintToAddressMap(uint256,uint256)',
|
||||
length: '$length_EnumerableMap_UintToAddressMap(uint256)',
|
||||
at: '$at_EnumerableMap_UintToAddressMap(uint256,uint256)',
|
||||
contains: '$contains_EnumerableMap_UintToAddressMap(uint256,uint256)',
|
||||
keys: '$keys_EnumerableMap_UintToAddressMap(uint256)',
|
||||
});
|
||||
|
||||
return { mock, keyA, keyB, keyC, valueA, valueB, valueC, methods };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeMap(ethers.ZeroAddress, 'uint256', {
|
||||
setReturn: 'return$set_EnumerableMap_UintToAddressMap_uint256_address',
|
||||
removeReturn: 'return$remove_EnumerableMap_UintToAddressMap_uint256',
|
||||
});
|
||||
});
|
||||
|
||||
// Bytes32ToBytes32Map
|
||||
describe('Bytes32ToBytes32Map', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[keyA, keyB, keyC].map(k => '0x' + k.toString(16).padEnd(64, '0')),
|
||||
[bytesA, bytesB, bytesC],
|
||||
constants.ZERO_BYTES32,
|
||||
getMethods({
|
||||
const fixture = async () => {
|
||||
const mock = await ethers.deployContract('$EnumerableMap');
|
||||
|
||||
const [keyA, keyB, keyC] = randomArray(generators.bytes32);
|
||||
const [valueA, valueB, valueC] = randomArray(generators.bytes32);
|
||||
|
||||
const methods = getMethods(mock, {
|
||||
set: '$set(uint256,bytes32,bytes32)',
|
||||
get: `$get_${library}_Bytes32ToBytes32Map(uint256,bytes32)`,
|
||||
tryGet: `$tryGet_${library}_Bytes32ToBytes32Map(uint256,bytes32)`,
|
||||
remove: `$remove_${library}_Bytes32ToBytes32Map(uint256,bytes32)`,
|
||||
length: `$length_${library}_Bytes32ToBytes32Map(uint256)`,
|
||||
at: `$at_${library}_Bytes32ToBytes32Map(uint256,uint256)`,
|
||||
contains: `$contains_${library}_Bytes32ToBytes32Map(uint256,bytes32)`,
|
||||
keys: `$keys_${library}_Bytes32ToBytes32Map(uint256)`,
|
||||
}),
|
||||
{
|
||||
setReturn: `return$set_${library}_Bytes32ToBytes32Map_bytes32_bytes32`,
|
||||
removeReturn: `return$remove_${library}_Bytes32ToBytes32Map_bytes32`,
|
||||
},
|
||||
);
|
||||
get: '$get_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
|
||||
tryGet: '$tryGet_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
|
||||
remove: '$remove_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
|
||||
length: '$length_EnumerableMap_Bytes32ToBytes32Map(uint256)',
|
||||
at: '$at_EnumerableMap_Bytes32ToBytes32Map(uint256,uint256)',
|
||||
contains: '$contains_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
|
||||
keys: '$keys_EnumerableMap_Bytes32ToBytes32Map(uint256)',
|
||||
});
|
||||
|
||||
return { mock, keyA, keyB, keyC, valueA, valueB, valueC, methods };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeMap(ethers.ZeroHash, 'bytes32', {
|
||||
setReturn: 'return$set_EnumerableMap_Bytes32ToBytes32Map_bytes32_bytes32',
|
||||
removeReturn: 'return$remove_EnumerableMap_Bytes32ToBytes32Map_bytes32',
|
||||
});
|
||||
});
|
||||
|
||||
// UintToUintMap
|
||||
describe('UintToUintMap', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[keyA, keyB, keyC],
|
||||
[keyA, keyB, keyC].map(k => k.add(new BN('1332'))),
|
||||
new BN('0'),
|
||||
getMethods({
|
||||
const fixture = async () => {
|
||||
const mock = await ethers.deployContract('$EnumerableMap');
|
||||
|
||||
const [keyA, keyB, keyC] = randomArray(generators.uint256);
|
||||
const [valueA, valueB, valueC] = randomArray(generators.uint256);
|
||||
|
||||
const methods = getMethods(mock, {
|
||||
set: '$set(uint256,uint256,uint256)',
|
||||
get: `$get_${library}_UintToUintMap(uint256,uint256)`,
|
||||
tryGet: `$tryGet_${library}_UintToUintMap(uint256,uint256)`,
|
||||
remove: `$remove_${library}_UintToUintMap(uint256,uint256)`,
|
||||
length: `$length_${library}_UintToUintMap(uint256)`,
|
||||
at: `$at_${library}_UintToUintMap(uint256,uint256)`,
|
||||
contains: `$contains_${library}_UintToUintMap(uint256,uint256)`,
|
||||
keys: `$keys_${library}_UintToUintMap(uint256)`,
|
||||
}),
|
||||
{
|
||||
setReturn: `return$set_${library}_UintToUintMap_uint256_uint256`,
|
||||
removeReturn: `return$remove_${library}_UintToUintMap_uint256`,
|
||||
},
|
||||
);
|
||||
get: '$get_EnumerableMap_UintToUintMap(uint256,uint256)',
|
||||
tryGet: '$tryGet_EnumerableMap_UintToUintMap(uint256,uint256)',
|
||||
remove: '$remove_EnumerableMap_UintToUintMap(uint256,uint256)',
|
||||
length: '$length_EnumerableMap_UintToUintMap(uint256)',
|
||||
at: '$at_EnumerableMap_UintToUintMap(uint256,uint256)',
|
||||
contains: '$contains_EnumerableMap_UintToUintMap(uint256,uint256)',
|
||||
keys: '$keys_EnumerableMap_UintToUintMap(uint256)',
|
||||
});
|
||||
|
||||
return { mock, keyA, keyB, keyC, valueA, valueB, valueC, methods };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeMap(0n, 'uint256', {
|
||||
setReturn: 'return$set_EnumerableMap_UintToUintMap_uint256_uint256',
|
||||
removeReturn: 'return$remove_EnumerableMap_UintToUintMap_uint256',
|
||||
});
|
||||
});
|
||||
|
||||
// Bytes32ToUintMap
|
||||
describe('Bytes32ToUintMap', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[bytesA, bytesB, bytesC],
|
||||
[keyA, keyB, keyC],
|
||||
new BN('0'),
|
||||
getMethods({
|
||||
const fixture = async () => {
|
||||
const mock = await ethers.deployContract('$EnumerableMap');
|
||||
|
||||
const [keyA, keyB, keyC] = randomArray(generators.bytes32);
|
||||
const [valueA, valueB, valueC] = randomArray(generators.uint256);
|
||||
|
||||
const methods = getMethods(mock, {
|
||||
set: '$set(uint256,bytes32,uint256)',
|
||||
get: `$get_${library}_Bytes32ToUintMap(uint256,bytes32)`,
|
||||
tryGet: `$tryGet_${library}_Bytes32ToUintMap(uint256,bytes32)`,
|
||||
remove: `$remove_${library}_Bytes32ToUintMap(uint256,bytes32)`,
|
||||
length: `$length_${library}_Bytes32ToUintMap(uint256)`,
|
||||
at: `$at_${library}_Bytes32ToUintMap(uint256,uint256)`,
|
||||
contains: `$contains_${library}_Bytes32ToUintMap(uint256,bytes32)`,
|
||||
keys: `$keys_${library}_Bytes32ToUintMap(uint256)`,
|
||||
}),
|
||||
{
|
||||
setReturn: `return$set_${library}_Bytes32ToUintMap_bytes32_uint256`,
|
||||
removeReturn: `return$remove_${library}_Bytes32ToUintMap_bytes32`,
|
||||
},
|
||||
);
|
||||
get: '$get_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
|
||||
tryGet: '$tryGet_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
|
||||
remove: '$remove_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
|
||||
length: '$length_EnumerableMap_Bytes32ToUintMap(uint256)',
|
||||
at: '$at_EnumerableMap_Bytes32ToUintMap(uint256,uint256)',
|
||||
contains: '$contains_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
|
||||
keys: '$keys_EnumerableMap_Bytes32ToUintMap(uint256)',
|
||||
});
|
||||
|
||||
return { mock, keyA, keyB, keyC, valueA, valueB, valueC, methods };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeMap(0n, 'bytes32', {
|
||||
setReturn: 'return$set_EnumerableMap_Bytes32ToUintMap_bytes32_uint256',
|
||||
removeReturn: 'return$remove_EnumerableMap_Bytes32ToUintMap_bytes32',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,125 +1,106 @@
|
||||
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
function shouldBehaveLikeSet(values, methods, events) {
|
||||
const [valueA, valueB, valueC] = values;
|
||||
function shouldBehaveLikeSet(events) {
|
||||
async function expectMembersMatch(methods, values) {
|
||||
expect(await methods.length()).to.equal(values.length);
|
||||
for (const value of values) expect(await methods.contains(value)).to.be.true;
|
||||
|
||||
async function expectMembersMatch(set, values) {
|
||||
const contains = await Promise.all(values.map(value => methods.contains(set, value)));
|
||||
expect(contains.every(Boolean)).to.be.equal(true);
|
||||
|
||||
const length = await methods.length(set);
|
||||
expect(length).to.bignumber.equal(values.length.toString());
|
||||
|
||||
// To compare values we convert to strings to workaround Chai
|
||||
// limitations when dealing with nested arrays (required for BNs)
|
||||
const indexedValues = await Promise.all(
|
||||
Array(values.length)
|
||||
.fill()
|
||||
.map((_, index) => methods.at(set, index)),
|
||||
);
|
||||
expect(indexedValues.map(v => v.toString())).to.have.same.members(values.map(v => v.toString()));
|
||||
|
||||
const returnedValues = await methods.values(set);
|
||||
expect(returnedValues.map(v => v.toString())).to.have.same.members(values.map(v => v.toString()));
|
||||
expect(await Promise.all(values.map((_, index) => methods.at(index)))).to.have.deep.members(values);
|
||||
expect([...(await methods.values())]).to.have.deep.members(values);
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
expect(await methods.contains(this.set, valueA)).to.equal(false);
|
||||
expect(await this.methods.contains(this.valueA)).to.be.false;
|
||||
|
||||
await expectMembersMatch(this.set, []);
|
||||
await expectMembersMatch(this.methods, []);
|
||||
});
|
||||
|
||||
describe('add', function () {
|
||||
it('adds a value', async function () {
|
||||
const receipt = await methods.add(this.set, valueA);
|
||||
expectEvent(receipt, events.addReturn, { ret0: true });
|
||||
await expect(this.methods.add(this.valueA)).to.emit(this.mock, events.addReturn).withArgs(true);
|
||||
|
||||
await expectMembersMatch(this.set, [valueA]);
|
||||
await expectMembersMatch(this.methods, [this.valueA]);
|
||||
});
|
||||
|
||||
it('adds several values', async function () {
|
||||
await methods.add(this.set, valueA);
|
||||
await methods.add(this.set, valueB);
|
||||
await this.methods.add(this.valueA);
|
||||
await this.methods.add(this.valueB);
|
||||
|
||||
await expectMembersMatch(this.set, [valueA, valueB]);
|
||||
expect(await methods.contains(this.set, valueC)).to.equal(false);
|
||||
await expectMembersMatch(this.methods, [this.valueA, this.valueB]);
|
||||
expect(await this.methods.contains(this.valueC)).to.be.false;
|
||||
});
|
||||
|
||||
it('returns false when adding values already in the set', async function () {
|
||||
await methods.add(this.set, valueA);
|
||||
await this.methods.add(this.valueA);
|
||||
|
||||
const receipt = await methods.add(this.set, valueA);
|
||||
expectEvent(receipt, events.addReturn, { ret0: false });
|
||||
await expect(this.methods.add(this.valueA)).to.emit(this.mock, events.addReturn).withArgs(false);
|
||||
|
||||
await expectMembersMatch(this.set, [valueA]);
|
||||
await expectMembersMatch(this.methods, [this.valueA]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('at', function () {
|
||||
it('reverts when retrieving non-existent elements', async function () {
|
||||
await expectRevert.unspecified(methods.at(this.set, 0));
|
||||
await expect(this.methods.at(0)).to.be.reverted;
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', function () {
|
||||
it('removes added values', async function () {
|
||||
await methods.add(this.set, valueA);
|
||||
await this.methods.add(this.valueA);
|
||||
|
||||
const receipt = await methods.remove(this.set, valueA);
|
||||
expectEvent(receipt, events.removeReturn, { ret0: true });
|
||||
await expect(this.methods.remove(this.valueA)).to.emit(this.mock, events.removeReturn).withArgs(true);
|
||||
|
||||
expect(await methods.contains(this.set, valueA)).to.equal(false);
|
||||
await expectMembersMatch(this.set, []);
|
||||
expect(await this.methods.contains(this.valueA)).to.be.false;
|
||||
await expectMembersMatch(this.methods, []);
|
||||
});
|
||||
|
||||
it('returns false when removing values not in the set', async function () {
|
||||
const receipt = await methods.remove(this.set, valueA);
|
||||
expectEvent(receipt, events.removeReturn, { ret0: false });
|
||||
await expect(this.methods.remove(this.valueA)).to.emit(this.mock, events.removeReturn).withArgs(false);
|
||||
|
||||
expect(await methods.contains(this.set, valueA)).to.equal(false);
|
||||
expect(await this.methods.contains(this.valueA)).to.be.false;
|
||||
});
|
||||
|
||||
it('adds and removes multiple values', async function () {
|
||||
// []
|
||||
|
||||
await methods.add(this.set, valueA);
|
||||
await methods.add(this.set, valueC);
|
||||
await this.methods.add(this.valueA);
|
||||
await this.methods.add(this.valueC);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await methods.remove(this.set, valueA);
|
||||
await methods.remove(this.set, valueB);
|
||||
await this.methods.remove(this.valueA);
|
||||
await this.methods.remove(this.valueB);
|
||||
|
||||
// [C]
|
||||
|
||||
await methods.add(this.set, valueB);
|
||||
await this.methods.add(this.valueB);
|
||||
|
||||
// [C, B]
|
||||
|
||||
await methods.add(this.set, valueA);
|
||||
await methods.remove(this.set, valueC);
|
||||
await this.methods.add(this.valueA);
|
||||
await this.methods.remove(this.valueC);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await methods.add(this.set, valueA);
|
||||
await methods.add(this.set, valueB);
|
||||
await this.methods.add(this.valueA);
|
||||
await this.methods.add(this.valueB);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await methods.add(this.set, valueC);
|
||||
await methods.remove(this.set, valueA);
|
||||
await this.methods.add(this.valueC);
|
||||
await this.methods.remove(this.valueA);
|
||||
|
||||
// [B, C]
|
||||
|
||||
await methods.add(this.set, valueA);
|
||||
await methods.remove(this.set, valueB);
|
||||
await this.methods.add(this.valueA);
|
||||
await this.methods.remove(this.valueB);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await expectMembersMatch(this.set, [valueA, valueC]);
|
||||
await expectMembersMatch(this.methods, [this.valueA, this.valueC]);
|
||||
|
||||
expect(await methods.contains(this.set, valueB)).to.equal(false);
|
||||
expect(await this.methods.contains(this.valueB)).to.be.false;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,79 +1,104 @@
|
||||
const EnumerableSet = artifacts.require('$EnumerableSet');
|
||||
const { ethers } = require('hardhat');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { mapValues } = require('../../helpers/iterate');
|
||||
const { randomArray, generators } = require('../../helpers/random');
|
||||
|
||||
const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
|
||||
|
||||
const getMethods = ms => {
|
||||
const getMethods = (mock, fnSigs) => {
|
||||
return mapValues(
|
||||
ms,
|
||||
m =>
|
||||
(self, ...args) =>
|
||||
self.methods[m](0, ...args),
|
||||
fnSigs,
|
||||
fnSig =>
|
||||
(...args) =>
|
||||
mock.getFunction(fnSig)(0, ...args),
|
||||
);
|
||||
};
|
||||
|
||||
// Get the name of the library. In the transpiled code it will be EnumerableSetUpgradeable.
|
||||
const library = EnumerableSet._json.contractName.replace(/^\$/, '');
|
||||
|
||||
contract('EnumerableSet', function (accounts) {
|
||||
beforeEach(async function () {
|
||||
this.set = await EnumerableSet.new();
|
||||
});
|
||||
|
||||
describe('EnumerableSet', function () {
|
||||
// Bytes32Set
|
||||
describe('EnumerableBytes32Set', function () {
|
||||
shouldBehaveLikeSet(
|
||||
['0xdeadbeef', '0x0123456789', '0x42424242'].map(e => e.padEnd(66, '0')),
|
||||
getMethods({
|
||||
const fixture = async () => {
|
||||
const mock = await ethers.deployContract('$EnumerableSet');
|
||||
|
||||
const [valueA, valueB, valueC] = randomArray(generators.bytes32);
|
||||
|
||||
const methods = getMethods(mock, {
|
||||
add: '$add(uint256,bytes32)',
|
||||
remove: '$remove(uint256,bytes32)',
|
||||
contains: '$contains(uint256,bytes32)',
|
||||
length: `$length_${library}_Bytes32Set(uint256)`,
|
||||
at: `$at_${library}_Bytes32Set(uint256,uint256)`,
|
||||
values: `$values_${library}_Bytes32Set(uint256)`,
|
||||
}),
|
||||
{
|
||||
addReturn: `return$add_${library}_Bytes32Set_bytes32`,
|
||||
removeReturn: `return$remove_${library}_Bytes32Set_bytes32`,
|
||||
},
|
||||
);
|
||||
length: `$length_EnumerableSet_Bytes32Set(uint256)`,
|
||||
at: `$at_EnumerableSet_Bytes32Set(uint256,uint256)`,
|
||||
values: `$values_EnumerableSet_Bytes32Set(uint256)`,
|
||||
});
|
||||
|
||||
return { mock, valueA, valueB, valueC, methods };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet({
|
||||
addReturn: `return$add_EnumerableSet_Bytes32Set_bytes32`,
|
||||
removeReturn: `return$remove_EnumerableSet_Bytes32Set_bytes32`,
|
||||
});
|
||||
});
|
||||
|
||||
// AddressSet
|
||||
describe('EnumerableAddressSet', function () {
|
||||
shouldBehaveLikeSet(
|
||||
accounts,
|
||||
getMethods({
|
||||
const fixture = async () => {
|
||||
const mock = await ethers.deployContract('$EnumerableSet');
|
||||
|
||||
const [valueA, valueB, valueC] = randomArray(generators.address);
|
||||
|
||||
const methods = getMethods(mock, {
|
||||
add: '$add(uint256,address)',
|
||||
remove: '$remove(uint256,address)',
|
||||
contains: '$contains(uint256,address)',
|
||||
length: `$length_${library}_AddressSet(uint256)`,
|
||||
at: `$at_${library}_AddressSet(uint256,uint256)`,
|
||||
values: `$values_${library}_AddressSet(uint256)`,
|
||||
}),
|
||||
{
|
||||
addReturn: `return$add_${library}_AddressSet_address`,
|
||||
removeReturn: `return$remove_${library}_AddressSet_address`,
|
||||
},
|
||||
);
|
||||
length: `$length_EnumerableSet_AddressSet(uint256)`,
|
||||
at: `$at_EnumerableSet_AddressSet(uint256,uint256)`,
|
||||
values: `$values_EnumerableSet_AddressSet(uint256)`,
|
||||
});
|
||||
|
||||
return { mock, valueA, valueB, valueC, methods };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet({
|
||||
addReturn: `return$add_EnumerableSet_AddressSet_address`,
|
||||
removeReturn: `return$remove_EnumerableSet_AddressSet_address`,
|
||||
});
|
||||
});
|
||||
|
||||
// UintSet
|
||||
describe('EnumerableUintSet', function () {
|
||||
shouldBehaveLikeSet(
|
||||
[1234, 5678, 9101112].map(e => web3.utils.toBN(e)),
|
||||
getMethods({
|
||||
const fixture = async () => {
|
||||
const mock = await ethers.deployContract('$EnumerableSet');
|
||||
|
||||
const [valueA, valueB, valueC] = randomArray(generators.uint256);
|
||||
|
||||
const methods = getMethods(mock, {
|
||||
add: '$add(uint256,uint256)',
|
||||
remove: '$remove(uint256,uint256)',
|
||||
contains: '$contains(uint256,uint256)',
|
||||
length: `$length_${library}_UintSet(uint256)`,
|
||||
at: `$at_${library}_UintSet(uint256,uint256)`,
|
||||
values: `$values_${library}_UintSet(uint256)`,
|
||||
}),
|
||||
{
|
||||
addReturn: `return$add_${library}_UintSet_uint256`,
|
||||
removeReturn: `return$remove_${library}_UintSet_uint256`,
|
||||
},
|
||||
);
|
||||
length: `$length_EnumerableSet_UintSet(uint256)`,
|
||||
at: `$at_EnumerableSet_UintSet(uint256,uint256)`,
|
||||
values: `$values_EnumerableSet_UintSet(uint256)`,
|
||||
});
|
||||
|
||||
return { mock, valueA, valueB, valueC, methods };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet({
|
||||
addReturn: `return$add_EnumerableSet_UintSet_uint256`,
|
||||
removeReturn: `return$remove_EnumerableSet_UintSet_uint256`,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user