Transpile 7bce2b72
This commit is contained in:
145
test/utils/structs/BitMap.test.js
Normal file
145
test/utils/structs/BitMap.test.js
Normal file
@ -0,0 +1,145 @@
|
||||
const { BN } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const BitMap = artifacts.require('BitMapMock');
|
||||
|
||||
contract('BitMap', function (accounts) {
|
||||
const keyA = new BN('7891');
|
||||
const keyB = new BN('451');
|
||||
const keyC = new BN('9592328');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.bitmap = await BitMap.new();
|
||||
});
|
||||
|
||||
it('starts empty', async function () {
|
||||
expect(await this.bitmap.get(keyA)).to.equal(false);
|
||||
expect(await this.bitmap.get(keyB)).to.equal(false);
|
||||
expect(await this.bitmap.get(keyC)).to.equal(false);
|
||||
});
|
||||
|
||||
describe('setTo', function () {
|
||||
it('set a key to true', async function () {
|
||||
await this.bitmap.setTo(keyA, true);
|
||||
expect(await this.bitmap.get(keyA)).to.equal(true);
|
||||
expect(await this.bitmap.get(keyB)).to.equal(false);
|
||||
expect(await this.bitmap.get(keyC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('set a key to false', async function () {
|
||||
await this.bitmap.setTo(keyA, true);
|
||||
await this.bitmap.setTo(keyA, false);
|
||||
expect(await this.bitmap.get(keyA)).to.equal(false);
|
||||
expect(await this.bitmap.get(keyB)).to.equal(false);
|
||||
expect(await this.bitmap.get(keyC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('set several consecutive keys', async function () {
|
||||
await this.bitmap.setTo(keyA.addn(0), true);
|
||||
await this.bitmap.setTo(keyA.addn(1), true);
|
||||
await this.bitmap.setTo(keyA.addn(2), true);
|
||||
await this.bitmap.setTo(keyA.addn(3), true);
|
||||
await this.bitmap.setTo(keyA.addn(4), true);
|
||||
await this.bitmap.setTo(keyA.addn(2), false);
|
||||
await this.bitmap.setTo(keyA.addn(4), false);
|
||||
expect(await this.bitmap.get(keyA.addn(0))).to.equal(true);
|
||||
expect(await this.bitmap.get(keyA.addn(1))).to.equal(true);
|
||||
expect(await this.bitmap.get(keyA.addn(2))).to.equal(false);
|
||||
expect(await this.bitmap.get(keyA.addn(3))).to.equal(true);
|
||||
expect(await this.bitmap.get(keyA.addn(4))).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('set', function () {
|
||||
it('adds a key', async function () {
|
||||
await this.bitmap.set(keyA);
|
||||
expect(await this.bitmap.get(keyA)).to.equal(true);
|
||||
expect(await this.bitmap.get(keyB)).to.equal(false);
|
||||
expect(await this.bitmap.get(keyC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('adds several keys', async function () {
|
||||
await this.bitmap.set(keyA);
|
||||
await this.bitmap.set(keyB);
|
||||
expect(await this.bitmap.get(keyA)).to.equal(true);
|
||||
expect(await this.bitmap.get(keyB)).to.equal(true);
|
||||
expect(await this.bitmap.get(keyC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('adds several consecutive keys', async function () {
|
||||
await this.bitmap.set(keyA.addn(0));
|
||||
await this.bitmap.set(keyA.addn(1));
|
||||
await this.bitmap.set(keyA.addn(3));
|
||||
expect(await this.bitmap.get(keyA.addn(0))).to.equal(true);
|
||||
expect(await this.bitmap.get(keyA.addn(1))).to.equal(true);
|
||||
expect(await this.bitmap.get(keyA.addn(2))).to.equal(false);
|
||||
expect(await this.bitmap.get(keyA.addn(3))).to.equal(true);
|
||||
expect(await this.bitmap.get(keyA.addn(4))).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('unset', function () {
|
||||
it('removes added keys', async function () {
|
||||
await this.bitmap.set(keyA);
|
||||
await this.bitmap.set(keyB);
|
||||
await this.bitmap.unset(keyA);
|
||||
expect(await this.bitmap.get(keyA)).to.equal(false);
|
||||
expect(await this.bitmap.get(keyB)).to.equal(true);
|
||||
expect(await this.bitmap.get(keyC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('removes consecutive added keys', async function () {
|
||||
await this.bitmap.set(keyA.addn(0));
|
||||
await this.bitmap.set(keyA.addn(1));
|
||||
await this.bitmap.set(keyA.addn(3));
|
||||
await this.bitmap.unset(keyA.addn(1));
|
||||
expect(await this.bitmap.get(keyA.addn(0))).to.equal(true);
|
||||
expect(await this.bitmap.get(keyA.addn(1))).to.equal(false);
|
||||
expect(await this.bitmap.get(keyA.addn(2))).to.equal(false);
|
||||
expect(await this.bitmap.get(keyA.addn(3))).to.equal(true);
|
||||
expect(await this.bitmap.get(keyA.addn(4))).to.equal(false);
|
||||
});
|
||||
|
||||
it('adds and removes multiple keys', async function () {
|
||||
// []
|
||||
|
||||
await this.bitmap.set(keyA);
|
||||
await this.bitmap.set(keyC);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await this.bitmap.unset(keyA);
|
||||
await this.bitmap.unset(keyB);
|
||||
|
||||
// [C]
|
||||
|
||||
await this.bitmap.set(keyB);
|
||||
|
||||
// [C, B]
|
||||
|
||||
await this.bitmap.set(keyA);
|
||||
await this.bitmap.unset(keyC);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.bitmap.set(keyA);
|
||||
await this.bitmap.set(keyB);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.bitmap.set(keyC);
|
||||
await this.bitmap.unset(keyA);
|
||||
|
||||
// [B, C]
|
||||
|
||||
await this.bitmap.set(keyA);
|
||||
await this.bitmap.unset(keyB);
|
||||
|
||||
// [A, C]
|
||||
|
||||
expect(await this.bitmap.get(keyA)).to.equal(true);
|
||||
expect(await this.bitmap.get(keyB)).to.equal(false);
|
||||
expect(await this.bitmap.get(keyC)).to.equal(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
181
test/utils/structs/EnumerableMap.test.js
Normal file
181
test/utils/structs/EnumerableMap.test.js
Normal file
@ -0,0 +1,181 @@
|
||||
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const zip = require('lodash.zip');
|
||||
|
||||
const EnumerableMapMock = artifacts.require('EnumerableMapMock');
|
||||
|
||||
contract('EnumerableMap', function (accounts) {
|
||||
const [ accountA, accountB, accountC ] = accounts;
|
||||
|
||||
const keyA = new BN('7891');
|
||||
const keyB = new BN('451');
|
||||
const keyC = new BN('9592328');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.map = await EnumerableMapMock.new();
|
||||
});
|
||||
|
||||
async function expectMembersMatch (map, keys, values) {
|
||||
expect(keys.length).to.equal(values.length);
|
||||
|
||||
await Promise.all(keys.map(async key =>
|
||||
expect(await map.contains(key)).to.equal(true),
|
||||
));
|
||||
|
||||
expect(await map.length()).to.bignumber.equal(keys.length.toString());
|
||||
|
||||
expect(await Promise.all(keys.map(key =>
|
||||
map.get(key),
|
||||
))).to.have.same.members(values);
|
||||
|
||||
// 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 map.at(index);
|
||||
return [entry.key.toString(), entry.value];
|
||||
}))).to.have.same.deep.members(
|
||||
zip(keys.map(k => k.toString()), values),
|
||||
);
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
expect(await this.map.contains(keyA)).to.equal(false);
|
||||
|
||||
await expectMembersMatch(this.map, [], []);
|
||||
});
|
||||
|
||||
describe('set', function () {
|
||||
it('adds a key', async function () {
|
||||
const receipt = await this.map.set(keyA, accountA);
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [accountA]);
|
||||
});
|
||||
|
||||
it('adds several keys', async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.set(keyB, accountB);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA, keyB], [accountA, accountB]);
|
||||
expect(await this.map.contains(keyC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('returns false when adding keys already in the set', async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
|
||||
const receipt = (await this.map.set(keyA, accountA));
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [accountA]);
|
||||
});
|
||||
|
||||
it('updates values for keys already in the set', async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
|
||||
await this.map.set(keyA, accountB);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [accountB]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', function () {
|
||||
it('removes added keys', async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
|
||||
const receipt = await this.map.remove(keyA);
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
expect(await this.map.contains(keyA)).to.equal(false);
|
||||
await expectMembersMatch(this.map, [], []);
|
||||
});
|
||||
|
||||
it('returns false when removing keys not in the set', async function () {
|
||||
const receipt = await this.map.remove(keyA);
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
expect(await this.map.contains(keyA)).to.equal(false);
|
||||
});
|
||||
|
||||
it('adds and removes multiple keys', async function () {
|
||||
// []
|
||||
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.set(keyC, accountC);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await this.map.remove(keyA);
|
||||
await this.map.remove(keyB);
|
||||
|
||||
// [C]
|
||||
|
||||
await this.map.set(keyB, accountB);
|
||||
|
||||
// [C, B]
|
||||
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.remove(keyC);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.set(keyB, accountB);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.map.set(keyC, accountC);
|
||||
await this.map.remove(keyA);
|
||||
|
||||
// [B, C]
|
||||
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.remove(keyB);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await expectMembersMatch(this.map, [keyA, keyC], [accountA, accountC]);
|
||||
|
||||
expect(await this.map.contains(keyB)).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('read', function () {
|
||||
beforeEach(async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
it('existing value', async function () {
|
||||
expect(await this.map.get(keyA)).to.be.equal(accountA);
|
||||
});
|
||||
it('missing value', async function () {
|
||||
await expectRevert(this.map.get(keyB), 'EnumerableMap: nonexistent key');
|
||||
});
|
||||
});
|
||||
|
||||
describe('get with message', function () {
|
||||
it('existing value', async function () {
|
||||
expect(await this.map.getWithMessage(keyA, 'custom error string')).to.be.equal(accountA);
|
||||
});
|
||||
it('missing value', async function () {
|
||||
await expectRevert(this.map.getWithMessage(keyB, 'custom error string'), 'custom error string');
|
||||
});
|
||||
});
|
||||
|
||||
describe('tryGet', function () {
|
||||
it('existing value', async function () {
|
||||
expect(await this.map.tryGet(keyA)).to.be.deep.equal({
|
||||
0: true,
|
||||
1: accountA,
|
||||
});
|
||||
});
|
||||
it('missing value', async function () {
|
||||
expect(await this.map.tryGet(keyB)).to.be.deep.equal({
|
||||
0: false,
|
||||
1: constants.ZERO_ADDRESS,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
131
test/utils/structs/EnumerableSet.behavior.js
Normal file
131
test/utils/structs/EnumerableSet.behavior.js
Normal file
@ -0,0 +1,131 @@
|
||||
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
function shouldBehaveLikeSet (valueA, valueB, valueC) {
|
||||
async function expectMembersMatch (set, values) {
|
||||
const contains = await Promise.all(values.map(value => set.contains(value)));
|
||||
expect(contains.every(Boolean)).to.be.equal(true);
|
||||
|
||||
const length = await set.length();
|
||||
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) => set.at(index)));
|
||||
expect(
|
||||
indexedValues.map(v => v.toString()),
|
||||
).to.have.same.members(
|
||||
values.map(v => v.toString()),
|
||||
);
|
||||
|
||||
const returnedValues = await set.values();
|
||||
expect(
|
||||
returnedValues.map(v => v.toString()),
|
||||
).to.have.same.members(
|
||||
values.map(v => v.toString()),
|
||||
);
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
expect(await this.set.contains(valueA)).to.equal(false);
|
||||
|
||||
await expectMembersMatch(this.set, []);
|
||||
});
|
||||
|
||||
describe('add', function () {
|
||||
it('adds a value', async function () {
|
||||
const receipt = await this.set.add(valueA);
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
await expectMembersMatch(this.set, [valueA]);
|
||||
});
|
||||
|
||||
it('adds several values', async function () {
|
||||
await this.set.add(valueA);
|
||||
await this.set.add(valueB);
|
||||
|
||||
await expectMembersMatch(this.set, [valueA, valueB]);
|
||||
expect(await this.set.contains(valueC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('returns false when adding values already in the set', async function () {
|
||||
await this.set.add(valueA);
|
||||
|
||||
const receipt = (await this.set.add(valueA));
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
await expectMembersMatch(this.set, [valueA]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('at', function () {
|
||||
it('reverts when retrieving non-existent elements', async function () {
|
||||
await expectRevert.unspecified(this.set.at(0));
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', function () {
|
||||
it('removes added values', async function () {
|
||||
await this.set.add(valueA);
|
||||
|
||||
const receipt = await this.set.remove(valueA);
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
expect(await this.set.contains(valueA)).to.equal(false);
|
||||
await expectMembersMatch(this.set, []);
|
||||
});
|
||||
|
||||
it('returns false when removing values not in the set', async function () {
|
||||
const receipt = await this.set.remove(valueA);
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
expect(await this.set.contains(valueA)).to.equal(false);
|
||||
});
|
||||
|
||||
it('adds and removes multiple values', async function () {
|
||||
// []
|
||||
|
||||
await this.set.add(valueA);
|
||||
await this.set.add(valueC);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await this.set.remove(valueA);
|
||||
await this.set.remove(valueB);
|
||||
|
||||
// [C]
|
||||
|
||||
await this.set.add(valueB);
|
||||
|
||||
// [C, B]
|
||||
|
||||
await this.set.add(valueA);
|
||||
await this.set.remove(valueC);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.set.add(valueA);
|
||||
await this.set.add(valueB);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.set.add(valueC);
|
||||
await this.set.remove(valueA);
|
||||
|
||||
// [B, C]
|
||||
|
||||
await this.set.add(valueA);
|
||||
await this.set.remove(valueB);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await expectMembersMatch(this.set, [valueA, valueC]);
|
||||
|
||||
expect(await this.set.contains(valueB)).to.equal(false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeSet,
|
||||
};
|
||||
46
test/utils/structs/EnumerableSet.test.js
Normal file
46
test/utils/structs/EnumerableSet.test.js
Normal file
@ -0,0 +1,46 @@
|
||||
const { BN } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const EnumerableBytes32SetMock = artifacts.require('EnumerableBytes32SetMock');
|
||||
const EnumerableAddressSetMock = artifacts.require('EnumerableAddressSetMock');
|
||||
const EnumerableUintSetMock = artifacts.require('EnumerableUintSetMock');
|
||||
|
||||
const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
|
||||
|
||||
contract('EnumerableSet', function (accounts) {
|
||||
// Bytes32Set
|
||||
describe('EnumerableBytes32Set', function () {
|
||||
const bytesA = '0xdeadbeef'.padEnd(66, '0');
|
||||
const bytesB = '0x0123456789'.padEnd(66, '0');
|
||||
const bytesC = '0x42424242'.padEnd(66, '0');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.set = await EnumerableBytes32SetMock.new();
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet(bytesA, bytesB, bytesC);
|
||||
});
|
||||
|
||||
// AddressSet
|
||||
describe('EnumerableAddressSet', function () {
|
||||
const [accountA, accountB, accountC] = accounts;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.set = await EnumerableAddressSetMock.new();
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet(accountA, accountB, accountC);
|
||||
});
|
||||
|
||||
// UintSet
|
||||
describe('EnumerableUintSet', function () {
|
||||
const uintA = new BN('1234');
|
||||
const uintB = new BN('5678');
|
||||
const uintC = new BN('9101112');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.set = await EnumerableUintSetMock.new();
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet(uintA, uintB, uintC);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user