Skip to content

Commit

Permalink
♻️ Fix tests (#1357)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored Feb 17, 2025
1 parent 5dd6f93 commit 30a0de6
Show file tree
Hide file tree
Showing 12 changed files with 994 additions and 324 deletions.
108 changes: 90 additions & 18 deletions test/DateTimeLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -543,48 +543,120 @@ contract DateTimeLibTest is SoladyTest {
assertTrue(a.hour == b.hour && a.minute == b.minute && a.second == b.second);
}

function addYears(uint256 timestamp, uint256 numYears) public pure returns (uint256) {
return DateTimeLib.addYears(timestamp, numYears);
}

function subYears(uint256 timestamp, uint256 numYears) public pure returns (uint256) {
return DateTimeLib.subYears(timestamp, numYears);
}

function diffYears(uint256 timestamp, uint256 numYears) public pure returns (uint256) {
return DateTimeLib.diffYears(timestamp, numYears);
}

function addMonths(uint256 timestamp, uint256 numMonths) public pure returns (uint256) {
return DateTimeLib.addMonths(timestamp, numMonths);
}

function subMonths(uint256 timestamp, uint256 numMonths) public pure returns (uint256) {
return DateTimeLib.subMonths(timestamp, numMonths);
}

function diffMonths(uint256 timestamp, uint256 numMonths) public pure returns (uint256) {
return DateTimeLib.diffMonths(timestamp, numMonths);
}

function addDays(uint256 timestamp, uint256 numDays) public pure returns (uint256) {
return DateTimeLib.addDays(timestamp, numDays);
}

function subDays(uint256 timestamp, uint256 numDays) public pure returns (uint256) {
return DateTimeLib.subDays(timestamp, numDays);
}

function diffDays(uint256 timestamp, uint256 numDays) public pure returns (uint256) {
return DateTimeLib.diffDays(timestamp, numDays);
}

function addHours(uint256 timestamp, uint256 numHours) public pure returns (uint256) {
return DateTimeLib.addHours(timestamp, numHours);
}

function subHours(uint256 timestamp, uint256 numHours) public pure returns (uint256) {
return DateTimeLib.subHours(timestamp, numHours);
}

function diffHours(uint256 timestamp, uint256 numHours) public pure returns (uint256) {
return DateTimeLib.diffHours(timestamp, numHours);
}

function addMinutes(uint256 timestamp, uint256 numMinutes) public pure returns (uint256) {
return DateTimeLib.addMinutes(timestamp, numMinutes);
}

function subMinutes(uint256 timestamp, uint256 numMinutes) public pure returns (uint256) {
return DateTimeLib.subMinutes(timestamp, numMinutes);
}

function diffMinutes(uint256 timestamp, uint256 numMinutes) public pure returns (uint256) {
return DateTimeLib.diffMinutes(timestamp, numMinutes);
}

function addSeconds(uint256 timestamp, uint256 numSeconds) public pure returns (uint256) {
return DateTimeLib.addSeconds(timestamp, numSeconds);
}

function subSeconds(uint256 timestamp, uint256 numSeconds) public pure returns (uint256) {
return DateTimeLib.subSeconds(timestamp, numSeconds);
}

function diffSeconds(uint256 timestamp, uint256 numSeconds) public pure returns (uint256) {
return DateTimeLib.diffSeconds(timestamp, numSeconds);
}

function testDateTimeArithmeticReverts() public {
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.addYears(2 ** 128 - 1, 2 ** 255 - 1);
this.addYears(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.subYears(2 ** 128 - 1, 2 ** 255 - 1);
this.subYears(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.diffYears(2 ** 128 - 1, 2 ** 127 - 1);
this.diffYears(2 ** 128 - 1, 2 ** 127 - 1);

vm.expectRevert(stdError.arithmeticError);
DateTimeLib.addMonths(2 ** 128 - 1, 2 ** 255 - 1);
this.addMonths(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.subMonths(2 ** 128 - 1, 2 ** 255 - 1);
this.subMonths(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.diffMonths(2 ** 128 - 1, 2 ** 127 - 1);
this.diffMonths(2 ** 128 - 1, 2 ** 127 - 1);

vm.expectRevert(stdError.arithmeticError);
DateTimeLib.addDays(2 ** 128 - 1, 2 ** 255 - 1);
this.addDays(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.subDays(2 ** 128 - 1, 2 ** 255 - 1);
this.subDays(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.diffDays(2 ** 128 - 1, 2 ** 127 - 1);
this.diffDays(2 ** 128 - 1, 2 ** 127 - 1);

vm.expectRevert(stdError.arithmeticError);
DateTimeLib.addHours(2 ** 128 - 1, 2 ** 255 - 1);
this.addHours(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.subHours(2 ** 128 - 1, 2 ** 255 - 1);
this.subHours(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.diffHours(2 ** 128 - 1, 2 ** 127 - 1);
this.diffHours(2 ** 128 - 1, 2 ** 127 - 1);

vm.expectRevert(stdError.arithmeticError);
DateTimeLib.addMinutes(2 ** 128 - 1, 2 ** 255 - 1);
this.addMinutes(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.subMinutes(2 ** 128 - 1, 2 ** 255 - 1);
this.subMinutes(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.diffMinutes(2 ** 128 - 1, 2 ** 127 - 1);
this.diffMinutes(2 ** 128 - 1, 2 ** 127 - 1);

vm.expectRevert(stdError.arithmeticError);
DateTimeLib.addSeconds(2 ** 128 - 1, 2 ** 255 - 1);
this.addSeconds(2 ** 256 - 1, 2 ** 256 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.subSeconds(2 ** 128 - 1, 2 ** 255 - 1);
this.subSeconds(2 ** 128 - 1, 2 ** 255 - 1);
vm.expectRevert(stdError.arithmeticError);
DateTimeLib.diffSeconds(2 ** 128 - 1, 2 ** 127 - 1);
this.diffSeconds(2 ** 128 - 1, 2 ** 127 - 1);
}

function testAddSubDiffMonths(uint256 timestamp, uint256 numMonths) public {
Expand Down
4 changes: 2 additions & 2 deletions test/ECDSA.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ contract ECDSATest is SoladyTest {

function testBytesToEthSignedMessageHashExceedsMaxLengthReverts() public {
vm.expectRevert();
_testBytesToEthSignedMessageHash(999999 + 1);
this._testBytesToEthSignedMessageHash(999999 + 1);
}

function _testBytesToEthSignedMessageHash(uint256 n) internal brutalizeMemory {
function _testBytesToEthSignedMessageHash(uint256 n) public brutalizeMemory {
bytes memory message;
/// @solidity memory-safe-assembly
assembly {
Expand Down
19 changes: 13 additions & 6 deletions test/ERC20Votes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -322,26 +322,26 @@ contract ERC20VotesTest is SoladyTest {
lengthSlot = uint256(keccak256(abi.encode(lengthSlot, "hehe")));
uint256 key = _randomUniform() & 0xf;
if (_randomChance(2)) {
_checkpointPushDiff(lengthSlot, key, type(uint256).max - 10, true);
this.checkpointPushDiff(lengthSlot, key, type(uint256).max - 10, true);
key += _randomUniform() & 0xf;
uint256 amount = _randomUniform() % 20;
if (amount <= 10) {
_checkpointPushDiff(lengthSlot, key, amount, true);
this.checkpointPushDiff(lengthSlot, key, amount, true);
assertEq(_checkpointLatest(lengthSlot), type(uint256).max - 10 + amount);
} else {
vm.expectRevert(ERC20Votes.ERC5805CheckpointValueOverflow.selector);
_checkpointPushDiff(lengthSlot, key, amount, true);
this.checkpointPushDiff(lengthSlot, key, amount, true);
}
} else {
_checkpointPushDiff(lengthSlot, key, 10, true);
this.checkpointPushDiff(lengthSlot, key, 10, true);
key += _randomUniform() & 0xf;
uint256 amount = _randomUniform() % 20;
if (amount <= 10) {
_checkpointPushDiff(lengthSlot, key, amount, false);
this.checkpointPushDiff(lengthSlot, key, amount, false);
assertEq(_checkpointLatest(lengthSlot), 10 - amount);
} else {
vm.expectRevert(ERC20Votes.ERC5805CheckpointValueUnderflow.selector);
_checkpointPushDiff(lengthSlot, key, amount, false);
this.checkpointPushDiff(lengthSlot, key, amount, false);
}
}
}
Expand Down Expand Up @@ -470,6 +470,13 @@ contract ERC20VotesTest is SoladyTest {
}
}

function checkpointPushDiff(uint256 lengthSlot, uint256 key, uint256 amount, bool isAdd)
public
returns (uint256 oldValue, uint256 newValue)
{
return _checkpointPushDiff(lengthSlot, key, amount, isAdd);
}

/// @dev Pushes a checkpoint.
function _checkpointPushDiff(uint256 lengthSlot, uint256 key, uint256 amount, bool isAdd)
private
Expand Down
4 changes: 2 additions & 2 deletions test/EnumerableSetLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ contract EnumerableSetLibTest is SoladyTest {
assertEq(addressSet.at(i), values[i]);
}
vm.expectRevert(EnumerableSetLib.IndexOutOfBounds.selector);
addressSetAt(_bound(_random(), values.length, type(uint256).max));
this.addressSetAt(_bound(_random(), values.length, type(uint256).max));
}
}

Expand All @@ -481,7 +481,7 @@ contract EnumerableSetLibTest is SoladyTest {
assertEq(bytes32Set.at(i), values[i]);
}
vm.expectRevert(EnumerableSetLib.IndexOutOfBounds.selector);
bytes32SetAt(_bound(_random(), values.length, type(uint256).max));
this.bytes32SetAt(_bound(_random(), values.length, type(uint256).max));
}
}

Expand Down
Loading

0 comments on commit 30a0de6

Please sign in to comment.