Checkpoints.t.js 4.5 KB

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