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

Add test cases for EnumerableGuardianMap. #6

Merged
merged 7 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/libraries/EnumerableGuardianMap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ library EnumerableGuardianMap {
returns (bool)
{
uint256 length = map._keys.length();
if (length > MAX_NUMBER_OF_GUARDIANS) {
if (length >= MAX_NUMBER_OF_GUARDIANS) {
revert MaxNumberOfGuardiansReached();
}
map._values[key] = value;
Expand Down
31 changes: 28 additions & 3 deletions test/unit/libraries/EnumerableGuardianMap/get.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,42 @@
pragma solidity ^0.8.25;

import "forge-std/console2.sol";
import { UnitBase } from "../../UnitBase.t.sol";
import {UnitBase} from "../../UnitBase.t.sol";
import {EnumerableGuardianMap, GuardianStorage, GuardianStatus} from "../../../../src/libraries/EnumerableGuardianMap.sol";

contract EnumerableGuardianMap_get_Test is UnitBase {
using EnumerableGuardianMap for EnumerableGuardianMap.AddressToGuardianMap;
mapping(address account => EnumerableGuardianMap.AddressToGuardianMap guardian)
internal guardiansStorage;

function setUp() public override {
super.setUp();
guardiansStorage[accountAddress].set({
key: guardian1,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
}

function test_Get_GetsExistingValue() public view {
// TODO: test
GuardianStorage memory result = guardiansStorage[accountAddress].get(
guardian1
);
require(
result.status == GuardianStatus.REQUESTED,
"Expected status to be REQUESTED"
);
require(result.weight == guardianWeights[0], "Expected weight to be 1");
}

function test_Get_GetsNonExistentValue() public view {
// TODO: test
// It will returns the default value
GuardianStorage memory result = guardiansStorage[accountAddress].get(
guardian2
);
require(
result.status == GuardianStatus.NONE,
"Expected status to be NONE"
);
require(result.weight == 0, "Expected weight to be 0");
}
}
63 changes: 56 additions & 7 deletions test/unit/libraries/EnumerableGuardianMap/keys.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,72 @@
pragma solidity ^0.8.25;

import "forge-std/console2.sol";
import { UnitBase } from "../../UnitBase.t.sol";
import {UnitBase} from "../../UnitBase.t.sol";
import {EnumerableGuardianMap, GuardianStorage, GuardianStatus} from "../../../../src/libraries/EnumerableGuardianMap.sol";

contract EnumerableGuardianMap_keys_Test is UnitBase {
using EnumerableGuardianMap for EnumerableGuardianMap.AddressToGuardianMap;
mapping(address account => EnumerableGuardianMap.AddressToGuardianMap guardian)
internal guardiansStorage;

function setUp() public override {
super.setUp();
}

function test_Keys_StartsEmpty() public view {
// TODO: test
address[] memory keys = guardiansStorage[accountAddress].keys();
assertEq(keys.length, 0);
}

function test_Keys_ReturnsEmptyArrayOfKeys() public view {
Copy link
Collaborator

Choose a reason for hiding this comment

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

cool, can you remove this test case then?

// TODO: test
// TODO: It's same as test_Keys_StartsEmpty()
}
function test_Keys_ReturnsArrayOfKeys() public view {
// TODO: test

function test_Keys_ReturnsArrayOfKeys() public {
bool result;

for (uint256 i = 1; i <= 3; i++) {
result = guardiansStorage[accountAddress].set({
key: vm.addr(i),
value: GuardianStorage(
GuardianStatus.REQUESTED,
guardianWeights[1]
)
});
assertEq(result, true);
}
address[] memory keys = guardiansStorage[accountAddress].keys();
assertEq(keys.length, 3);
for (uint256 i = 0; i < 3; i++) {
assertEq(keys[i], vm.addr(i + 1));
}
}
function test_Keys_ReturnMaxArrayOfKeys() public view {
// TODO: test

function test_Keys_ReturnMaxArrayOfKeys() public {
bool result;

for (
uint256 i = 1;
i <= EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS;
i++
) {
result = guardiansStorage[accountAddress].set({
key: vm.addr(i),
value: GuardianStorage(
GuardianStatus.REQUESTED,
guardianWeights[1]
)
});
assertEq(result, true);
}
address[] memory keys = guardiansStorage[accountAddress].keys();
assertEq(keys.length, EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS);
for (
uint256 i = 0;
i < EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS;
i++
) {
assertEq(keys[i], vm.addr(i + 1));
}
}
}
124 changes: 117 additions & 7 deletions test/unit/libraries/EnumerableGuardianMap/remove.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,130 @@
pragma solidity ^0.8.25;

import "forge-std/console2.sol";
import { UnitBase } from "../../UnitBase.t.sol";
import {UnitBase} from "../../UnitBase.t.sol";
import {EnumerableGuardianMap, GuardianStorage, GuardianStatus} from "../../../../src/libraries/EnumerableGuardianMap.sol";

contract EnumerableGuardianMap_remove_Test is UnitBase {
using EnumerableGuardianMap for EnumerableGuardianMap.AddressToGuardianMap;
mapping(address account => EnumerableGuardianMap.AddressToGuardianMap guardian)
internal guardiansStorage;

function setUp() public override {
super.setUp();
}

function test_Remove_ReturnsFalseWhenRemovingKeysNotInTheSet() public view {
// TODO: test
function test_Remove_RemovesAddedKeys() public {
bool result;

guardiansStorage[accountAddress].set({
key: guardian1,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
result = guardiansStorage[accountAddress].remove(guardian1);
assertEq(result, true);
require(
guardiansStorage[accountAddress]._values[guardian1].status ==
GuardianStatus.NONE,
"Expected status to be NONE"
);
}
function test_Remove_RemovesAddedKeys() public view {
// TODO: test

function test_Remove_ReturnsFalseWhenRemovingKeysNotInTheSet() public {
bool result;

result = guardiansStorage[accountAddress].remove(guardian1);
assertEq(result, false);
}
function test_Remove_AddsAndRemovesMultipleKeys() public view {
// TODO: test

function test_Remove_AddsAndRemovesMultipleKeys() public {
bool result;

// []

result = guardiansStorage[accountAddress].set({
key: guardian1,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
assertEq(result, true);
result = guardiansStorage[accountAddress].set({
key: guardian3,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
assertEq(result, true);

// [1, 3]

result = guardiansStorage[accountAddress].remove(guardian1);
assertEq(result, true);
result = guardiansStorage[accountAddress].remove(guardian2);
assertEq(result, false);

// [3]

result = guardiansStorage[accountAddress].set({
key: guardian2,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
assertEq(result, true);

// [3,2]

result = guardiansStorage[accountAddress].set({
key: guardian1,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
assertEq(result, true);
result = guardiansStorage[accountAddress].remove(guardian3);
assertEq(result, true);

// [1,2]

result = guardiansStorage[accountAddress].set({
key: guardian1,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
assertEq(result, false);
result = guardiansStorage[accountAddress].set({
key: guardian2,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
assertEq(result, false);

// [1,2]

result = guardiansStorage[accountAddress].set({
key: guardian3,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
assertEq(result, true);
result = guardiansStorage[accountAddress].remove(guardian1);
assertEq(result, true);

// [2,3]

result = guardiansStorage[accountAddress].set({
key: guardian1,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
assertEq(result, true);
result = guardiansStorage[accountAddress].remove(guardian2);
assertEq(result, true);

// [1,3]
require(
guardiansStorage[accountAddress]._values[guardian1].status ==
GuardianStatus.REQUESTED,
"Expected status to be REQUESTED"
);
require(
guardiansStorage[accountAddress]._values[guardian2].status ==
GuardianStatus.NONE,
"Expected status to be NONE"
);
require(
guardiansStorage[accountAddress]._values[guardian3].status ==
GuardianStatus.REQUESTED,
"Expected status to be REQUESTED"
);
}
}
75 changes: 68 additions & 7 deletions test/unit/libraries/EnumerableGuardianMap/removeAll.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,82 @@
pragma solidity ^0.8.25;

import "forge-std/console2.sol";
import { UnitBase } from "../../UnitBase.t.sol";
import {UnitBase} from "../../UnitBase.t.sol";
import {EnumerableGuardianMap, GuardianStorage, GuardianStatus} from "../../../../src/libraries/EnumerableGuardianMap.sol";

contract EnumerableGuardianMap_removeAll_Test is UnitBase {
using EnumerableGuardianMap for EnumerableGuardianMap.AddressToGuardianMap;
mapping(address account => EnumerableGuardianMap.AddressToGuardianMap guardian)
internal guardiansStorage;

function setUp() public override {
super.setUp();
}

function test_RemoveAll_RevertWhen_TooManyValuesToRemove() public view {
// TODO: test
function test_RemoveAll_Succeeds() public {
bool result;

for (uint256 i = 1; i <= 3; i++) {
result = guardiansStorage[accountAddress].set({
key: vm.addr(i),
value: GuardianStorage(
GuardianStatus.REQUESTED,
guardianWeights[1]
)
});
assertEq(result, true);
}
address[] memory addresses = new address[](3);
addresses[0] = vm.addr(1);
addresses[1] = vm.addr(2);
addresses[2] = vm.addr(3);
guardiansStorage[accountAddress].removeAll(addresses);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add an assert to check that the values are no longer in storage?

}

function test_RemoveAll_Succeeds() public view {
// TODO: test
function test_RemoveAll_RemovesMaxNumberOfValues() public {
bool result;

for (
uint256 i = 1;
i <= EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS;
i++
) {
result = guardiansStorage[accountAddress].set({
key: vm.addr(i),
value: GuardianStorage(
GuardianStatus.REQUESTED,
guardianWeights[1]
)
});
assertEq(result, true);
}

address[] memory addresses = new address[](
EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS
);
for (
uint256 i = 0;
i < EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS;
i++
) {
addresses[i] = vm.addr(i + 1);
}
guardiansStorage[accountAddress].removeAll(addresses);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, can you add an assert to check that the values are no longer in storage?

}
function test_RemoveAll_RemovesMaxNumberOfValues() public view {
// TODO: test

function test_RemoveAll_RevertWhen_TooManyValuesToRemove() public {
address[] memory addresses = new address[](
EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS + 1
);

for (
uint256 i = 0;
i < EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS + 1;
i++
) {
addresses[i] = vm.addr(i + 1);
}
vm.expectRevert(EnumerableGuardianMap.TooManyValuesToRemove.selector);
guardiansStorage[accountAddress].removeAll(addresses);
}
}
Loading