Skip to content

Commit fb11d45

Browse files
authored
Add tests in SearchInARowAndColWiseSortedMatrix (#5675)
1 parent a663e66 commit fb11d45

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@
10151015
* [RabinKarpAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java)
10161016
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
10171017
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
1018+
* [SearchInARowAndColWiseSortedMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java)
10181019
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
10191020
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
10201021
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)

src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class SearchInARowAndColWiseSortedMatrix {
88
* @param value Key being searched for
99
* @author Sadiul Hakim : https://github.com/sadiul-hakim
1010
*/
11-
1211
public int[] search(int[][] matrix, int value) {
1312
int n = matrix.length;
1413
// This variable iterates over rows
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class SearchInARowAndColWiseSortedMatrixTest {
8+
9+
private final SearchInARowAndColWiseSortedMatrix searcher = new SearchInARowAndColWiseSortedMatrix();
10+
11+
@Test
12+
void testSearchValueExistsInMatrix() {
13+
int[][] matrix = {{10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}};
14+
int value = 29;
15+
int[] expected = {2, 1}; // Row 2, Column 1
16+
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the matrix");
17+
}
18+
19+
@Test
20+
void testSearchValueNotExistsInMatrix() {
21+
int[][] matrix = {{10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}};
22+
int value = 100;
23+
int[] expected = {-1, -1}; // Not found
24+
assertArrayEquals(expected, searcher.search(matrix, value), "Value should not be found in the matrix");
25+
}
26+
27+
@Test
28+
void testSearchInEmptyMatrix() {
29+
int[][] matrix = {};
30+
int value = 5;
31+
int[] expected = {-1, -1}; // Not found
32+
assertArrayEquals(expected, searcher.search(matrix, value), "Should return {-1, -1} for empty matrix");
33+
}
34+
35+
@Test
36+
void testSearchInSingleElementMatrixFound() {
37+
int[][] matrix = {{5}};
38+
int value = 5;
39+
int[] expected = {0, 0}; // Found at (0,0)
40+
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in single element matrix");
41+
}
42+
43+
@Test
44+
void testSearchInSingleElementMatrixNotFound() {
45+
int[][] matrix = {{10}};
46+
int value = 5;
47+
int[] expected = {-1, -1}; // Not found
48+
assertArrayEquals(expected, searcher.search(matrix, value), "Should return {-1, -1} for value not found in single element matrix");
49+
}
50+
51+
@Test
52+
void testSearchInRowWiseSortedMatrix() {
53+
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
54+
int value = 6;
55+
int[] expected = {1, 2}; // Found at (1, 2)
56+
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the row-wise sorted matrix");
57+
}
58+
59+
@Test
60+
void testSearchInColWiseSortedMatrix() {
61+
int[][] matrix = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
62+
int value = 5;
63+
int[] expected = {1, 1}; // Found at (1, 1)
64+
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the column-wise sorted matrix");
65+
}
66+
}

0 commit comments

Comments
 (0)