Skip to content

Commit 29ad197

Browse files
authored
Add tests, remove main in SaddlebackSearch (#5674)
1 parent e8b3251 commit 29ad197

File tree

3 files changed

+92
-29
lines changed

3 files changed

+92
-29
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@
10171017
* [RabinKarpAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java)
10181018
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
10191019
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
1020+
* [SaddlebackSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java)
10201021
* [SearchInARowAndColWiseSortedMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java)
10211022
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
10221023
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.searches;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Program to perform Saddleback Search Given a sorted 2D array(elements are
75
* sorted across every row and column, assuming ascending order) of size n*m we
@@ -27,10 +25,15 @@ private SaddlebackSearch() {
2725
* @param row the current row.
2826
* @param col the current column.
2927
* @param key the element that we want to search for.
28+
* @throws IllegalArgumentException if the array is empty.
3029
* @return The index(row and column) of the element if found. Else returns
3130
* -1 -1.
3231
*/
33-
private static int[] find(int[][] arr, int row, int col, int key) {
32+
static int[] find(int[][] arr, int row, int col, int key) {
33+
if (arr.length == 0) {
34+
throw new IllegalArgumentException("Array is empty");
35+
}
36+
3437
// array to store the answer row and column
3538
int[] ans = {-1, -1};
3639
if (row < 0 || col >= arr[row].length) {
@@ -47,30 +50,4 @@ else if (arr[row][col] > key) {
4750
// else we move right
4851
return find(arr, row, col + 1, key);
4952
}
50-
51-
/**
52-
* Main method
53-
*
54-
* @param args Command line arguments
55-
*/
56-
public static void main(String[] args) {
57-
// TODO Auto-generated method stub
58-
Scanner sc = new Scanner(System.in);
59-
int[][] arr;
60-
int i;
61-
int j;
62-
int rows = sc.nextInt();
63-
int col = sc.nextInt();
64-
arr = new int[rows][col];
65-
for (i = 0; i < rows; i++) {
66-
for (j = 0; j < col; j++) {
67-
arr[i][j] = sc.nextInt();
68-
}
69-
}
70-
int ele = sc.nextInt();
71-
// we start from bottom left corner
72-
int[] ans = find(arr, rows - 1, 0, ele);
73-
System.out.println(ans[0] + " " + ans[1]);
74-
sc.close();
75-
}
7653
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SaddlebackSearchTest {
9+
10+
/**
11+
* Test searching for an element that exists in the array.
12+
*/
13+
@Test
14+
void testFindElementExists() {
15+
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
16+
17+
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 4);
18+
assertArrayEquals(new int[] {0, 3}, result, "Element 4 should be found at (0, 3)");
19+
}
20+
21+
/**
22+
* Test searching for an element that does not exist in the array.
23+
*/
24+
@Test
25+
void testFindElementNotExists() {
26+
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
27+
28+
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 1000);
29+
assertArrayEquals(new int[] {-1, -1}, result, "Element 1000 should not be found");
30+
}
31+
32+
/**
33+
* Test searching for the smallest element in the array.
34+
*/
35+
@Test
36+
void testFindSmallestElement() {
37+
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
38+
39+
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, -10);
40+
assertArrayEquals(new int[] {0, 0}, result, "Element -10 should be found at (0, 0)");
41+
}
42+
43+
/**
44+
* Test searching for the largest element in the array.
45+
*/
46+
@Test
47+
void testFindLargestElement() {
48+
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
49+
50+
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 150);
51+
assertArrayEquals(new int[] {4, 4}, result, "Element 150 should be found at (4, 4)");
52+
}
53+
54+
/**
55+
* Test searching in an empty array.
56+
*/
57+
@Test
58+
void testFindInEmptyArray() {
59+
int[][] arr = {};
60+
61+
assertThrows(IllegalArgumentException.class, () -> { SaddlebackSearch.find(arr, 0, 0, 4); });
62+
}
63+
64+
/**
65+
* Test searching in a single element array that matches the search key.
66+
*/
67+
@Test
68+
void testFindSingleElementExists() {
69+
int[][] arr = {{5}};
70+
71+
int[] result = SaddlebackSearch.find(arr, 0, 0, 5);
72+
assertArrayEquals(new int[] {0, 0}, result, "Element 5 should be found at (0, 0)");
73+
}
74+
75+
/**
76+
* Test searching in a single element array that does not match the search key.
77+
*/
78+
@Test
79+
void testFindSingleElementNotExists() {
80+
int[][] arr = {{5}};
81+
82+
int[] result = SaddlebackSearch.find(arr, 0, 0, 10);
83+
assertArrayEquals(new int[] {-1, -1}, result, "Element 10 should not be found in single element array");
84+
}
85+
}

0 commit comments

Comments
 (0)