Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improving test coverage of SCs #240

Merged
merged 16 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.20;

import "../../vendor/AddressAliasHelper.sol";

contract AddressAliasHelperTest {
vladbochok marked this conversation as resolved.
Show resolved Hide resolved
function applyL1ToL2Alias(address _l1Address) external pure returns (address) {
return AddressAliasHelper.applyL1ToL2Alias(_l1Address);
}

function undoL1ToL2Alias(address _l2Address) external pure returns (address) {
return AddressAliasHelper.undoL1ToL2Alias(_l2Address);
}
}
19 changes: 7 additions & 12 deletions l1-contracts/contracts/dev-contracts/test/PriorityQueueTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,33 @@ pragma solidity 0.8.20;
import "../../state-transition/libraries/PriorityQueue.sol";

contract PriorityQueueTest {
// add this to be excluded from coverage report
function test() internal virtual {}

using PriorityQueue for PriorityQueue.Queue;

PriorityQueue.Queue priorityQueue;

function getFirstUnprocessedPriorityTx() external view returns (uint256) {
return priorityQueue.getFirstUnprocessedPriorityTx();
return PriorityQueue.getFirstUnprocessedPriorityTx(priorityQueue);
}

function getTotalPriorityTxs() external view returns (uint256) {
return priorityQueue.getTotalPriorityTxs();
return PriorityQueue.getTotalPriorityTxs(priorityQueue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foundry-rs/foundry#3128

using for patterns are ignored by coverage

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wanted to ask why the PriorityQueue folder is missing from tests in this branch, but is available in the main.

Shall I add it back or maybe the base is not correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add back, thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It moved to state-transition folder, so all good, missed it :)

}

function getSize() external view returns (uint256) {
return priorityQueue.getSize();
return PriorityQueue.getSize(priorityQueue);
}

function isEmpty() external view returns (bool) {
return priorityQueue.isEmpty();
return PriorityQueue.isEmpty(priorityQueue);
}

function pushBack(PriorityOperation memory _operation) external {
return priorityQueue.pushBack(_operation);
return PriorityQueue.pushBack(priorityQueue, _operation);
}

function front() external view returns (PriorityOperation memory) {
return priorityQueue.front();
return PriorityQueue.front(priorityQueue);
}

function popFront() external returns (PriorityOperation memory operation) {
return priorityQueue.popFront();
return PriorityQueue.popFront(priorityQueue);
}
}
15 changes: 15 additions & 0 deletions l1-contracts/contracts/dev-contracts/test/UncheckedMathTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.20;

import "../../common/libraries/UncheckedMath.sol";

contract UncheckedMathTest {
function uncheckedInc(uint256 _number) external pure returns (uint256) {
return UncheckedMath.uncheckedInc(_number);
}

function uncheckedAdd(uint256 _lhs, uint256 _rhs) external pure returns (uint256) {
return UncheckedMath.uncheckedAdd(_lhs, _rhs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {Test} from "forge-std/Test.sol";
import {AddressAliasHelperTest} from "solpp/dev-contracts/test/AddressAliasHelperTest.sol";

contract AddressAliasHelperSharedTest is Test {
vladbochok marked this conversation as resolved.
Show resolved Hide resolved
AddressAliasHelperTest addressAliasHelper;

function setUp() public {
addressAliasHelper = new AddressAliasHelperTest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {AddressAliasHelperSharedTest} from "./_AddressAliasHelper_Shared.t.sol";

contract applyL1ToL2AliasTest is AddressAliasHelperSharedTest {
function testL1toL2AddressConversion() public {
address[2] memory l1Addresses = [0xEEeEfFfffffFffFFFFffFFffFfFfFfffFfFFEEeE, 0x0000000000000000000000000000081759a874B3];
address[2] memory l2ExpectedAddresses = [0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF, 0x1111000000000000000000000000081759a885c4];

for (uint i; i < l1Addresses.length; i++) {
address l2Address = addressAliasHelper.applyL1ToL2Alias(l1Addresses[i]);

assertEq(
l2Address,
l2ExpectedAddresses[i],
"L1 to L2 address conversion is not correct"
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {AddressAliasHelperSharedTest} from "./_AddressAliasHelper_Shared.t.sol";

contract undoL1ToL2AliasTest is AddressAliasHelperSharedTest {
function testL2toL1AddressConversion() public {
address[2] memory l2Addresses = [0x1111000000000000000000000000000000001110, 0x1111000000000000000000000000081759a885c4];
address[2] memory l1ExpectedAddresses = [0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF, 0x0000000000000000000000000000081759a874B3];

for (uint i; i < l2Addresses.length; i++) {
address l1Address = addressAliasHelper.undoL1ToL2Alias(l2Addresses[i]);

assertEq(
l1Address,
l1ExpectedAddresses[i],
"L2 to L1 address conversion is not correct"
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {UncheckedMathTest} from "./_UncheckedMath_Shared.t.sol";

import {UncheckedMath} from "solpp/common/libraries/UncheckedMath.sol";

contract UncheckedAddTest is UncheckedMathTest {
using UncheckedMath for uint256;
import {UncheckedMathSharedTest} from "./_UncheckedMath_Shared.t.sol";

contract UncheckedAddTest is UncheckedMathSharedTest {
function test_Add() public {
uint256 a = 1234;
uint256 b = 4321;
uint256 c = a.uncheckedAdd(b);
uint256 c = uncheckedMath.uncheckedAdd(a, b);
assertEq(c, 5555);
}

Expand All @@ -20,7 +16,7 @@ contract UncheckedAddTest is UncheckedMathTest {
uint256 b = 1;

// uncheckedAdd does not fail
uint256 c = a.uncheckedAdd(b);
uint256 c = uncheckedMath.uncheckedAdd(a, b);
assertEq(c, 0);

// regular addition fails with overflow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {UncheckedMathTest} from "./_UncheckedMath_Shared.t.sol";

import {UncheckedMath} from "solpp/common/libraries/UncheckedMath.sol";

contract UncheckedIncTest is UncheckedMathTest {
using UncheckedMath for uint256;
import {UncheckedMathSharedTest} from "./_UncheckedMath_Shared.t.sol";

contract UncheckedIncTest is UncheckedMathSharedTest {
function test_Inc() public {
uint256 a = 1234;
uint256 c = a.uncheckedInc();
uint256 c = uncheckedMath.uncheckedInc(a);
assertEq(c, 1235);
}

function test_IncWithOverflow() public {
uint256 a = type(uint256).max;

// uncheckedInc does not fail
uint256 c = a.uncheckedInc();
uint256 c = uncheckedMath.uncheckedInc(a);
assertEq(c, 0);

// regular addition fails with overflow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
pragma solidity 0.8.20;

import {Test} from "forge-std/Test.sol";
import {UncheckedMathTest} from "solpp/dev-contracts/test/UncheckedMathTest.sol";

contract UncheckedMathTest is Test {}
contract UncheckedMathSharedTest is Test {
vladbochok marked this conversation as resolved.
Show resolved Hide resolved
UncheckedMathTest uncheckedMath;

function setUp() public {
uncheckedMath = new UncheckedMathTest();
}
}
Loading