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