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 comprehensive unit tests for TicTacToe and NestedArray contracts #37

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
74 changes: 74 additions & 0 deletions NestedArray/test/NestedArray.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,78 @@ contract NestedArrayTest is Test {

assertEq(nestedArray.getNestedSum(), 1200, "expected sum to be 1200");
}

// Test case for empty array
function testEmptyArray() external {
uint256[] memory _newArr = new uint256[](0);
newArr.push(_newArr);
nestedArray.setArr(newArr);
assertEq(
nestedArray.getNestedSum(),
0,
"Expected sum to be 0 for an empty array"
);
}

// Test case for empty inner arrays
function testEmptyInnerArray() external {
uint256[] memory _newArr = new uint256[](0);
newArr.push(_newArr);
newArr.push(_newArr);
newArr.push(_newArr);

nestedArray.setArr(newArr);
assertEq(
nestedArray.getNestedSum(),
0,
"Expected sum to be 0 when all inner arrays are empty"
);
}

// Test case for one-dimensional array
function test1DArray() external {
uint256[] memory _newArr = new uint256[](3);
_newArr[0] = 1;
_newArr[1] = 2;
_newArr[2] = 3;
newArr.push(_newArr);

nestedArray.setArr(newArr);
assertEq(
nestedArray.getNestedSum(),
6,
"Expected sum to be 6 for a one-dimensional array"
);
}

// Test case for single value in the nested array
function testSingleValue() external {
uint256[] memory _newArr = new uint256[](1);
_newArr[0] = 1;
newArr.push(_newArr);

nestedArray.setArr(newArr);
assertEq(
nestedArray.getNestedSum(),
1,
"Expected sum to be 1 for a single value in the nested array"
);
}

// Test case for large numbers
function testLargeNumbers() external {
uint256[] memory _newArr = new uint256[](3);
_newArr[0] = 1 << 250;
_newArr[1] = 1 << 255;
_newArr[2] = 1;

newArr.push(_newArr);

nestedArray.setArr(newArr);
assertEq(
nestedArray.getNestedSum(),
2 ** 250 + 2 ** 255 + 1,
"Expected sum to be 59705296012991163265278789145104702486842335843220915832845316754080207470593 for large numbers"
);
}
}
52 changes: 52 additions & 0 deletions TicTacToe/test/TicTacToe.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,63 @@ contract TicTacToeTest is Test {
assertEq(isWinning, true, "expected isWinning to be true");
}

function testWinningRow() external {
uint8[3][3] memory board = [[1, 1, 1], [0, 0, 1], [1, 0, 0]];
assertEq(tictactoe.isWinning(board), true, "expected row win");
}

function testWinningColumn() external {
uint8[3][3] memory board = [[1, 0, 0], [1, 0, 1], [1, 0, 0]];
assertEq(tictactoe.isWinning(board), true, "expected column win");
}

function testWinningDiagonalLeftToRight() external {
uint8[3][3] memory board = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
assertEq(tictactoe.isWinning(board), true, "expected diagonal win");
}

function testWinningDiagonalRightToLeft() external {
uint8[3][3] memory board = [[0, 0, 1], [0, 1, 0], [1, 0, 0]];
assertEq(
tictactoe.isWinning(board),
true,
"expected right to left diagonal win"
);
}

function testIsLosing() external {
uint8[3][3] memory board = [[0, 1, 0], [0, 1, 0], [1, 0, 1]];

bool isWinning = tictactoe.isWinning(board);

assertFalse(isWinning, "expected isWinning to be false");
}

function testAllZeros() external {
uint8[3][3] memory board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
assertEq(
tictactoe.isWinning(board),
true,
"expected win for all zeros"
);
}

function testAllOnes() external {
uint8[3][3] memory board = [[1, 1, 1], [1, 1, 1], [1, 1, 1]];
assertEq(tictactoe.isWinning(board), true, "expected win for all ones");
}

function testCornerCaseWin() external {
uint8[3][3] memory board = [[1, 1, 1], [0, 0, 0], [1, 0, 1]];
assertEq(
tictactoe.isWinning(board),
true,
"expected win with only one row full"
);
}

function testNoWin() external {
uint8[3][3] memory board = [[1, 1, 0], [0, 1, 1], [1, 0, 0]];
assertEq(tictactoe.isWinning(board), false, "expected false");
}
}