EnumerableMap.test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const { ethers } = require('hardhat');
  2. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  3. const { mapValues } = require('../../helpers/iterate');
  4. const { randomArray, generators } = require('../../helpers/random');
  5. const { shouldBehaveLikeMap } = require('./EnumerableMap.behavior');
  6. const getMethods = (mock, fnSigs) => {
  7. return mapValues(
  8. fnSigs,
  9. fnSig =>
  10. (...args) =>
  11. mock.getFunction(fnSig)(0, ...args),
  12. );
  13. };
  14. describe('EnumerableMap', function () {
  15. // UintToAddressMap
  16. describe('UintToAddressMap', function () {
  17. const fixture = async () => {
  18. const mock = await ethers.deployContract('$EnumerableMap');
  19. const [keyA, keyB, keyC] = randomArray(generators.uint256);
  20. const [valueA, valueB, valueC] = randomArray(generators.address);
  21. const methods = getMethods(mock, {
  22. set: '$set(uint256,uint256,address)',
  23. get: '$get_EnumerableMap_UintToAddressMap(uint256,uint256)',
  24. tryGet: '$tryGet_EnumerableMap_UintToAddressMap(uint256,uint256)',
  25. remove: '$remove_EnumerableMap_UintToAddressMap(uint256,uint256)',
  26. length: '$length_EnumerableMap_UintToAddressMap(uint256)',
  27. at: '$at_EnumerableMap_UintToAddressMap(uint256,uint256)',
  28. contains: '$contains_EnumerableMap_UintToAddressMap(uint256,uint256)',
  29. keys: '$keys_EnumerableMap_UintToAddressMap(uint256)',
  30. });
  31. return { mock, keyA, keyB, keyC, valueA, valueB, valueC, methods };
  32. };
  33. beforeEach(async function () {
  34. Object.assign(this, await loadFixture(fixture));
  35. });
  36. shouldBehaveLikeMap(ethers.ZeroAddress, 'uint256', {
  37. setReturn: 'return$set_EnumerableMap_UintToAddressMap_uint256_address',
  38. removeReturn: 'return$remove_EnumerableMap_UintToAddressMap_uint256',
  39. });
  40. });
  41. // Bytes32ToBytes32Map
  42. describe('Bytes32ToBytes32Map', function () {
  43. const fixture = async () => {
  44. const mock = await ethers.deployContract('$EnumerableMap');
  45. const [keyA, keyB, keyC] = randomArray(generators.bytes32);
  46. const [valueA, valueB, valueC] = randomArray(generators.bytes32);
  47. const methods = getMethods(mock, {
  48. set: '$set(uint256,bytes32,bytes32)',
  49. get: '$get_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  50. tryGet: '$tryGet_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  51. remove: '$remove_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  52. length: '$length_EnumerableMap_Bytes32ToBytes32Map(uint256)',
  53. at: '$at_EnumerableMap_Bytes32ToBytes32Map(uint256,uint256)',
  54. contains: '$contains_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  55. keys: '$keys_EnumerableMap_Bytes32ToBytes32Map(uint256)',
  56. });
  57. return { mock, keyA, keyB, keyC, valueA, valueB, valueC, methods };
  58. };
  59. beforeEach(async function () {
  60. Object.assign(this, await loadFixture(fixture));
  61. });
  62. shouldBehaveLikeMap(ethers.ZeroHash, 'bytes32', {
  63. setReturn: 'return$set_EnumerableMap_Bytes32ToBytes32Map_bytes32_bytes32',
  64. removeReturn: 'return$remove_EnumerableMap_Bytes32ToBytes32Map_bytes32',
  65. });
  66. });
  67. // UintToUintMap
  68. describe('UintToUintMap', function () {
  69. const fixture = async () => {
  70. const mock = await ethers.deployContract('$EnumerableMap');
  71. const [keyA, keyB, keyC] = randomArray(generators.uint256);
  72. const [valueA, valueB, valueC] = randomArray(generators.uint256);
  73. const methods = getMethods(mock, {
  74. set: '$set(uint256,uint256,uint256)',
  75. get: '$get_EnumerableMap_UintToUintMap(uint256,uint256)',
  76. tryGet: '$tryGet_EnumerableMap_UintToUintMap(uint256,uint256)',
  77. remove: '$remove_EnumerableMap_UintToUintMap(uint256,uint256)',
  78. length: '$length_EnumerableMap_UintToUintMap(uint256)',
  79. at: '$at_EnumerableMap_UintToUintMap(uint256,uint256)',
  80. contains: '$contains_EnumerableMap_UintToUintMap(uint256,uint256)',
  81. keys: '$keys_EnumerableMap_UintToUintMap(uint256)',
  82. });
  83. return { mock, keyA, keyB, keyC, valueA, valueB, valueC, methods };
  84. };
  85. beforeEach(async function () {
  86. Object.assign(this, await loadFixture(fixture));
  87. });
  88. shouldBehaveLikeMap(0n, 'uint256', {
  89. setReturn: 'return$set_EnumerableMap_UintToUintMap_uint256_uint256',
  90. removeReturn: 'return$remove_EnumerableMap_UintToUintMap_uint256',
  91. });
  92. });
  93. // Bytes32ToUintMap
  94. describe('Bytes32ToUintMap', function () {
  95. const fixture = async () => {
  96. const mock = await ethers.deployContract('$EnumerableMap');
  97. const [keyA, keyB, keyC] = randomArray(generators.bytes32);
  98. const [valueA, valueB, valueC] = randomArray(generators.uint256);
  99. const methods = getMethods(mock, {
  100. set: '$set(uint256,bytes32,uint256)',
  101. get: '$get_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  102. tryGet: '$tryGet_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  103. remove: '$remove_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  104. length: '$length_EnumerableMap_Bytes32ToUintMap(uint256)',
  105. at: '$at_EnumerableMap_Bytes32ToUintMap(uint256,uint256)',
  106. contains: '$contains_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  107. keys: '$keys_EnumerableMap_Bytes32ToUintMap(uint256)',
  108. });
  109. return { mock, keyA, keyB, keyC, valueA, valueB, valueC, methods };
  110. };
  111. beforeEach(async function () {
  112. Object.assign(this, await loadFixture(fixture));
  113. });
  114. shouldBehaveLikeMap(0n, 'bytes32', {
  115. setReturn: 'return$set_EnumerableMap_Bytes32ToUintMap_bytes32_uint256',
  116. removeReturn: 'return$remove_EnumerableMap_Bytes32ToUintMap_bytes32',
  117. });
  118. });
  119. });