Skip to content

Commit 45b093b

Browse files
committed
uintSet tests
1 parent 89f5253 commit 45b093b

File tree

1 file changed

+176
-7
lines changed

1 file changed

+176
-7
lines changed

test/data/EnumerableSet.ts

Lines changed: 176 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { bnToBytes32 } from '@solidstate/library';
33
import {
44
EnumerableSetBytes32Mock,
55
EnumerableSetBytes32Mock__factory,
6+
EnumerableSetUintMock,
7+
EnumerableSetUintMock__factory,
68
} from '@solidstate/typechain-types';
79
import { expect } from 'chai';
810
import { ethers } from 'hardhat';
@@ -35,8 +37,11 @@ describe('EnumerableSet', async () => {
3537

3638
describe('reverts if', function () {
3739
it('index out of bounds', async () => {
38-
await expect(instance['at(uint256)'](0)).to.be.revertedWith(
39-
'EnumerableSet: index out of bounds',
40+
await expect(
41+
instance['at(uint256)'](0),
42+
).to.be.revertedWithCustomError(
43+
instance,
44+
'EnumerableSet__IndexOutOfBounds',
4045
);
4146
});
4247
});
@@ -119,9 +124,9 @@ describe('EnumerableSet', async () => {
119124
});
120125

121126
it('returns false if value has already been added', async () => {
122-
await instance.callStatic['remove(bytes32)'](zeroBytes32);
123-
expect(await instance.callStatic['remove(bytes32)'](zeroBytes32)).to
124-
.be.false;
127+
await instance['add(bytes32)'](zeroBytes32);
128+
expect(await instance.callStatic['add(bytes32)'](zeroBytes32)).to.be
129+
.false;
125130
});
126131
});
127132

@@ -179,17 +184,181 @@ describe('EnumerableSet', async () => {
179184
describe('#toArray()', () => {
180185
it('returns the set as an array', async () => {
181186
await instance['add(bytes32)'](zeroBytes32);
182-
await instance['add(bytes32)'](oneBytes32);
183187
await instance['add(bytes32)'](twoBytes32);
188+
await instance['add(bytes32)'](oneBytes32);
184189

185190
expect(await instance['toArray()']()).to.deep.equal([
186191
zeroBytes32,
187-
oneBytes32,
188192
twoBytes32,
193+
oneBytes32,
189194
]);
190195
});
191196
});
192197
});
193198
});
194199
});
200+
201+
describe('UintSet', async () => {
202+
let instance: EnumerableSetUintMock;
203+
let deployer: SignerWithAddress;
204+
205+
beforeEach(async () => {
206+
[deployer] = await ethers.getSigners();
207+
instance = await new EnumerableSetUintMock__factory(deployer).deploy();
208+
});
209+
210+
describe('__internal', () => {
211+
const zero = ethers.constants.Zero;
212+
const one = ethers.constants.One;
213+
const two = ethers.constants.Two;
214+
215+
describe('#at(uint256)', () => {
216+
it('returns the value corresponding to index provided', async () => {
217+
await instance['add(uint256)'](zero);
218+
await instance['add(uint256)'](two);
219+
await instance['add(uint256)'](one);
220+
221+
expect(await instance['at(uint256)'](0)).to.equal(zero);
222+
expect(await instance['at(uint256)'](1)).to.equal(two);
223+
expect(await instance['at(uint256)'](2)).to.equal(one);
224+
});
225+
226+
describe('reverts if', function () {
227+
it('index out of bounds', async () => {
228+
await expect(
229+
instance['at(uint256)'](0),
230+
).to.be.revertedWithCustomError(
231+
instance,
232+
'EnumerableSet__IndexOutOfBounds',
233+
);
234+
});
235+
});
236+
});
237+
238+
describe('#contains(uint256)', () => {
239+
it('returns true if the value has been added', async () => {
240+
await instance['add(uint256)'](zero);
241+
await instance['add(uint256)'](two);
242+
await instance['add(uint256)'](one);
243+
244+
expect(await instance['contains(uint256)'](zero)).to.be.true;
245+
expect(await instance['contains(uint256)'](one)).to.be.true;
246+
expect(await instance['contains(uint256)'](two)).to.be.true;
247+
});
248+
249+
it('returns false if the value has not been added', async () => {
250+
expect(await instance['contains(uint256)'](zero)).to.be.false;
251+
});
252+
});
253+
254+
describe('#indexOf(uint256)', () => {
255+
it('returns index of the value', async () => {
256+
await instance['add(uint256)'](zero);
257+
await instance['add(uint256)'](two);
258+
await instance['add(uint256)'](one);
259+
260+
expect(await instance['indexOf(uint256)'](zero)).to.equal(0);
261+
expect(await instance['indexOf(uint256)'](one)).to.equal(2);
262+
expect(await instance['indexOf(uint256)'](two)).to.equal(1);
263+
});
264+
265+
it('returns max uint256 if value does not exist', async () => {
266+
expect(await instance['indexOf(uint256)'](zero)).to.equal(
267+
ethers.constants.MaxUint256.toString(),
268+
);
269+
});
270+
});
271+
272+
describe('#length()', () => {
273+
it('returns length of enumerable set', async () => {
274+
expect(await instance['length()']()).to.equal(0);
275+
276+
await instance['add(uint256)'](zero);
277+
expect(await instance['length()']()).to.equal(1);
278+
279+
await instance['add(uint256)'](one);
280+
expect(await instance['length()']()).to.equal(2);
281+
282+
await instance['add(uint256)'](two);
283+
expect(await instance['length()']()).to.equal(3);
284+
285+
await instance['remove(uint256)'](two);
286+
expect(await instance['length()']()).to.equal(2);
287+
288+
await instance['remove(uint256)'](one);
289+
expect(await instance['length()']()).to.equal(1);
290+
291+
await instance['remove(uint256)'](zero);
292+
expect(await instance['length()']()).to.equal(0);
293+
});
294+
});
295+
296+
describe('#add(bytes32)', () => {
297+
it('adds value to set', async () => {
298+
await instance['add(uint256)'](zero);
299+
await instance['add(uint256)'](one);
300+
await instance['add(uint256)'](two);
301+
302+
expect(await instance['toArray()']()).to.deep.equal([zero, one, two]);
303+
});
304+
305+
it('returns true if value is added', async () => {
306+
expect(await instance.callStatic['add(uint256)'](zero)).to.be.true;
307+
});
308+
309+
it('returns false if value has already been added', async () => {
310+
await instance['add(uint256)'](zero);
311+
expect(await instance.callStatic['add(uint256)'](zero)).to.be.false;
312+
});
313+
});
314+
315+
describe('#remove(bytes32)', () => {
316+
it('removes value from set', async () => {
317+
await instance['add(uint256)'](zero);
318+
await instance['add(uint256)'](one);
319+
await instance['add(uint256)'](two);
320+
321+
await instance['remove(uint256)'](zero);
322+
expect(await instance['toArray()']()).to.deep.equal([two, one]);
323+
});
324+
325+
it('returns true if value is removed', async () => {
326+
await instance['add(uint256)'](zero);
327+
await instance['add(uint256)'](one);
328+
await instance['add(uint256)'](two);
329+
330+
expect(await instance.callStatic['remove(uint256)'](zero)).to.be.true;
331+
});
332+
333+
it('returns false if value is not removed', async () => {
334+
expect(await instance.callStatic['remove(uint256)'](zero)).to.be
335+
.false;
336+
});
337+
338+
it('removes value from index mapping and array', async () => {
339+
await instance['add(uint256)'](zero);
340+
await instance['add(uint256)'](one);
341+
await instance['add(uint256)'](two);
342+
343+
await instance['remove(uint256)'](two);
344+
expect(await instance['length()']()).to.be.equal(2);
345+
expect(await instance['indexOf(uint256)'](two)).to.be.equal(
346+
ethers.constants.MaxUint256,
347+
);
348+
349+
await instance['remove(uint256)'](one);
350+
expect(await instance['length()']()).to.be.equal(1);
351+
expect(await instance['indexOf(uint256)'](one)).to.be.equal(
352+
ethers.constants.MaxUint256,
353+
);
354+
355+
await instance['remove(uint256)'](zero);
356+
expect(await instance['length()']()).to.be.equal(0);
357+
expect(await instance['indexOf(uint256)'](zero)).to.be.equal(
358+
ethers.constants.MaxUint256,
359+
);
360+
});
361+
});
362+
});
363+
});
195364
});

0 commit comments

Comments
 (0)