EnumerableMap.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. keys: '$keys_EnumerableMap_AddressToUintMap(uint256)',
  35. }),
  36. {
  37. setReturn: 'return$set_EnumerableMap_AddressToUintMap_address_uint256',
  38. removeReturn: 'return$remove_EnumerableMap_AddressToUintMap_address',
  39. },
  40. );
  41. });
  42. // UintToAddressMap
  43. describe('UintToAddressMap', function () {
  44. shouldBehaveLikeMap(
  45. [ keyA, keyB, keyC ],
  46. [ accountA, accountB, accountC ],
  47. constants.ZERO_ADDRESS,
  48. getMethods({
  49. set: '$set(uint256,uint256,address)',
  50. get: '$get_EnumerableMap_UintToAddressMap(uint256,uint256)',
  51. getWithMessage: '$get_EnumerableMap_UintToAddressMap(uint256,uint256,string)',
  52. tryGet: '$tryGet_EnumerableMap_UintToAddressMap(uint256,uint256)',
  53. remove: '$remove_EnumerableMap_UintToAddressMap(uint256,uint256)',
  54. length: '$length_EnumerableMap_UintToAddressMap(uint256)',
  55. at: '$at_EnumerableMap_UintToAddressMap(uint256,uint256)',
  56. contains: '$contains_EnumerableMap_UintToAddressMap(uint256,uint256)',
  57. keys: '$keys_EnumerableMap_UintToAddressMap(uint256)',
  58. }),
  59. {
  60. setReturn: 'return$set_EnumerableMap_UintToAddressMap_uint256_address',
  61. removeReturn: 'return$remove_EnumerableMap_UintToAddressMap_uint256',
  62. },
  63. );
  64. });
  65. // Bytes32ToBytes32Map
  66. describe('Bytes32ToBytes32Map', function () {
  67. shouldBehaveLikeMap(
  68. [ keyA, keyB, keyC ].map(k => '0x' + k.toString(16).padEnd(64, '0')),
  69. [ bytesA, bytesB, bytesC ],
  70. constants.ZERO_BYTES32,
  71. getMethods({
  72. set: '$set(uint256,bytes32,bytes32)',
  73. get: '$get_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  74. getWithMessage: '$get_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32,string)',
  75. tryGet: '$tryGet_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  76. remove: '$remove_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  77. length: '$length_EnumerableMap_Bytes32ToBytes32Map(uint256)',
  78. at: '$at_EnumerableMap_Bytes32ToBytes32Map(uint256,uint256)',
  79. contains: '$contains_EnumerableMap_Bytes32ToBytes32Map(uint256,bytes32)',
  80. keys: '$keys_EnumerableMap_Bytes32ToBytes32Map(uint256)',
  81. }),
  82. {
  83. setReturn: 'return$set_EnumerableMap_Bytes32ToBytes32Map_bytes32_bytes32',
  84. removeReturn: 'return$remove_EnumerableMap_Bytes32ToBytes32Map_bytes32',
  85. },
  86. );
  87. });
  88. // UintToUintMap
  89. describe('UintToUintMap', function () {
  90. shouldBehaveLikeMap(
  91. [ keyA, keyB, keyC ],
  92. [ keyA, keyB, keyC ].map(k => k.add(new BN('1332'))),
  93. new BN('0'),
  94. getMethods({
  95. set: '$set(uint256,uint256,uint256)',
  96. get: '$get_EnumerableMap_UintToUintMap(uint256,uint256)',
  97. getWithMessage: '$get_EnumerableMap_UintToUintMap(uint256,uint256,string)',
  98. tryGet: '$tryGet_EnumerableMap_UintToUintMap(uint256,uint256)',
  99. remove: '$remove_EnumerableMap_UintToUintMap(uint256,uint256)',
  100. length: '$length_EnumerableMap_UintToUintMap(uint256)',
  101. at: '$at_EnumerableMap_UintToUintMap(uint256,uint256)',
  102. contains: '$contains_EnumerableMap_UintToUintMap(uint256,uint256)',
  103. keys: '$keys_EnumerableMap_UintToUintMap(uint256)',
  104. }),
  105. {
  106. setReturn: 'return$set_EnumerableMap_UintToUintMap_uint256_uint256',
  107. removeReturn: 'return$remove_EnumerableMap_UintToUintMap_uint256',
  108. },
  109. );
  110. });
  111. // Bytes32ToUintMap
  112. describe('Bytes32ToUintMap', function () {
  113. shouldBehaveLikeMap(
  114. [ bytesA, bytesB, bytesC ],
  115. [ keyA, keyB, keyC ],
  116. new BN('0'),
  117. getMethods({
  118. set: '$set(uint256,bytes32,uint256)',
  119. get: '$get_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  120. getWithMessage: '$get_EnumerableMap_Bytes32ToUintMap(uint256,bytes32,string)',
  121. tryGet: '$tryGet_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  122. remove: '$remove_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  123. length: '$length_EnumerableMap_Bytes32ToUintMap(uint256)',
  124. at: '$at_EnumerableMap_Bytes32ToUintMap(uint256,uint256)',
  125. contains: '$contains_EnumerableMap_Bytes32ToUintMap(uint256,bytes32)',
  126. keys: '$keys_EnumerableMap_Bytes32ToUintMap(uint256)',
  127. }),
  128. {
  129. setReturn: 'return$set_EnumerableMap_Bytes32ToUintMap_bytes32_uint256',
  130. removeReturn: 'return$remove_EnumerableMap_Bytes32ToUintMap_bytes32',
  131. },
  132. );
  133. });
  134. });