|
| 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