Skip to content

Commit 2879b67

Browse files
committed
refactor: optimize loop incrementing in DiamondTester tests using unchecked for consistency
1 parent 4da0b14 commit 2879b67

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

test/DiamondTester.t.sol

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,77 @@ contract DiamondTester is DeployedDiamondState {
2323
/// @dev Expects exactly 3 standard facets: DiamondCut, DiamondLoupe, and OwnableRoles.
2424
function testStandardFacetsDeployed() public view {
2525
assertEq(facetAddresses.length, 3);
26-
for (uint256 i; i < facetAddresses.length; i++) {
26+
for (uint256 i; i < facetAddresses.length;) {
2727
assertNotEq(address(facetAddresses[i]), address(0));
28+
unchecked {
29+
++i;
30+
}
2831
}
2932
}
3033

3134
/// @notice Ensures all function selectors are registered correctly for each facet.
3235
/// @dev Compares generated selectors with those registered in the diamond via facetAddress().
3336
function testSelectorsAreComplete() public {
34-
for (uint256 i; i < facetAddresses.length; i++) {
37+
for (uint256 i; i < facetAddresses.length;) {
3538
bytes4[] memory fromGenSelectors = _generateSelectors(facetNames[i]);
36-
for (uint256 j; j < fromGenSelectors.length; j++) {
39+
for (uint256 j; j < fromGenSelectors.length;) {
3740
assertEq(facetAddresses[i], diamondLoupe.facetAddress(fromGenSelectors[j]));
41+
unchecked {
42+
++j;
43+
}
44+
}
45+
unchecked {
46+
++i;
3847
}
3948
}
4049
}
4150

4251
/// @notice Asserts that all function selectors across all facets are unique.
4352
function testSelectorsAreUnique() public view {
4453
bytes4[] memory allSelectors = getAllSelectors(address(diamond));
45-
for (uint256 i; i < allSelectors.length; i++) {
46-
for (uint256 j = i + 1; j < allSelectors.length; j++) {
54+
for (uint256 i; i < allSelectors.length;) {
55+
for (uint256 j = i + 1; j < allSelectors.length;) {
4756
assertNotEq(allSelectors[i], allSelectors[j]);
57+
unchecked {
58+
++j;
59+
}
60+
}
61+
unchecked {
62+
++i;
4863
}
4964
}
5065
}
5166

5267
/// @notice Ensures each selector maps back to the correct facet.
5368
function testSelectorToFacetMappingIsCorrect() public view {
5469
Facet[] memory facetsList = diamondLoupe.facets();
55-
for (uint256 i; i < facetsList.length; i++) {
56-
for (uint256 j; j < facetsList[i].functionSelectors.length; j++) {
70+
for (uint256 i; i < facetsList.length;) {
71+
for (uint256 j; j < facetsList[i].functionSelectors.length;) {
5772
bytes4 selector = facetsList[i].functionSelectors[j];
5873
address expected = facetsList[i].facetAddress;
5974
assertEq(diamondLoupe.facetAddress(selector), expected);
75+
unchecked {
76+
++j;
77+
}
78+
}
79+
unchecked {
80+
++i;
6081
}
6182
}
6283
}
6384

6485
/// @notice Ensures facet addresses return the correct function selectors.
6586
function testFacetAddressToSelectorsMappingIsCorrect() public view {
66-
for (uint256 i; i < facetAddresses.length; i++) {
87+
for (uint256 i; i < facetAddresses.length;) {
6788
bytes4[] memory selectors = diamondLoupe.facetFunctionSelectors(facetAddresses[i]);
68-
for (uint256 j; j < selectors.length; j++) {
89+
for (uint256 j; j < selectors.length;) {
6990
assertEq(diamondLoupe.facetAddress(selectors[j]), facetAddresses[i]);
91+
unchecked {
92+
++j;
93+
}
94+
}
95+
unchecked {
96+
++i;
7097
}
7198
}
7299
}

0 commit comments

Comments
 (0)