Add tests in SearchInARowAndColWiseSortedMatrix (#5675)

This commit is contained in:
Hardik Pawar
2024-10-11 01:32:13 +05:30
committed by GitHub
parent a663e66782
commit fb11d455dd
3 changed files with 67 additions and 1 deletions

View File

@ -1015,6 +1015,7 @@
* [RabinKarpAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java) * [RabinKarpAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java)
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java) * [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java) * [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
* [SearchInARowAndColWiseSortedMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java)
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java) * [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java) * [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java) * [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)

View File

@ -8,7 +8,6 @@ public class SearchInARowAndColWiseSortedMatrix {
* @param value Key being searched for * @param value Key being searched for
* @author Sadiul Hakim : https://github.com/sadiul-hakim * @author Sadiul Hakim : https://github.com/sadiul-hakim
*/ */
public int[] search(int[][] matrix, int value) { public int[] search(int[][] matrix, int value) {
int n = matrix.length; int n = matrix.length;
// This variable iterates over rows // This variable iterates over rows

View File

@ -0,0 +1,66 @@
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
class SearchInARowAndColWiseSortedMatrixTest {
private final SearchInARowAndColWiseSortedMatrix searcher = new SearchInARowAndColWiseSortedMatrix();
@Test
void testSearchValueExistsInMatrix() {
int[][] matrix = {{10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}};
int value = 29;
int[] expected = {2, 1}; // Row 2, Column 1
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the matrix");
}
@Test
void testSearchValueNotExistsInMatrix() {
int[][] matrix = {{10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}};
int value = 100;
int[] expected = {-1, -1}; // Not found
assertArrayEquals(expected, searcher.search(matrix, value), "Value should not be found in the matrix");
}
@Test
void testSearchInEmptyMatrix() {
int[][] matrix = {};
int value = 5;
int[] expected = {-1, -1}; // Not found
assertArrayEquals(expected, searcher.search(matrix, value), "Should return {-1, -1} for empty matrix");
}
@Test
void testSearchInSingleElementMatrixFound() {
int[][] matrix = {{5}};
int value = 5;
int[] expected = {0, 0}; // Found at (0,0)
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in single element matrix");
}
@Test
void testSearchInSingleElementMatrixNotFound() {
int[][] matrix = {{10}};
int value = 5;
int[] expected = {-1, -1}; // Not found
assertArrayEquals(expected, searcher.search(matrix, value), "Should return {-1, -1} for value not found in single element matrix");
}
@Test
void testSearchInRowWiseSortedMatrix() {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int value = 6;
int[] expected = {1, 2}; // Found at (1, 2)
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the row-wise sorted matrix");
}
@Test
void testSearchInColWiseSortedMatrix() {
int[][] matrix = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
int value = 5;
int[] expected = {1, 1}; // Found at (1, 1)
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the column-wise sorted matrix");
}
}