Skip to content

Commit

Permalink
Add equivalence test for _wrapNFTId
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Jun 3, 2024
1 parent 80b38f3 commit a427683
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
29 changes: 15 additions & 14 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,28 @@ DN404Test:test__codesize() (gas: 61620)
DN404ZeroIndexedTest:testBatchNFTLog() (gas: 326053)
DN404ZeroIndexedTest:testBurnOnTransfer(uint32,address) (runs: 270, μ: 255124, ~: 255124)
DN404ZeroIndexedTest:testFnSelectorNotRecognized() (gas: 6268)
DN404ZeroIndexedTest:testInitialize(uint32,address) (runs: 272, μ: 109068, ~: 113108)
DN404ZeroIndexedTest:testInitialize(uint32,address) (runs: 272, μ: 109048, ~: 113108)
DN404ZeroIndexedTest:testMintAndBurn() (gas: 343030)
DN404ZeroIndexedTest:testMintAndBurn2() (gas: 280268)
DN404ZeroIndexedTest:testMintAndBurn2() (gas: 280290)
DN404ZeroIndexedTest:testMintNext() (gas: 701766)
DN404ZeroIndexedTest:testMintNextContiguous(uint256) (runs: 272, μ: 559796, ~: 516666)
DN404ZeroIndexedTest:testMintOnTransfer(uint32,address) (runs: 270, μ: 269705, ~: 269705)
DN404ZeroIndexedTest:testMixed(bytes32) (runs: 272, μ: 577183, ~: 498226)
DN404ZeroIndexedTest:testNameAndSymbol(string,string) (runs: 272, μ: 205773, ~: 206176)
DN404ZeroIndexedTest:testMintNextContiguous(uint256) (runs: 272, μ: 568584, ~: 524801)
DN404ZeroIndexedTest:testMintOnTransfer(uint32,address) (runs: 270, μ: 269727, ~: 269727)
DN404ZeroIndexedTest:testMixed(bytes32) (runs: 272, μ: 564213, ~: 505534)
DN404ZeroIndexedTest:testNameAndSymbol(string,string) (runs: 272, μ: 205728, ~: 206131)
DN404ZeroIndexedTest:testNumAliasesOverflowReverts() (gas: 40944)
DN404ZeroIndexedTest:testOwnedIds() (gas: 347336)
DN404ZeroIndexedTest:testOwnedIds(uint256) (runs: 272, μ: 261162, ~: 284431)
DN404ZeroIndexedTest:testPermit2() (gas: 455270)
DN404ZeroIndexedTest:testRegisterAndResolveAlias(address,address) (runs: 272, μ: 120044, ~: 120191)
DN404ZeroIndexedTest:testSetAndGetAux(address,uint88) (runs: 272, μ: 21987, ~: 22272)
DN404ZeroIndexedTest:testOwnedIds(uint256) (runs: 272, μ: 263381, ~: 286216)
DN404ZeroIndexedTest:testPermit2() (gas: 455292)
DN404ZeroIndexedTest:testRegisterAndResolveAlias(address,address) (runs: 272, μ: 120191, ~: 120191)
DN404ZeroIndexedTest:testSetAndGetAux(address,uint88) (runs: 272, μ: 22004, ~: 22289)
DN404ZeroIndexedTest:testSetAndGetOperatorApprovals(address,address,bool) (runs: 272, μ: 129643, ~: 119840)
DN404ZeroIndexedTest:testSetAndGetSkipNFT() (gas: 708368)
DN404ZeroIndexedTest:testTokenURI(string,uint256) (runs: 272, μ: 156264, ~: 156411)
DN404ZeroIndexedTest:testSetAndGetSkipNFT() (gas: 708390)
DN404ZeroIndexedTest:testTokenURI(string,uint256) (runs: 272, μ: 156286, ~: 156433)
DN404ZeroIndexedTest:testTransferWithMirrorEvent() (gas: 409821)
DN404ZeroIndexedTest:testTransfersAndBurns() (gas: 485767)
DN404ZeroIndexedTest:testWrapAround(uint32,uint256) (runs: 272, μ: 380944, ~: 388042)
DN404ZeroIndexedTest:test__codesize() (gas: 61169)
DN404ZeroIndexedTest:testWrapAround(uint32,uint256) (runs: 272, μ: 380053, ~: 387951)
DN404ZeroIndexedTest:testWrapNFTIdEquivalence(uint256,uint256,bool) (runs: 272, μ: 1379, ~: 1418)
DN404ZeroIndexedTest:test__codesize() (gas: 61415)
DN420OnlyERC20Test:testApprove() (gas: 35770)
DN420OnlyERC20Test:testApprove(address,uint256) (runs: 272, μ: 30882, ~: 31321)
DN420OnlyERC20Test:testBurn() (gas: 49034)
Expand Down
36 changes: 36 additions & 0 deletions test/DN404ZeroIndexed.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,40 @@ contract DN404ZeroIndexedTest is SoladyTest {
}
}
}

function testWrapNFTIdEquivalence(uint256 id, uint256 idLimit, bool useOneIndexed) public {
id = _bound(id, 0, 0xffff);
idLimit = _bound(idLimit, 0, 0xff);
// if (useOneIndexed) while (id == 0) id = _random();
uint256 original = _wrapNFTIdOrigina(id, idLimit, useOneIndexed);
assertEq(_wrapNFTId(id, idLimit, useOneIndexed), original);
}

function _wrapNFTIdOrigina(uint256 id, uint256 idLimit, bool useOneIndexed)
internal
pure
returns (uint256)
{
if (useOneIndexed) {
return id <= idLimit ? id : 1;
} else {
return id < idLimit ? id : 0;
}
}

function _wrapNFTId(uint256 id, uint256 idLimit, bool useOneIndexed)
internal
pure
returns (uint256 result)
{
result = useOneIndexed ? 1 : 0;
/// @solidity memory-safe-assembly
assembly {
result :=
or(
mul(or(mul(iszero(gt(id, idLimit)), id), gt(id, idLimit)), result),
mul(mul(lt(id, idLimit), id), iszero(result))
)
}
}
}

0 comments on commit a427683

Please sign in to comment.