EnumerableMap.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const { BN, constants } = require('@openzeppelin/test-helpers');
  2. const { mapValues } = require('../../helpers/map-values');
  3. const EnumerableMap = artifacts.require('$EnumerableMap');
  4. const { shouldBehaveLikeMap } = require('./EnumerableMap.behavior');
  5. const getMethods = (ms) => {
  6. return mapValues(ms, m => (self, ...args) => self.methods[m](0, ...args));
  7. };
  8. contract('EnumerableMap', function (accounts) {
  9. const [ accountA, accountB, accountC ] = accounts;
  10. const keyA = new BN('7891');
  11. const keyB = new BN('451');
  12. const keyC = new BN('9592328');
  13. const bytesA = '0xdeadbeef'.padEnd(66, '0');
  14. const bytesB = '0x0123456789'.padEnd(66, '0');
  15. const bytesC = '0x42424242'.padEnd(66, '0');
  16. beforeEach(async function () {
  17. this.map = await EnumerableMap.new();
  18. });
  19. // AddressToUintMap
  20. describe('AddressToUintMap', function () {
  21. shouldBehaveLikeMap(
  22. [ accountA, accountB, accountC ],
  23. [ keyA, keyB, keyC ],
  24. new BN('0'),
  25. getMethods({
  26. set: '$set(uint256,address,uint256)',
  27. get: '$get(uint256,address)',
  28. getWithMessage: '$get(uint256,address,string)',
  29. tryGet: '$tryGet(uint256,address)',
  30. remove: '$remove(uint256,address)',
  31. length: '$length_EnumerableMap_AddressToUintMap(uint256)',
  32. at: '$at_EnumerableMap_AddressToUintMap(uint256,uint256)',
  33. contains: '$contains(uint256,address)',
  34. }),
  35. {
  36. setReturn: 'return$set_EnumerableMap_AddressToUintMap_address_uint256',
  37. removeReturn: 'return$remove_EnumerableMap_AddressToUintMap_address',
  38. },
  39. );
  40. });
  41. // UintToAddressMap
  42. describe('UintToAddressMap', function () {
  43. shouldBehaveLikeMap(
  44. [ keyA, keyB, keyC ],
  45. [ accountA, accountB, accountC ],
  46. constants.ZERO_ADDRESS,
  47. getMethods({
  48. set: '$set(uint256,uint256,address)',
  49. get: '$get_EnumerableMap_UintToAddressMap(uint256,uint256)',
  50. getWithMessage: '$get_EnumerableMap_UintToAddressMap(uint256,uint256,string)',
  51. tryGet: '$tryGet_EnumerableMap_UintToAddressMap(uint256,uint256)',
  52. remove: '$remove_EnumerableMap_UintToAddressMap(uint256,uint256)',
  53. length: '$length_EnumerableMap_UintToAddressMap(uint256)',
  54. at: '$at_EnumerableMap_UintToAddressMap(uint256,uint256)',
  55. contains: '$contains_EnumerableMap_UintToAddressMap(uint256,uint256)',
  56. }),
  57. {
  58. setReturn: 'return$set_EnumerableMap_UintToAddressMap_uint256_address',
  59. removeReturn: 'return$remove_EnumerableMap_UintToAddressMap_uint256',
  60. },
  61. );
  62. });
  63. // Bytes32ToBytes32Map
  64. describe('Bytes32ToBytes32Map', function () {
  65. shouldBehaveLikeMap(
  66. [ keyA, keyB, keyC ].map(k => '0x' + k.toString(16).padEnd(64, '0')),
  67. [ bytesA, bytesB, bytesC ],
  68. constants.ZERO_BYTES32,
  69. getMethods({
  70. set: '$set(uint256,bytes32,bytes32)',
  71. get: '$get_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  72. getWithMessage: '$get_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32,string)',
  73. tryGet: '$tryGet_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  74. remove: '$remove_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  75. length: '$length_EnumerableMap_Bytes32ToBytes32Map(uint256)',
  76. at: '$at_EnumerableMap_Bytes32ToBytes32Map(uint256,uint256)',
  77. contains: '$contains_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  78. }),
  79. {
  80. setReturn: 'return$set_EnumerableMap_Bytes32ToBytes32Map_bytes32_bytes32',
  81. removeReturn: 'return$remove_EnumerableMap_Bytes32ToBytes32Map_bytes32',
  82. },
  83. );
  84. });
  85. // UintToUintMap
  86. describe('UintToUintMap', function () {
  87. shouldBehaveLikeMap(
  88. [ keyA, keyB, keyC ],
  89. [ keyA, keyB, keyC ].map(k => k.add(new BN('1332'))),
  90. new BN('0'),
  91. getMethods({
  92. set: '$set(uint256,uint256,uint256)',
  93. get: '$get_EnumerableMap_UintToUintMap(uint256,uint256)',
  94. getWithMessage: '$get_EnumerableMap_UintToUintMap(uint256,uint256,string)',
  95. tryGet: '$tryGet_EnumerableMap_UintToUintMap(uint256,uint256)',
  96. remove: '$remove_EnumerableMap_UintToUintMap(uint256,uint256)',
  97. length: '$length_EnumerableMap_UintToUintMap(uint256)',
  98. at: '$at_EnumerableMap_UintToUintMap(uint256,uint256)',
  99. contains: '$contains_EnumerableMap_UintToUintMap(uint256,uint256)',
  100. }),
  101. {
  102. setReturn: 'return$set_EnumerableMap_UintToUintMap_uint256_uint256',
  103. removeReturn: 'return$remove_EnumerableMap_UintToUintMap_uint256',
  104. },
  105. );
  106. });
  107. // Bytes32ToUintMap
  108. describe('Bytes32ToUintMap', function () {
  109. shouldBehaveLikeMap(
  110. [ bytesA, bytesB, bytesC ],
  111. [ keyA, keyB, keyC ],
  112. new BN('0'),
  113. getMethods({
  114. set: '$set(uint256,bytes32,uint256)',
  115. get: '$get_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  116. getWithMessage: '$get_EnumerableMap_Bytes32ToUintMap(uint256,bytes32,string)',
  117. tryGet: '$tryGet_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  118. remove: '$remove_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  119. length: '$length_EnumerableMap_Bytes32ToUintMap(uint256)',
  120. at: '$at_EnumerableMap_Bytes32ToUintMap(uint256,uint256)',
  121. contains: '$contains_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  122. }),
  123. {
  124. setReturn: 'return$set_EnumerableMap_Bytes32ToUintMap_bytes32_uint256',
  125. removeReturn: 'return$remove_EnumerableMap_Bytes32ToUintMap_bytes32',
  126. },
  127. );
  128. });
  129. });