Checkpoints.t.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const format = require('../format-lines');
  2. const { capitalize } = require('../../helpers');
  3. const { OPTS } = require('./Checkpoints.opts.js');
  4. // TEMPLATE
  5. const header = `\
  6. pragma solidity ^0.8.20;
  7. import {Test} from "forge-std/Test.sol";
  8. import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
  9. import {Checkpoints} from "@openzeppelin/contracts/utils/structs/Checkpoints.sol";
  10. `;
  11. /* eslint-disable max-len */
  12. const template = opts => `\
  13. using Checkpoints for Checkpoints.${opts.historyTypeName};
  14. // Maximum gap between keys used during the fuzzing tests: the \`_prepareKeys\` function with make sure that
  15. // key#n+1 is in the [key#n, key#n + _KEY_MAX_GAP] range.
  16. uint8 internal constant _KEY_MAX_GAP = 64;
  17. Checkpoints.${opts.historyTypeName} internal _ckpts;
  18. // helpers
  19. function _bound${capitalize(opts.keyTypeName)}(${opts.keyTypeName} x, ${opts.keyTypeName} min, ${
  20. opts.keyTypeName
  21. } max) internal pure returns (${opts.keyTypeName}) {
  22. return SafeCast.to${capitalize(opts.keyTypeName)}(bound(uint256(x), uint256(min), uint256(max)));
  23. }
  24. function _prepareKeys(${opts.keyTypeName}[] memory keys, ${opts.keyTypeName} maxSpread) internal pure {
  25. ${opts.keyTypeName} lastKey = 0;
  26. for (uint256 i = 0; i < keys.length; ++i) {
  27. ${opts.keyTypeName} key = _bound${capitalize(opts.keyTypeName)}(keys[i], lastKey, lastKey + maxSpread);
  28. keys[i] = key;
  29. lastKey = key;
  30. }
  31. }
  32. function _assertLatestCheckpoint(bool exist, ${opts.keyTypeName} key, ${opts.valueTypeName} value) internal {
  33. (bool _exist, ${opts.keyTypeName} _key, ${opts.valueTypeName} _value) = _ckpts.latestCheckpoint();
  34. assertEq(_exist, exist);
  35. assertEq(_key, key);
  36. assertEq(_value, value);
  37. }
  38. // tests
  39. function testPush(${opts.keyTypeName}[] memory keys, ${opts.valueTypeName}[] memory values, ${
  40. opts.keyTypeName
  41. } pastKey) public {
  42. vm.assume(values.length > 0 && values.length <= keys.length);
  43. _prepareKeys(keys, _KEY_MAX_GAP);
  44. // initial state
  45. assertEq(_ckpts.length(), 0);
  46. assertEq(_ckpts.latest(), 0);
  47. _assertLatestCheckpoint(false, 0, 0);
  48. uint256 duplicates = 0;
  49. for (uint256 i = 0; i < keys.length; ++i) {
  50. ${opts.keyTypeName} key = keys[i];
  51. ${opts.valueTypeName} value = values[i % values.length];
  52. if (i > 0 && key == keys[i - 1]) ++duplicates;
  53. // push
  54. _ckpts.push(key, value);
  55. // check length & latest
  56. assertEq(_ckpts.length(), i + 1 - duplicates);
  57. assertEq(_ckpts.latest(), value);
  58. _assertLatestCheckpoint(true, key, value);
  59. }
  60. if (keys.length > 0) {
  61. ${opts.keyTypeName} lastKey = keys[keys.length - 1];
  62. if (lastKey > 0) {
  63. pastKey = _bound${capitalize(opts.keyTypeName)}(pastKey, 0, lastKey - 1);
  64. vm.expectRevert();
  65. this.push(pastKey, values[keys.length % values.length]);
  66. }
  67. }
  68. }
  69. // used to test reverts
  70. function push(${opts.keyTypeName} key, ${opts.valueTypeName} value) external {
  71. _ckpts.push(key, value);
  72. }
  73. function testLookup(${opts.keyTypeName}[] memory keys, ${opts.valueTypeName}[] memory values, ${
  74. opts.keyTypeName
  75. } lookup) public {
  76. vm.assume(values.length > 0 && values.length <= keys.length);
  77. _prepareKeys(keys, _KEY_MAX_GAP);
  78. ${opts.keyTypeName} lastKey = keys.length == 0 ? 0 : keys[keys.length - 1];
  79. lookup = _bound${capitalize(opts.keyTypeName)}(lookup, 0, lastKey + _KEY_MAX_GAP);
  80. ${opts.valueTypeName} upper = 0;
  81. ${opts.valueTypeName} lower = 0;
  82. ${opts.keyTypeName} lowerKey = type(${opts.keyTypeName}).max;
  83. for (uint256 i = 0; i < keys.length; ++i) {
  84. ${opts.keyTypeName} key = keys[i];
  85. ${opts.valueTypeName} value = values[i % values.length];
  86. // push
  87. _ckpts.push(key, value);
  88. // track expected result of lookups
  89. if (key <= lookup) {
  90. upper = value;
  91. }
  92. // find the first key that is not smaller than the lookup key
  93. if (key >= lookup && (i == 0 || keys[i - 1] < lookup)) {
  94. lowerKey = key;
  95. }
  96. if (key == lowerKey) {
  97. lower = value;
  98. }
  99. }
  100. // check lookup
  101. assertEq(_ckpts.lowerLookup(lookup), lower);
  102. assertEq(_ckpts.upperLookup(lookup), upper);
  103. assertEq(_ckpts.upperLookupRecent(lookup), upper);
  104. }
  105. `;
  106. // GENERATE
  107. module.exports = format(
  108. header,
  109. ...OPTS.flatMap(opts => [
  110. `contract Checkpoints${opts.historyTypeName}Test is Test {`,
  111. [template(opts).trimEnd()],
  112. '}',
  113. '',
  114. ]),
  115. );