Heap.t.sol 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: MIT
  2. // This file was procedurally generated from scripts/generate/templates/Heap.t.js.
  3. pragma solidity ^0.8.20;
  4. import {Test} from "forge-std/Test.sol";
  5. import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
  6. import {Heap} from "@openzeppelin/contracts/utils/structs/Heap.sol";
  7. import {Comparators} from "@openzeppelin/contracts/utils/Comparators.sol";
  8. contract Uint256HeapTest is Test {
  9. using Heap for Heap.Uint256Heap;
  10. Heap.Uint256Heap internal heap;
  11. function _validateHeap(function(uint256, uint256) view returns (bool) comp) internal {
  12. for (uint32 i = 0; i < heap.length(); ++i) {
  13. // lookups
  14. assertEq(i, heap.data[heap.data[i].index].lookup);
  15. assertEq(i, heap.data[heap.data[i].lookup].index);
  16. // ordering: each node has a value bigger then its parent
  17. if (i > 0)
  18. assertFalse(comp(heap.data[heap.data[i].index].value, heap.data[heap.data[(i - 1) / 2].index].value));
  19. }
  20. }
  21. function testFuzz(uint256[] calldata input) public {
  22. vm.assume(input.length < 0x20);
  23. assertEq(heap.length(), 0);
  24. uint256 min = type(uint256).max;
  25. for (uint256 i = 0; i < input.length; ++i) {
  26. heap.insert(input[i]);
  27. assertEq(heap.length(), i + 1);
  28. _validateHeap(Comparators.lt);
  29. min = Math.min(min, input[i]);
  30. assertEq(heap.peek(), min);
  31. }
  32. uint256 max = 0;
  33. for (uint256 i = 0; i < input.length; ++i) {
  34. uint256 top = heap.peek();
  35. uint256 pop = heap.pop();
  36. assertEq(heap.length(), input.length - i - 1);
  37. _validateHeap(Comparators.lt);
  38. assertEq(pop, top);
  39. assertGe(pop, max);
  40. max = pop;
  41. }
  42. }
  43. function testFuzzGt(uint256[] calldata input) public {
  44. vm.assume(input.length < 0x20);
  45. assertEq(heap.length(), 0);
  46. uint256 max = 0;
  47. for (uint256 i = 0; i < input.length; ++i) {
  48. heap.insert(input[i], Comparators.gt);
  49. assertEq(heap.length(), i + 1);
  50. _validateHeap(Comparators.gt);
  51. max = Math.max(max, input[i]);
  52. assertEq(heap.peek(), max);
  53. }
  54. uint256 min = type(uint256).max;
  55. for (uint256 i = 0; i < input.length; ++i) {
  56. uint256 top = heap.peek();
  57. uint256 pop = heap.pop(Comparators.gt);
  58. assertEq(heap.length(), input.length - i - 1);
  59. _validateHeap(Comparators.gt);
  60. assertEq(pop, top);
  61. assertLe(pop, min);
  62. min = pop;
  63. }
  64. }
  65. }
  66. contract Uint208HeapTest is Test {
  67. using Heap for Heap.Uint208Heap;
  68. Heap.Uint208Heap internal heap;
  69. function _validateHeap(function(uint256, uint256) view returns (bool) comp) internal {
  70. for (uint32 i = 0; i < heap.length(); ++i) {
  71. // lookups
  72. assertEq(i, heap.data[heap.data[i].index].lookup);
  73. assertEq(i, heap.data[heap.data[i].lookup].index);
  74. // ordering: each node has a value bigger then its parent
  75. if (i > 0)
  76. assertFalse(comp(heap.data[heap.data[i].index].value, heap.data[heap.data[(i - 1) / 2].index].value));
  77. }
  78. }
  79. function testFuzz(uint208[] calldata input) public {
  80. vm.assume(input.length < 0x20);
  81. assertEq(heap.length(), 0);
  82. uint256 min = type(uint256).max;
  83. for (uint256 i = 0; i < input.length; ++i) {
  84. heap.insert(input[i]);
  85. assertEq(heap.length(), i + 1);
  86. _validateHeap(Comparators.lt);
  87. min = Math.min(min, input[i]);
  88. assertEq(heap.peek(), min);
  89. }
  90. uint256 max = 0;
  91. for (uint256 i = 0; i < input.length; ++i) {
  92. uint208 top = heap.peek();
  93. uint208 pop = heap.pop();
  94. assertEq(heap.length(), input.length - i - 1);
  95. _validateHeap(Comparators.lt);
  96. assertEq(pop, top);
  97. assertGe(pop, max);
  98. max = pop;
  99. }
  100. }
  101. function testFuzzGt(uint208[] calldata input) public {
  102. vm.assume(input.length < 0x20);
  103. assertEq(heap.length(), 0);
  104. uint256 max = 0;
  105. for (uint256 i = 0; i < input.length; ++i) {
  106. heap.insert(input[i], Comparators.gt);
  107. assertEq(heap.length(), i + 1);
  108. _validateHeap(Comparators.gt);
  109. max = Math.max(max, input[i]);
  110. assertEq(heap.peek(), max);
  111. }
  112. uint256 min = type(uint256).max;
  113. for (uint256 i = 0; i < input.length; ++i) {
  114. uint208 top = heap.peek();
  115. uint208 pop = heap.pop(Comparators.gt);
  116. assertEq(heap.length(), input.length - i - 1);
  117. _validateHeap(Comparators.gt);
  118. assertEq(pop, top);
  119. assertLe(pop, min);
  120. min = pop;
  121. }
  122. }
  123. }