Repository: TheAlgorithms/Java
Pull request #5674 (2024-10-09 07:04:07)

Add tests, remove `main` in `SaddlebackSearch`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index cfc8841f..cbe176f2 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -1017,6 +1017,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)
+            * [SaddlebackSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.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)
diff --git a/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java b/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java
index 5c7a914e..192c4d26 100644
--- a/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java
+++ b/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java
@@ -1,7 +1,5 @@
 package com.thealgorithms.searches;
 
-import java.util.Scanner;
-
 /**
  * Program to perform Saddleback Search Given a sorted 2D array(elements are
  * sorted across every row and column, assuming ascending order) of size n*m we
@@ -27,10 +25,15 @@ public final class SaddlebackSearch {
      * @param row the current row.
      * @param col the current column.
      * @param key the element that we want to search for.
+     * @throws IllegalArgumentException if the array is empty.
      * @return The index(row and column) of the element if found. Else returns
      * -1 -1.
      */
-    private static int[] find(int[][] arr, int row, int col, int key) {
+    static int[] find(int[][] arr, int row, int col, int key) {
+        if (arr.length == 0) {
+            throw new IllegalArgumentException("Array is empty");
+        }
+
         // array to store the answer row and column
         int[] ans = {-1, -1};
         if (row < 0 || col >= arr[row].length) {
@@ -47,30 +50,4 @@ public final class SaddlebackSearch {
         // else we move right
         return find(arr, row, col + 1, key);
     }
-
-    /**
-     * Main method
-     *
-     * @param args Command line arguments
-     */
-    public static void main(String[] args) {
-        // TODO Auto-generated method stub
-        Scanner sc = new Scanner(System.in);
-        int[][] arr;
-        int i;
-        int j;
-        int rows = sc.nextInt();
-        int col = sc.nextInt();
-        arr = new int[rows][col];
-        for (i = 0; i < rows; i++) {
-            for (j = 0; j < col; j++) {
-                arr[i][j] = sc.nextInt();
-            }
-        }
-        int ele = sc.nextInt();
-        // we start from bottom left corner
-        int[] ans = find(arr, rows - 1, 0, ele);
-        System.out.println(ans[0] + " " + ans[1]);
-        sc.close();
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java b/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java
new file mode 100644
index 00000000..ec22cbf3
--- /dev/null
+++ b/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java
@@ -0,0 +1,85 @@
+package com.thealgorithms.searches;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+class SaddlebackSearchTest {
+
+    /**
+     * Test searching for an element that exists in the array.
+     */
+    @Test
+    void testFindElementExists() {
+        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}};
+
+        int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 4);
+        assertArrayEquals(new int[] {0, 3}, result, "Element 4 should be found at (0, 3)");
+    }
+
+    /**
+     * Test searching for an element that does not exist in the array.
+     */
+    @Test
+    void testFindElementNotExists() {
+        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}};
+
+        int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 1000);
+        assertArrayEquals(new int[] {-1, -1}, result, "Element 1000 should not be found");
+    }
+
+    /**
+     * Test searching for the smallest element in the array.
+     */
+    @Test
+    void testFindSmallestElement() {
+        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}};
+
+        int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, -10);
+        assertArrayEquals(new int[] {0, 0}, result, "Element -10 should be found at (0, 0)");
+    }
+
+    /**
+     * Test searching for the largest element in the array.
+     */
+    @Test
+    void testFindLargestElement() {
+        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}};
+
+        int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 150);
+        assertArrayEquals(new int[] {4, 4}, result, "Element 150 should be found at (4, 4)");
+    }
+
+    /**
+     * Test searching in an empty array.
+     */
+    @Test
+    void testFindInEmptyArray() {
+        int[][] arr = {};
+
+        assertThrows(IllegalArgumentException.class, () -> { SaddlebackSearch.find(arr, 0, 0, 4); });
+    }
+
+    /**
+     * Test searching in a single element array that matches the search key.
+     */
+    @Test
+    void testFindSingleElementExists() {
+        int[][] arr = {{5}};
+
+        int[] result = SaddlebackSearch.find(arr, 0, 0, 5);
+        assertArrayEquals(new int[] {0, 0}, result, "Element 5 should be found at (0, 0)");
+    }
+
+    /**
+     * Test searching in a single element array that does not match the search key.
+     */
+    @Test
+    void testFindSingleElementNotExists() {
+        int[][] arr = {{5}};
+
+        int[] result = SaddlebackSearch.find(arr, 0, 0, 10);
+        assertArrayEquals(new int[] {-1, -1}, result, "Element 10 should not be found in single element array");
+    }
+}