Skip to content

Commit b1fe8bc

Browse files
Add unit tests for Search2DMatrix implementations
- Added JUnit tests covering both variations of 2D matrix search. - Tests validate correctness for typical inputs and edge cases. - Helps increase code coverage and ensures algorithm correctness.
1 parent cf12cf8 commit b1fe8bc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.thealgorithms.matrix;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class Search2DMatrixTest {
7+
8+
@Test
9+
public void testSearchMatrixSortedRowsAndCols() {
10+
int[][] matrix = {
11+
{1, 4, 7, 11},
12+
{2, 5, 8, 12},
13+
{3, 6, 9, 16},
14+
{10, 13, 14, 17}
15+
};
16+
assertTrue(Search2DMatrix.searchMatrixSortedRowsAndCols(matrix, 5));
17+
assertFalse(Search2DMatrix.searchMatrixSortedRowsAndCols(matrix, 20));
18+
}
19+
20+
@Test
21+
public void testSearchMatrixFlattenedSorted() {
22+
int[][] matrix = {
23+
{1, 3, 5, 7},
24+
{10, 11, 16, 20},
25+
{23, 30, 34, 50}
26+
};
27+
assertTrue(Search2DMatrix.searchMatrixFlattenedSorted(matrix, 3));
28+
assertFalse(Search2DMatrix.searchMatrixFlattenedSorted(matrix, 4));
29+
}
30+
}

0 commit comments

Comments
 (0)