Skip to content

Commit

Permalink
refactor: return 0 when no greater element
Browse files Browse the repository at this point in the history
  • Loading branch information
detectivekim committed Jan 16, 2024
1 parent 6ec3919 commit f3ceedf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
4 changes: 1 addition & 3 deletions contracts/Heap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ library Heap {
uint256 b1Bitmap = (MAX_UINT_256_MINUS_1 << (b0b1 & 0xff)) & heap[~b0];
if (b1Bitmap == 0) {
uint256 b0Bitmap = (MAX_UINT_256_MINUS_1 << b0) & heap[B0_BITMAP_KEY];
if (b0Bitmap == 0) {
revert EmptyError();
}
if (b0Bitmap == 0) return 0;
b0 = b0Bitmap.leastSignificantBit();
b1Bitmap = heap[~b0];
}
Expand Down
6 changes: 2 additions & 4 deletions test/foundry/Heap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ contract HeapTest is Test {
}
assertTrue(testWrapper.minGreaterThan(elements[i]) == elements[i + 1], "WRONG_MIN");
}
vm.expectRevert(abi.encodeWithSelector(Heap.EmptyError.selector));
testWrapper.minGreaterThan(elements[length - 1]);
assertTrue(testWrapper.minGreaterThan(elements[length - 1]) == 0, "NO_MORE_MIN_VALUE");

assertFalse(testWrapper.isEmpty(), "HAS_TO_BE_OCCUPIED");
while (!testWrapper.isEmpty()) {
_min = testWrapper.root();
assertTrue(testWrapper.has(_min), "HEAP_HAS_ROOT");
uint256 min;
if (length == 1) {
vm.expectRevert(abi.encodeWithSelector(Heap.EmptyError.selector));
testWrapper.minGreaterThan(_min);
assertTrue(testWrapper.minGreaterThan(_min) == 0, "NO_MORE_MIN_VALUE");
} else {
min = testWrapper.minGreaterThan(_min);
}
Expand Down

0 comments on commit f3ceedf

Please sign in to comment.