Skip to content

Commit

Permalink
♻️ Use _randomChance shorthand for tests (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored Jul 18, 2024
1 parent ba82ec0 commit c0292e8
Show file tree
Hide file tree
Showing 29 changed files with 932 additions and 912 deletions.
1,250 changes: 625 additions & 625 deletions .gas-snapshot

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/Base64.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ contract Base64Test is SoladyTest {
function testBase64EncodeDecodeAltModes(bytes memory input) public brutalizeMemory {
for (uint256 i; i < 2; ++i) {
_misalignFreeMemoryPointer();
if (_random() % 2 == 0) {
if (_randomChance(2)) {
input = _withDirtyEndBits(input);
}
string memory encoded = Base64.encode(input);
Expand Down
10 changes: 5 additions & 5 deletions test/DeploylessPredeployQueryer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ contract DeploylessPredeployQueryerTest is SoladyTest {
bytes[] memory targetQueryCalldata,
bytes memory factoryCalldata
) internal returns (address result) {
if (_random() % 2 == 0) {
if (_randomChance(2)) {
return address(
new DeploylessPredeployQueryer(
target, targetQueryCalldata, address(factory), factoryCalldata
Expand All @@ -111,7 +111,7 @@ contract DeploylessPredeployQueryerTest is SoladyTest {
bytes memory args =
abi.encode(target, targetQueryCalldata, address(factory), factoryCalldata);
bytes memory initcode;
if (false && _random() % 2 == 0) {
if (false && _randomChance(2)) {
initcode = _CREATION_CODE;
} else {
initcode = type(DeploylessPredeployQueryer).creationCode;
Expand All @@ -135,20 +135,20 @@ contract DeploylessPredeployQueryerTest is SoladyTest {
unchecked {
_TestTemps memory t;
t.target = factory.predictDeployment(salt);
if (_random() % 2 == 0) {
if (_randomChance(2)) {
assertEq(factory.deploy(salt), t.target);
}
t.factoryCalldata = abi.encodeWithSignature("deploy(bytes32)", salt);
t.n = _random() % 3;
t.targetQueryCalldata = new bytes[](t.n);
t.seeds = new uint256[](t.n);
t.bytesSeeds = new bytes[](t.n);
if (_random() % 2 == 0) {
if (_randomChance(2)) {
vm.expectRevert(DeploylessPredeployQueryer.ReturnedAddressMismatch.selector);
address wrongTarget = address(uint160(t.target) ^ 1);
t.deployed = _deployQuery(wrongTarget, t.targetQueryCalldata, t.factoryCalldata);
}
if (_random() % 2 == 0) {
if (_randomChance(2)) {
for (uint256 i; i < t.n; ++i) {
t.bytesSeeds[i] = _randomBytes();
t.targetQueryCalldata[i] =
Expand Down
48 changes: 24 additions & 24 deletions test/DynamicBufferLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ contract DynamicBufferLibTest is SoladyTest {
using DynamicBufferLib for DynamicBufferLib.DynamicBuffer;

function testDynamicBufferPushSingles(uint256 x, uint256 y, uint256 z) public {
if (_random() % 32 == 0) _brutalizeMemory();
if (_randomChance(32)) _brutalizeMemory();
DynamicBufferLib.DynamicBuffer memory buffer;
if (_random() % 4 == 0) {
if (_random() % 2 == 0) {
if (_randomChance(4)) {
if (_randomChance(2)) {
DynamicBufferLib.DynamicBuffer memory newBuffer;
buffer = newBuffer;
}
Expand All @@ -20,9 +20,9 @@ contract DynamicBufferLibTest is SoladyTest {
buffer.pUint256(y).pUint256(z);
assertEq(buffer.data, abi.encodePacked(uint256(x), uint256(y), uint256(z)));
}
if (_random() % 32 == 0) _brutalizeMemory();
if (_random() % 4 == 0) {
if (_random() % 2 == 0) {
if (_randomChance(32)) _brutalizeMemory();
if (_randomChance(4)) {
if (_randomChance(2)) {
DynamicBufferLib.DynamicBuffer memory newBuffer;
buffer = newBuffer;
} else {
Expand All @@ -33,9 +33,9 @@ contract DynamicBufferLibTest is SoladyTest {
buffer.pUint32(uint32(y)).pUint32(uint32(z));
assertEq(buffer.data, abi.encodePacked(uint32(x), uint32(y), uint32(z)));
}
if (_random() % 32 == 0) _brutalizeMemory();
if (_random() % 4 == 0) {
if (_random() % 2 == 0) {
if (_randomChance(32)) _brutalizeMemory();
if (_randomChance(4)) {
if (_randomChance(2)) {
DynamicBufferLib.DynamicBuffer memory newBuffer;
buffer = newBuffer;
} else {
Expand All @@ -46,9 +46,9 @@ contract DynamicBufferLibTest is SoladyTest {
buffer.pUint8(uint8(y)).pUint8(uint8(z));
assertEq(buffer.data, abi.encodePacked(uint8(x), uint8(y), uint8(z)));
}
if (_random() % 32 == 0) _brutalizeMemory();
if (_random() % 4 == 0) {
if (_random() % 2 == 0) {
if (_randomChance(32)) _brutalizeMemory();
if (_randomChance(4)) {
if (_randomChance(2)) {
DynamicBufferLib.DynamicBuffer memory newBuffer;
buffer = newBuffer;
} else {
Expand All @@ -59,9 +59,9 @@ contract DynamicBufferLibTest is SoladyTest {
buffer.pBytes32(bytes32(y)).pBytes32(bytes32(z));
assertEq(buffer.data, abi.encodePacked(bytes32(x), bytes32(y), bytes32(z)));
}
if (_random() % 32 == 0) _brutalizeMemory();
if (_random() % 4 == 0) {
if (_random() % 2 == 0) {
if (_randomChance(32)) _brutalizeMemory();
if (_randomChance(4)) {
if (_randomChance(2)) {
DynamicBufferLib.DynamicBuffer memory newBuffer;
buffer = newBuffer;
} else {
Expand All @@ -75,9 +75,9 @@ contract DynamicBufferLibTest is SoladyTest {
abi.encodePacked(bytes3(bytes32(x)), bytes3(bytes32(y)), bytes3(bytes32(z)))
);
}
if (_random() % 32 == 0) _brutalizeMemory();
if (_random() % 4 == 0) {
if (_random() % 2 == 0) {
if (_randomChance(32)) _brutalizeMemory();
if (_randomChance(4)) {
if (_randomChance(2)) {
DynamicBufferLib.DynamicBuffer memory newBuffer;
buffer = newBuffer;
} else {
Expand All @@ -91,9 +91,9 @@ contract DynamicBufferLibTest is SoladyTest {
abi.encodePacked(bytes1(bytes32(x)), bytes1(bytes32(y)), bytes1(bytes32(z)))
);
}
if (_random() % 32 == 0) _brutalizeMemory();
if (_random() % 4 == 0) {
if (_random() % 2 == 0) {
if (_randomChance(32)) _brutalizeMemory();
if (_randomChance(4)) {
if (_randomChance(2)) {
DynamicBufferLib.DynamicBuffer memory newBuffer;
buffer = newBuffer;
} else {
Expand All @@ -104,9 +104,9 @@ contract DynamicBufferLibTest is SoladyTest {
buffer.pBool(y % 2 == 0).pBool(z % 2 == 0);
assertEq(buffer.data, abi.encodePacked(x % 2 == 0, y % 2 == 0, z % 2 == 0));
}
if (_random() % 32 == 0) _brutalizeMemory();
if (_random() % 4 == 0) {
if (_random() % 2 == 0) {
if (_randomChance(32)) _brutalizeMemory();
if (_randomChance(4)) {
if (_randomChance(2)) {
DynamicBufferLib.DynamicBuffer memory newBuffer;
buffer = newBuffer;
} else {
Expand Down
22 changes: 11 additions & 11 deletions test/ERC1155.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ contract ERC1155Test is SoladyTest, ERC1155TokenReceiver {
uint256 amount,
bytes memory data
) internal {
if (_random() % 2 == 0) {
if (_randomChance(2)) {
token.safeTransferFrom(from, to, id, amount, data);
} else {
token.directSafeTransferFrom(from, to, id, amount, data);
Expand All @@ -346,15 +346,15 @@ contract ERC1155Test is SoladyTest, ERC1155TokenReceiver {
uint256[] memory amounts,
bytes memory data
) internal {
if (_random() % 2 == 0) {
if (_randomChance(2)) {
token.safeBatchTransferFrom(from, to, ids, amounts, data);
} else {
token.directSafeBatchTransferFrom(from, to, ids, amounts, data);
}
}

function _setApprovalForAll(address operator, bool approved) internal {
if (_random() % 2 == 0) {
if (_randomChance(2)) {
token.setApprovalForAll(operator, approved);
} else {
token.directSetApprovalForAll(operator, approved);
Expand Down Expand Up @@ -567,10 +567,10 @@ contract ERC1155Test is SoladyTest, ERC1155TokenReceiver {
_expectMintEvent(t.to, t.id, t.mintAmount);
token.mint(t.to, t.id, t.mintAmount, t.mintData);

if (_random() % 2 == 0) {
if (_randomChance(2)) {
_expectBurnEvent(t.to, t.id, t.burnAmount);
token.uncheckedBurn(t.to, t.id, t.burnAmount);
} else if (_random() % 8 == 0) {
} else if (_randomChance(8)) {
vm.expectRevert(ERC1155.NotOwnerNorApproved.selector);
token.burn(t.to, t.id, t.burnAmount);
return;
Expand Down Expand Up @@ -603,10 +603,10 @@ contract ERC1155Test is SoladyTest, ERC1155TokenReceiver {
_expectMintEvent(t.to, t.ids, t.mintAmounts);
token.batchMint(t.to, t.ids, t.mintAmounts, t.mintData);

if (_random() % 2 == 0) {
if (_randomChance(2)) {
_expectBurnEvent(t.to, t.ids, t.burnAmounts);
token.uncheckedBatchBurn(t.to, t.ids, t.burnAmounts);
} else if (_random() % 8 == 0) {
} else if (_randomChance(8)) {
vm.expectRevert(ERC1155.NotOwnerNorApproved.selector);
token.batchBurn(t.to, t.ids, t.burnAmounts);
return;
Expand Down Expand Up @@ -642,10 +642,10 @@ contract ERC1155Test is SoladyTest, ERC1155TokenReceiver {
_expectMintEvent(t.from, t.id, t.mintAmount);
token.mint(t.from, t.id, t.mintAmount, t.mintData);

if (_random() % 2 == 0) {
if (_randomChance(2)) {
_expectTransferEvent(t.from, t.to, t.id, t.transferAmount);
token.uncheckedSafeTransferFrom(t.from, t.to, t.id, t.transferAmount, t.transferData);
} else if (_random() % 8 == 0) {
} else if (_randomChance(8)) {
vm.expectRevert(ERC1155.NotOwnerNorApproved.selector);
_safeTransferFrom(t.from, t.to, t.id, t.transferAmount, t.transferData);
return;
Expand Down Expand Up @@ -731,12 +731,12 @@ contract ERC1155Test is SoladyTest, ERC1155TokenReceiver {
_expectMintEvent(t.from, t.ids, t.mintAmounts);
token.batchMint(t.from, t.ids, t.mintAmounts, t.mintData);

if (_random() % 2 == 0) {
if (_randomChance(2)) {
_expectTransferEvent(t.from, t.to, t.ids, t.transferAmounts);
token.uncheckedSafeBatchTransferFrom(
t.from, t.to, t.ids, t.transferAmounts, t.transferData
);
} else if (_random() % 8 == 0) {
} else if (_randomChance(8)) {
vm.expectRevert(ERC1155.NotOwnerNorApproved.selector);
_safeBatchTransferFrom(t.from, t.to, t.ids, t.transferAmounts, t.transferData);
return;
Expand Down
20 changes: 10 additions & 10 deletions test/ERC1271.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ contract ERC1271Test is SoladyTest {

function _testTempsMint(address owner) internal returns (uint256 tokenId) {
while (true) {
tokenId = _random() % 8 == 0 ? _random() % 32 : _random();
tokenId = _randomChance(8) ? _random() % 32 : _random();
(bool success,) =
_erc721.call(abi.encodeWithSignature("mint(address,uint256)", owner, tokenId));
if (success) return tokenId;
Expand All @@ -128,24 +128,24 @@ contract ERC1271Test is SoladyTest {

function testIsValidSignature(uint256 x) public {
vm.txGasPrice(10);
if (_random() % 8 == 0) {
if (_randomChance(8)) {
_testIsValidSignature(abi.encodePacked(uint8(x)), false);
}
if (_random() % 32 == 0) {
if (_randomChance(32)) {
_etchBasefeeContract();
}
_TestIsValidSignatureTemps memory t;
t.uppercased = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
t.lowercased = "abcdefghijklmnopqrstuvwxyz";
t.rest = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
t.banned = "\x00 ,)";
if (_random() % 4 == 0) {
if (_randomChance(4)) {
bytes memory contentsType = abi.encodePacked(
_randomString(t.uppercased, true), _randomString(t.rest, false), "(bytes32 stuff)"
);
_testIsValidSignature(contentsType, true);
}
if (_random() % 4 == 0) {
if (_randomChance(4)) {
bytes memory contentsType = abi.encodePacked(
_randomString(t.uppercased, false),
_randomString(t.banned, true),
Expand All @@ -154,18 +154,18 @@ contract ERC1271Test is SoladyTest {
);
_testIsValidSignature(contentsType, false);
}
if (_random() % 4 == 0) {
if (_randomChance(4)) {
bytes memory contentsType = abi.encodePacked(
_randomString(t.lowercased, true), _randomString(t.rest, false), "(bytes32 stuff)"
);
_testIsValidSignature(contentsType, false);
}
if (_random() % 4 == 0) {
if (_randomChance(4)) {
bytes memory contentsType =
abi.encodePacked(_randomString(t.uppercased, true), _randomString(t.rest, false));
_testIsValidSignature(contentsType, false);
}
if (_random() % 16 == 0) {
if (_randomChance(16)) {
_testIsValidSignatureWontOutOfGas();
}
}
Expand All @@ -175,7 +175,7 @@ contract ERC1271Test is SoladyTest {
returns (string memory result)
{
uint256 randomness = _random();
uint256 resultLength = _bound(_random(), nonEmpty ? 1 : 0, _random() % 32 != 0 ? 4 : 128);
uint256 resultLength = _bound(_random(), nonEmpty ? 1 : 0, !_randomChance(32) ? 4 : 128);
/// @solidity memory-safe-assembly
assembly {
if mload(byteChoices) {
Expand Down Expand Up @@ -234,7 +234,7 @@ contract ERC1271Test is SoladyTest {
bytes memory signature = abi.encodePacked(
t.r, t.s, t.v, _DOMAIN_SEP_B, contents, contentsType, uint16(contentsType.length)
);
if (_random() % 4 == 0) signature = _erc6492Wrap(signature);
if (_randomChance(4)) signature = _erc6492Wrap(signature);
// Success returns `0x1626ba7e`.
assertEq(
t.account.isValidSignature(_toContentsHash(contents), signature),
Expand Down
2 changes: 1 addition & 1 deletion test/ERC1967Factory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ contract ERC1967FactoryTest is SoladyTest {
bytes memory data = abi.encodeWithSignature("setValue(uint256,uint256)", t.key, t.value);
vm.deal(admin, type(uint128).max);
vm.prank(admin);
if (_random() % 8 == 0) {
if (_randomChance(8)) {
t.salt = keccak256(abi.encode(_random()));
vm.expectRevert(ERC1967Factory.SaltDoesNotStartWithCaller.selector);
t.proxy = factory.deployDeterministicAndCall{value: t.msgValue}(
Expand Down
8 changes: 4 additions & 4 deletions test/ERC2981.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ contract ERC2981Test is SoladyTest {
function testSetAndGetRoyaltyInfo(uint256) public {
_TestTemps memory t = _testTemps();

if (_random() % 16 == 0) _checkReverts(t);
if (_randomChance(16)) _checkReverts(t);

_checkRoyaltyInfoIsZero(t);

Expand All @@ -70,7 +70,7 @@ contract ERC2981Test is SoladyTest {
_checkRoyaltyInfo(t, 0);
_checkRoyaltyInfo(t, 1);

if (_random() % 16 == 0) _checkReverts(t);
if (_randomChance(16)) _checkReverts(t);

token.resetTokenRoyalty(t.tokenIds[0]);
_checkRoyaltyInfoIsDefault(t, 0);
Expand All @@ -79,13 +79,13 @@ contract ERC2981Test is SoladyTest {
_checkRoyaltyInfoIsDefault(t, 0);
_checkRoyaltyInfoIsDefault(t, 1);

if (_random() % 16 == 0) _checkReverts(t);
if (_randomChance(16)) _checkReverts(t);

token.deleteDefaultRoyalty();

_checkRoyaltyInfoIsZero(t);

if (_random() % 16 == 0) _checkReverts(t);
if (_randomChance(16)) _checkReverts(t);
}

function _getInvalidFeeNumerator(_TestTemps memory t) internal returns (uint96 r) {
Expand Down
Loading

0 comments on commit c0292e8

Please sign in to comment.