Skip to content

Commit fc7a85b

Browse files
committed
Add tests, remove main in SaddlebackSearch
1 parent b54cc21 commit fc7a85b

File tree

2 files changed

+115
-29
lines changed

2 files changed

+115
-29
lines changed
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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 = {
16+
{-10, -5, -3, 4, 9},
17+
{-6, -2, 0, 5, 10},
18+
{-4, -1, 1, 6, 12},
19+
{2, 3, 7, 8, 13},
20+
{100, 120, 130, 140, 150}
21+
};
22+
23+
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 4);
24+
assertArrayEquals(new int[]{0, 3}, result, "Element 4 should be found at (0, 3)");
25+
}
26+
27+
/**
28+
* Test searching for an element that does not exist in the array.
29+
*/
30+
@Test
31+
void testFindElementNotExists() {
32+
int[][] arr = {
33+
{-10, -5, -3, 4, 9},
34+
{-6, -2, 0, 5, 10},
35+
{-4, -1, 1, 6, 12},
36+
{2, 3, 7, 8, 13},
37+
{100, 120, 130, 140, 150}
38+
};
39+
40+
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 1000);
41+
assertArrayEquals(new int[]{-1, -1}, result, "Element 1000 should not be found");
42+
}
43+
44+
/**
45+
* Test searching for the smallest element in the array.
46+
*/
47+
@Test
48+
void testFindSmallestElement() {
49+
int[][] arr = {
50+
{-10, -5, -3, 4, 9},
51+
{-6, -2, 0, 5, 10},
52+
{-4, -1, 1, 6, 12},
53+
{2, 3, 7, 8, 13},
54+
{100, 120, 130, 140, 150}
55+
};
56+
57+
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, -10);
58+
assertArrayEquals(new int[]{0, 0}, result, "Element -10 should be found at (0, 0)");
59+
}
60+
61+
/**
62+
* Test searching for the largest element in the array.
63+
*/
64+
@Test
65+
void testFindLargestElement() {
66+
int[][] arr = {
67+
{-10, -5, -3, 4, 9},
68+
{-6, -2, 0, 5, 10},
69+
{-4, -1, 1, 6, 12},
70+
{2, 3, 7, 8, 13},
71+
{100, 120, 130, 140, 150}
72+
};
73+
74+
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 150);
75+
assertArrayEquals(new int[]{4, 4}, result, "Element 150 should be found at (4, 4)");
76+
}
77+
78+
/**
79+
* Test searching in an empty array.
80+
*/
81+
@Test
82+
void testFindInEmptyArray() {
83+
int[][] arr = {};
84+
85+
assertThrows(IllegalArgumentException.class, () -> { SaddlebackSearch.find(arr, 0, 0, 4); });
86+
}
87+
88+
/**
89+
* Test searching in a single element array that matches the search key.
90+
*/
91+
@Test
92+
void testFindSingleElementExists() {
93+
int[][] arr = {{5}};
94+
95+
int[] result = SaddlebackSearch.find(arr, 0, 0, 5);
96+
assertArrayEquals(new int[]{0, 0}, result, "Element 5 should be found at (0, 0)");
97+
}
98+
99+
/**
100+
* Test searching in a single element array that does not match the search key.
101+
*/
102+
@Test
103+
void testFindSingleElementNotExists() {
104+
int[][] arr = {{5}};
105+
106+
int[] result = SaddlebackSearch.find(arr, 0, 0, 10);
107+
assertArrayEquals(new int[]{-1, -1}, result, "Element 10 should not be found in single element array");
108+
}
109+
}

0 commit comments

Comments
 (0)