11import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' ;
2- import { bnToBytes32 } from '@solidstate/library' ;
2+ import { bnToBytes32 , bnToAddress } from '@solidstate/library' ;
33import {
44 EnumerableSetBytes32Mock ,
55 EnumerableSetBytes32Mock__factory ,
6+ EnumerableSetAddressMock ,
7+ EnumerableSetAddressMock__factory ,
68 EnumerableSetUintMock ,
79 EnumerableSetUintMock__factory ,
810} from '@solidstate/typechain-types' ;
911import { expect } from 'chai' ;
12+ import { BigNumber } from 'ethers' ;
1013import { ethers } from 'hardhat' ;
1114
1215describe ( 'EnumerableSet' , async ( ) => {
@@ -198,6 +201,194 @@ describe('EnumerableSet', async () => {
198201 } ) ;
199202 } ) ;
200203
204+ describe ( 'AddressSet' , async ( ) => {
205+ let instance : EnumerableSetAddressMock ;
206+ let deployer : SignerWithAddress ;
207+
208+ beforeEach ( async ( ) => {
209+ [ deployer ] = await ethers . getSigners ( ) ;
210+ instance = await new EnumerableSetAddressMock__factory ( deployer ) . deploy ( ) ;
211+ } ) ;
212+
213+ describe ( '__internal' , ( ) => {
214+ const zeroAddress = bnToAddress ( BigNumber . from ( 0 ) ) ;
215+ const oneAddress = bnToAddress ( BigNumber . from ( 1 ) ) ;
216+ const twoAddress = bnToAddress ( BigNumber . from ( 2 ) ) ;
217+
218+ describe ( '#at(uint256)' , ( ) => {
219+ it ( 'returns the value corresponding to index provided' , async ( ) => {
220+ await instance [ 'add(address)' ] ( zeroAddress ) ;
221+ await instance [ 'add(address)' ] ( twoAddress ) ;
222+ await instance [ 'add(address)' ] ( oneAddress ) ;
223+
224+ expect ( await instance [ 'at(uint256)' ] ( 0 ) ) . to . equal ( zeroAddress ) ;
225+ expect ( await instance [ 'at(uint256)' ] ( 1 ) ) . to . equal ( twoAddress ) ;
226+ expect ( await instance [ 'at(uint256)' ] ( 2 ) ) . to . equal ( oneAddress ) ;
227+ } ) ;
228+
229+ describe ( 'reverts if' , function ( ) {
230+ it ( 'index out of bounds' , async ( ) => {
231+ await expect (
232+ instance [ 'at(uint256)' ] ( 0 ) ,
233+ ) . to . be . revertedWithCustomError (
234+ instance ,
235+ 'EnumerableSet__IndexOutOfBounds' ,
236+ ) ;
237+ } ) ;
238+ } ) ;
239+ } ) ;
240+
241+ describe ( '#contains(address)' , ( ) => {
242+ it ( 'returns true if the value has been added' , async ( ) => {
243+ await instance [ 'add(address)' ] ( zeroAddress ) ;
244+ await instance [ 'add(address)' ] ( twoAddress ) ;
245+ await instance [ 'add(address)' ] ( oneAddress ) ;
246+
247+ expect ( await instance [ 'contains(address)' ] ( zeroAddress ) ) . to . be . true ;
248+ expect ( await instance [ 'contains(address)' ] ( oneAddress ) ) . to . be . true ;
249+ expect ( await instance [ 'contains(address)' ] ( twoAddress ) ) . to . be . true ;
250+ } ) ;
251+
252+ it ( 'returns false if the value has not been added' , async ( ) => {
253+ expect ( await instance [ 'contains(address)' ] ( zeroAddress ) ) . to . be . false ;
254+ } ) ;
255+ } ) ;
256+
257+ describe ( '#indexOf(address)' , ( ) => {
258+ it ( 'returns index of the value' , async ( ) => {
259+ await instance [ 'add(address)' ] ( zeroAddress ) ;
260+ await instance [ 'add(address)' ] ( twoAddress ) ;
261+ await instance [ 'add(address)' ] ( oneAddress ) ;
262+
263+ expect ( await instance [ 'indexOf(address)' ] ( zeroAddress ) ) . to . equal ( 0 ) ;
264+ expect ( await instance [ 'indexOf(address)' ] ( oneAddress ) ) . to . equal ( 2 ) ;
265+ expect ( await instance [ 'indexOf(address)' ] ( twoAddress ) ) . to . equal ( 1 ) ;
266+ } ) ;
267+
268+ it ( 'returns max address if value does not exist' , async ( ) => {
269+ expect ( await instance [ 'indexOf(address)' ] ( zeroAddress ) ) . to . equal (
270+ ethers . constants . MaxUint256 . toString ( ) ,
271+ ) ;
272+ } ) ;
273+ } ) ;
274+
275+ describe ( '#length()' , ( ) => {
276+ it ( 'returns length of enumerable set' , async ( ) => {
277+ expect ( await instance [ 'length()' ] ( ) ) . to . equal ( 0 ) ;
278+
279+ await instance [ 'add(address)' ] ( zeroAddress ) ;
280+ expect ( await instance [ 'length()' ] ( ) ) . to . equal ( 1 ) ;
281+
282+ await instance [ 'add(address)' ] ( oneAddress ) ;
283+ expect ( await instance [ 'length()' ] ( ) ) . to . equal ( 2 ) ;
284+
285+ await instance [ 'add(address)' ] ( twoAddress ) ;
286+ expect ( await instance [ 'length()' ] ( ) ) . to . equal ( 3 ) ;
287+
288+ await instance [ 'remove(address)' ] ( twoAddress ) ;
289+ expect ( await instance [ 'length()' ] ( ) ) . to . equal ( 2 ) ;
290+
291+ await instance [ 'remove(address)' ] ( oneAddress ) ;
292+ expect ( await instance [ 'length()' ] ( ) ) . to . equal ( 1 ) ;
293+
294+ await instance [ 'remove(address)' ] ( zeroAddress ) ;
295+ expect ( await instance [ 'length()' ] ( ) ) . to . equal ( 0 ) ;
296+ } ) ;
297+ } ) ;
298+
299+ describe ( '#add(address)' , ( ) => {
300+ it ( 'adds value to set' , async ( ) => {
301+ await instance [ 'add(address)' ] ( zeroAddress ) ;
302+ await instance [ 'add(address)' ] ( oneAddress ) ;
303+ await instance [ 'add(address)' ] ( twoAddress ) ;
304+
305+ expect ( await instance [ 'toArray()' ] ( ) ) . to . deep . equal ( [
306+ zeroAddress ,
307+ oneAddress ,
308+ twoAddress ,
309+ ] ) ;
310+ } ) ;
311+
312+ it ( 'returns true if value is added' , async ( ) => {
313+ expect ( await instance . callStatic [ 'add(address)' ] ( zeroAddress ) ) . to . be
314+ . true ;
315+ } ) ;
316+
317+ it ( 'returns false if value has already been added' , async ( ) => {
318+ await instance [ 'add(address)' ] ( zeroAddress ) ;
319+ expect ( await instance . callStatic [ 'add(address)' ] ( zeroAddress ) ) . to . be
320+ . false ;
321+ } ) ;
322+ } ) ;
323+
324+ describe ( '#remove(address)' , ( ) => {
325+ it ( 'removes value from set' , async ( ) => {
326+ await instance [ 'add(address)' ] ( zeroAddress ) ;
327+ await instance [ 'add(address)' ] ( oneAddress ) ;
328+ await instance [ 'add(address)' ] ( twoAddress ) ;
329+
330+ await instance [ 'remove(address)' ] ( zeroAddress ) ;
331+ expect ( await instance [ 'toArray()' ] ( ) ) . to . deep . equal ( [
332+ twoAddress ,
333+ oneAddress ,
334+ ] ) ;
335+ } ) ;
336+
337+ it ( 'returns true if value is removed' , async ( ) => {
338+ await instance [ 'add(address)' ] ( zeroAddress ) ;
339+ await instance [ 'add(address)' ] ( oneAddress ) ;
340+ await instance [ 'add(address)' ] ( twoAddress ) ;
341+
342+ expect ( await instance . callStatic [ 'remove(address)' ] ( zeroAddress ) ) . to
343+ . be . true ;
344+ } ) ;
345+
346+ it ( 'returns false if value is not removed' , async ( ) => {
347+ expect ( await instance . callStatic [ 'remove(address)' ] ( zeroAddress ) ) . to
348+ . be . false ;
349+ } ) ;
350+
351+ it ( 'removes value from index mapping and array' , async ( ) => {
352+ await instance [ 'add(address)' ] ( zeroAddress ) ;
353+ await instance [ 'add(address)' ] ( oneAddress ) ;
354+ await instance [ 'add(address)' ] ( twoAddress ) ;
355+
356+ await instance [ 'remove(address)' ] ( twoAddress ) ;
357+ expect ( await instance [ 'length()' ] ( ) ) . to . be . equal ( 2 ) ;
358+ expect ( await instance [ 'indexOf(address)' ] ( twoAddress ) ) . to . be . equal (
359+ ethers . constants . MaxUint256 ,
360+ ) ;
361+
362+ await instance [ 'remove(address)' ] ( oneAddress ) ;
363+ expect ( await instance [ 'length()' ] ( ) ) . to . be . equal ( 1 ) ;
364+ expect ( await instance [ 'indexOf(address)' ] ( oneAddress ) ) . to . be . equal (
365+ ethers . constants . MaxUint256 ,
366+ ) ;
367+
368+ await instance [ 'remove(address)' ] ( zeroAddress ) ;
369+ expect ( await instance [ 'length()' ] ( ) ) . to . be . equal ( 0 ) ;
370+ expect ( await instance [ 'indexOf(address)' ] ( zeroAddress ) ) . to . be . equal (
371+ ethers . constants . MaxUint256 ,
372+ ) ;
373+ } ) ;
374+ } ) ;
375+
376+ describe ( '#toArray()' , ( ) => {
377+ it ( 'returns the set as an array' , async ( ) => {
378+ await instance [ 'add(address)' ] ( zeroAddress ) ;
379+ await instance [ 'add(address)' ] ( twoAddress ) ;
380+ await instance [ 'add(address)' ] ( oneAddress ) ;
381+
382+ expect ( await instance [ 'toArray()' ] ( ) ) . to . deep . equal ( [
383+ zeroAddress ,
384+ twoAddress ,
385+ oneAddress ,
386+ ] ) ;
387+ } ) ;
388+ } ) ;
389+ } ) ;
390+ } ) ;
391+
201392 describe ( 'UintSet' , async ( ) => {
202393 let instance : EnumerableSetUintMock ;
203394 let deployer : SignerWithAddress ;
@@ -293,7 +484,7 @@ describe('EnumerableSet', async () => {
293484 } ) ;
294485 } ) ;
295486
296- describe ( '#add(bytes32 )' , ( ) => {
487+ describe ( '#add(uint256 )' , ( ) => {
297488 it ( 'adds value to set' , async ( ) => {
298489 await instance [ 'add(uint256)' ] ( zero ) ;
299490 await instance [ 'add(uint256)' ] ( one ) ;
@@ -312,7 +503,7 @@ describe('EnumerableSet', async () => {
312503 } ) ;
313504 } ) ;
314505
315- describe ( '#remove(bytes32 )' , ( ) => {
506+ describe ( '#remove(uint256 )' , ( ) => {
316507 it ( 'removes value from set' , async ( ) => {
317508 await instance [ 'add(uint256)' ] ( zero ) ;
318509 await instance [ 'add(uint256)' ] ( one ) ;
@@ -359,6 +550,16 @@ describe('EnumerableSet', async () => {
359550 ) ;
360551 } ) ;
361552 } ) ;
553+
554+ describe ( '#toArray()' , ( ) => {
555+ it ( 'returns the set as an array' , async ( ) => {
556+ await instance [ 'add(uint256)' ] ( zero ) ;
557+ await instance [ 'add(uint256)' ] ( two ) ;
558+ await instance [ 'add(uint256)' ] ( one ) ;
559+
560+ expect ( await instance [ 'toArray()' ] ( ) ) . to . deep . equal ( [ zero , two , one ] ) ;
561+ } ) ;
562+ } ) ;
362563 } ) ;
363564 } ) ;
364565} ) ;
0 commit comments