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