Skip to content

Add tests in SearchInARowAndColWiseSortedMatrix #5675

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

Merged
merged 3 commits into from
Oct 10, 2024
Merged
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@
* [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)
* [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)
* [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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class SearchInARowAndColWiseSortedMatrix {
* @param value Key being searched for
* @author Sadiul Hakim : https://github.com/sadiul-hakim
*/

public int[] search(int[][] matrix, int value) {
int n = matrix.length;
// This variable iterates over rows
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}