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

Improved test coverage for array exercises - FilterOddNumbers and Mean #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
80 changes: 80 additions & 0 deletions FilterOddNumbers/test/FilterOddNumbers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,84 @@ contract FilterOddNumbersTest is Test {
assertEq(filter2[4], 2, "expected 2");
assertEq(filter2[5], 20, "expected 20");
}

// Test case when the array is already all even numbers
function testAllEven() external {
uint256[] memory evenArray = new uint256[](5);
evenArray[0] = 2;
evenArray[1] = 4;
evenArray[2] = 6;
evenArray[3] = 8;
evenArray[4] = 10;

uint256[] memory filteredArray = filterContract.filterOdd(evenArray);

assertEq(filteredArray.length, 5, "expected length 5");
assertEq(filteredArray[0], 2, "expected 2");
assertEq(filteredArray[1], 4, "expected 4");
assertEq(filteredArray[2], 6, "expected 6");
assertEq(filteredArray[3], 8, "expected 8");
assertEq(filteredArray[4], 10, "expected 10");
}

// Test case when the array has all odd numbers
function testAllOdd() external {
uint256[] memory oddArray = new uint256[](5);
oddArray[0] = 1;
oddArray[1] = 3;
oddArray[2] = 5;
oddArray[3] = 7;
oddArray[4] = 9;

uint256[] memory filteredArray = filterContract.filterOdd(oddArray);

assertEq(filteredArray.length, 0, "expected length 0");
}

// Test case with an empty array
function testEmptyArray() external {
uint256[] memory emptyArray = new uint256[](0);
uint256[] memory filteredArray = filterContract.filterOdd(emptyArray);

assertEq(filteredArray.length, 0, "expected length 0");
}

// Test case with a single even number
function testSingleEvenNumber() external {
uint256[] memory singleEven = new uint256[](1);
singleEven[0] = 2;

uint256[] memory filteredArray = filterContract.filterOdd(singleEven);

assertEq(filteredArray.length, 1, "expected length 1");
assertEq(filteredArray[0], 2, "expected 2");
}

// Test case with a single odd number
function testSingleOddNumber() external {
uint256[] memory singleOdd = new uint256[](1);
singleOdd[0] = 1;

uint256[] memory filteredArray = filterContract.filterOdd(singleOdd);

assertEq(filteredArray.length, 0, "expected length 0");
}

// Test case with a large array
function testLargeArray() external {
uint256[] memory largeArray = new uint256[](50);
uint256 evenCount = 0;

// Fill the array with numbers and count even numbers
for (uint256 i = 0; i < largeArray.length; i++) {
largeArray[i] = i;
if (i % 2 == 0) {
evenCount++;
}
}

uint256[] memory filteredArray = filterContract.filterOdd(largeArray);

assertEq(filteredArray.length, evenCount, "expected even count"); // Length should match even number count
}
}
43 changes: 39 additions & 4 deletions Mean/test/Mean.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,65 @@ contract MeanTest is Test {
mean = new Mean();
}

function testMean() public {
function testMeanx() external {
uint256[] memory x = new uint256[](5);
x[0] = 5;
x[1] = 10;
x[2] = 15;
x[3] = 20;
x[4] = 25;

assertEq(mean.mean(x), 15, "expected mean of arr 'x' is 15");
}

function testMeany() external {
uint256[] memory y = new uint256[](5);
y[0] = 5;
y[1] = 10;
y[2] = 15;
y[3] = 19;
y[4] = 25;

assertEq(mean.mean(y), 14, "expected mean of array 'y' is 14");
}

function testMeanMostlyZeroes() external {
uint256[] memory z = new uint256[](5);
z[0] = 0;
z[1] = 0;
z[2] = 1;
z[3] = 0;
z[4] = 0;

assertEq(mean.mean(x), 15, "mean of arr 'x' is 15");
assertEq(mean.mean(y), 14, "mean of arr 'y' is 14");
assertEq(mean.mean(z), 0, "mean of arr 'z' is 0");
assertEq(mean.mean(z), 0, "expected mean of arr 'z' is 0");
}

function testMeanSingleElement() external {
uint256[] memory singleElement = new uint256[](1);
singleElement[0] = 42;
assertEq(
mean.mean(singleElement),
42,
"expected 42, because the answer to ultimate question of life is 42. Also, mean of a single-element array should be element itself"
);
}

function testMeanLargeNumbers() external {
uint256[] memory largeNumbers = new uint256[](3);
largeNumbers[0] = 1e42;
largeNumbers[1] = 1e42;
largeNumbers[2] = 1e42;

assertEq(
mean.mean(largeNumbers),
1e42,
"Mean of large numbers should be the large number itself"
);
}

function testMeanEmptyArray() external {
uint256[] memory emptyArray = new uint256[](0);
vm.expectRevert(abi.encodeWithSignature("Panic(uint256)", 0x12));
mean.mean(emptyArray);
}
}