|
1 | 1 | import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; |
2 | | -import { bnToBytes32, bnToAddress } from '@solidstate/library'; |
| 2 | +import { bnToAddress } from '@solidstate/library'; |
3 | 3 | import { |
4 | 4 | EnumerableMapAddressToAddressMock, |
5 | 5 | EnumerableMapAddressToAddressMock__factory, |
| 6 | + EnumerableMapUintToAddressMock, |
| 7 | + EnumerableMapUintToAddressMock__factory, |
6 | 8 | } from '@solidstate/typechain-types'; |
7 | 9 | import { expect } from 'chai'; |
8 | 10 | import { BigNumber } from 'ethers'; |
9 | 11 | import { ethers } from 'hardhat'; |
10 | 12 |
|
11 | | -describe('EnumerableMap', async () => { |
| 13 | +describe('EnumerableMap', () => { |
12 | 14 | describe('AddressToAddressMap', async () => { |
13 | 15 | let instance: EnumerableMapAddressToAddressMock; |
14 | 16 | let deployer: SignerWithAddress; |
@@ -200,4 +202,203 @@ describe('EnumerableMap', async () => { |
200 | 202 | }); |
201 | 203 | }); |
202 | 204 | }); |
| 205 | + |
| 206 | + describe('UintToAddressMap', async () => { |
| 207 | + let instance: EnumerableMapUintToAddressMock; |
| 208 | + let deployer: SignerWithAddress; |
| 209 | + |
| 210 | + beforeEach(async () => { |
| 211 | + [deployer] = await ethers.getSigners(); |
| 212 | + instance = await new EnumerableMapUintToAddressMock__factory( |
| 213 | + deployer, |
| 214 | + ).deploy(); |
| 215 | + }); |
| 216 | + |
| 217 | + describe('__internal', () => { |
| 218 | + const addressOne = bnToAddress(BigNumber.from('100')); |
| 219 | + const addressTwo = bnToAddress(BigNumber.from('200')); |
| 220 | + const addressThree = bnToAddress(BigNumber.from('300')); |
| 221 | + const uintOne = BigNumber.from('1'); |
| 222 | + const uintTwo = BigNumber.from('2'); |
| 223 | + const uintThree = BigNumber.from('3'); |
| 224 | + |
| 225 | + describe('#at(uint256)', () => { |
| 226 | + it('returns value coresponding to index provided', async () => { |
| 227 | + await instance['set(uint256,address)'](uintOne, addressOne); |
| 228 | + await instance['set(uint256,address)'](uintTwo, addressTwo); |
| 229 | + await instance['set(uint256,address)'](uintThree, addressThree); |
| 230 | + |
| 231 | + expect(await instance['at(uint256)'](0)).to.deep.equal([ |
| 232 | + uintOne, |
| 233 | + addressOne, |
| 234 | + ]); |
| 235 | + expect(await instance['at(uint256)'](1)).to.deep.equal([ |
| 236 | + uintTwo, |
| 237 | + addressTwo, |
| 238 | + ]); |
| 239 | + expect(await instance['at(uint256)'](2)).to.deep.equal([ |
| 240 | + uintThree, |
| 241 | + addressThree, |
| 242 | + ]); |
| 243 | + }); |
| 244 | + |
| 245 | + describe('reverts if', () => { |
| 246 | + it('index is out of bounds', async () => { |
| 247 | + await expect( |
| 248 | + instance['at(uint256)'](0), |
| 249 | + ).to.be.revertedWithCustomError( |
| 250 | + instance, |
| 251 | + 'EnumerableMap__IndexOutOfBounds', |
| 252 | + ); |
| 253 | + }); |
| 254 | + }); |
| 255 | + }); |
| 256 | + |
| 257 | + describe('#contains(uint256)', () => { |
| 258 | + it('returns true if value has been added', async () => { |
| 259 | + await instance['set(uint256,address)'](uintOne, addressOne); |
| 260 | + await instance['set(uint256,address)'](uintTwo, addressTwo); |
| 261 | + await instance['set(uint256,address)'](uintThree, addressThree); |
| 262 | + |
| 263 | + expect(await instance['contains(uint256)'](uintOne)).to.be.true; |
| 264 | + expect(await instance['contains(uint256)'](uintTwo)).to.be.true; |
| 265 | + expect(await instance['contains(uint256)'](uintThree)).to.be.true; |
| 266 | + }); |
| 267 | + |
| 268 | + it('returns false if value has not been added', async () => { |
| 269 | + expect(await instance['contains(uint256)'](uintOne)).to.be.false; |
| 270 | + }); |
| 271 | + }); |
| 272 | + |
| 273 | + describe('#length()', () => { |
| 274 | + it('returns length of enumerable map', async () => { |
| 275 | + expect(await instance['length()']()).to.equal(0); |
| 276 | + |
| 277 | + await instance['set(uint256,address)'](uintOne, addressOne); |
| 278 | + expect(await instance['length()']()).to.equal(1); |
| 279 | + |
| 280 | + await instance['set(uint256,address)'](uintTwo, addressTwo); |
| 281 | + expect(await instance['length()']()).to.equal(2); |
| 282 | + |
| 283 | + await instance['set(uint256,address)'](uintThree, addressThree); |
| 284 | + expect(await instance['length()']()).to.equal(3); |
| 285 | + |
| 286 | + await instance['remove(uint256)'](uintOne); |
| 287 | + expect(await instance['length()']()).to.equal(2); |
| 288 | + |
| 289 | + await instance['remove(uint256)'](uintTwo); |
| 290 | + expect(await instance['length()']()).to.equal(1); |
| 291 | + |
| 292 | + await instance['remove(uint256)'](uintThree); |
| 293 | + expect(await instance['length()']()).to.equal(0); |
| 294 | + }); |
| 295 | + }); |
| 296 | + |
| 297 | + describe('#get(uint256)', () => { |
| 298 | + it('returns address stored at key', async () => { |
| 299 | + await instance['set(uint256,address)'](uintOne, addressOne); |
| 300 | + await instance['set(uint256,address)'](uintTwo, addressTwo); |
| 301 | + await instance['set(uint256,address)'](uintThree, addressThree); |
| 302 | + |
| 303 | + expect(await instance['get(uint256)'](uintOne)).to.eq(addressOne); |
| 304 | + expect(await instance['get(uint256)'](uintTwo)).to.eq(addressTwo); |
| 305 | + expect(await instance['get(uint256)'](uintThree)).to.eq(addressThree); |
| 306 | + }); |
| 307 | + |
| 308 | + describe('reverts if', () => { |
| 309 | + it('key does not exist', async () => { |
| 310 | + await expect( |
| 311 | + instance['get(uint256)'](uintOne), |
| 312 | + ).to.be.revertedWithCustomError( |
| 313 | + instance, |
| 314 | + 'EnumerableMap__NonExistentKey', |
| 315 | + ); |
| 316 | + }); |
| 317 | + }); |
| 318 | + }); |
| 319 | + |
| 320 | + describe('#set(uint256,address)', () => { |
| 321 | + it('sets the address value at uint256 key', async () => { |
| 322 | + await instance['set(uint256,address)'](uintOne, addressOne); |
| 323 | + await instance['set(uint256,address)'](uintTwo, addressTwo); |
| 324 | + await instance['set(uint256,address)'](uintThree, addressThree); |
| 325 | + |
| 326 | + expect(await instance['contains(uint256)'](uintOne)).to.be.true; |
| 327 | + expect(await instance['contains(uint256)'](uintTwo)).to.be.true; |
| 328 | + expect(await instance['contains(uint256)'](uintThree)).to.be.true; |
| 329 | + |
| 330 | + expect(await instance['get(uint256)'](uintOne)).to.eq(addressOne); |
| 331 | + expect(await instance['get(uint256)'](uintTwo)).to.eq(addressTwo); |
| 332 | + expect(await instance['get(uint256)'](uintThree)).to.eq(addressThree); |
| 333 | + }); |
| 334 | + |
| 335 | + it('returns true if address value is added at uint256 key', async () => { |
| 336 | + expect( |
| 337 | + await instance.callStatic['set(uint256,address)']( |
| 338 | + uintOne, |
| 339 | + addressOne, |
| 340 | + ), |
| 341 | + ).to.be.true; |
| 342 | + }); |
| 343 | + |
| 344 | + it('returns false if address value is already added at uint256 key', async () => { |
| 345 | + await instance['set(uint256,address)'](uintOne, addressOne); |
| 346 | + |
| 347 | + expect( |
| 348 | + await instance.callStatic['set(uint256,address)']( |
| 349 | + uintOne, |
| 350 | + addressOne, |
| 351 | + ), |
| 352 | + ).to.be.false; |
| 353 | + }); |
| 354 | + }); |
| 355 | + |
| 356 | + describe('#remove(uint256)', () => { |
| 357 | + it('removes the address value at given uint256 key', async () => { |
| 358 | + await instance['set(uint256,address)'](uintOne, addressOne); |
| 359 | + await instance['set(uint256,address)'](uintTwo, addressTwo); |
| 360 | + await instance['set(uint256,address)'](uintThree, addressThree); |
| 361 | + |
| 362 | + expect(await instance['length()']()).to.eq(3); |
| 363 | + |
| 364 | + await instance['remove(uint256)'](uintOne); |
| 365 | + await expect( |
| 366 | + instance['get(uint256)'](uintOne), |
| 367 | + ).to.be.revertedWithCustomError( |
| 368 | + instance, |
| 369 | + 'EnumerableMap__NonExistentKey', |
| 370 | + ); |
| 371 | + expect(await instance['length()']()).to.eq(2); |
| 372 | + |
| 373 | + await instance['remove(uint256)'](uintTwo); |
| 374 | + await expect( |
| 375 | + instance['get(uint256)'](uintTwo), |
| 376 | + ).to.be.revertedWithCustomError( |
| 377 | + instance, |
| 378 | + 'EnumerableMap__NonExistentKey', |
| 379 | + ); |
| 380 | + expect(await instance['length()']()).to.eq(1); |
| 381 | + |
| 382 | + await instance['remove(uint256)'](uintThree); |
| 383 | + await expect( |
| 384 | + instance['get(uint256)'](uintThree), |
| 385 | + ).to.be.revertedWithCustomError( |
| 386 | + instance, |
| 387 | + 'EnumerableMap__NonExistentKey', |
| 388 | + ); |
| 389 | + expect(await instance['length()']()).to.eq(0); |
| 390 | + }); |
| 391 | + |
| 392 | + it('returns true if uint256 key removed', async () => { |
| 393 | + await instance['set(uint256,address)'](uintOne, addressOne); |
| 394 | + expect(await instance.callStatic['remove(uint256)'](uintOne)).to.be |
| 395 | + .true; |
| 396 | + }); |
| 397 | + it('returns false if uint256 key does not exist', async () => { |
| 398 | + expect(await instance.callStatic['remove(uint256)'](uintOne)).to.be |
| 399 | + .false; |
| 400 | + }); |
| 401 | + }); |
| 402 | + }); |
| 403 | + }); |
203 | 404 | }); |
0 commit comments