|
| 1 | +package com.thealgorithms.searches; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +public class MetaBinarySearchTest { |
| 7 | + |
| 8 | + @Test |
| 9 | + public void testElementFound() { |
| 10 | + int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21}; |
| 11 | + int target = 13; |
| 12 | + int expectedIndex = 6; |
| 13 | + |
| 14 | + int result = MetaBinarySearch.metaBinarySearch(sortedArray, target); |
| 15 | + assertEquals(expectedIndex, result, "The target element should be found at index 6"); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testElementNotFound() { |
| 20 | + int[] sortedArray = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; |
| 21 | + int target = 7; |
| 22 | + |
| 23 | + int result = MetaBinarySearch.metaBinarySearch(sortedArray, target); |
| 24 | + assertEquals(-1, result, "The target element should not be found in the array"); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testSingleElementArrayFound() { |
| 29 | + int[] singleElementArray = {5}; |
| 30 | + int target = 5; |
| 31 | + |
| 32 | + int result = MetaBinarySearch.metaBinarySearch(singleElementArray, target); |
| 33 | + assertEquals(0, result, "The target element should be found at index 0 in a single-element array"); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testSingleElementArrayNotFound() { |
| 38 | + int[] singleElementArray = {3}; |
| 39 | + int target = 5; |
| 40 | + |
| 41 | + int result = MetaBinarySearch.metaBinarySearch(singleElementArray, target); |
| 42 | + assertEquals(-1, result, "The target element should not be found in a single-element array"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testEmptyArray() { |
| 47 | + int[] emptyArray = {}; |
| 48 | + int target = 10; |
| 49 | + |
| 50 | + int result = MetaBinarySearch.metaBinarySearch(emptyArray, target); |
| 51 | + assertEquals(-1, result, "The target element should not be found in an empty array"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testFirstElement() { |
| 56 | + int[] sortedArray = {1, 2, 3, 4, 5}; |
| 57 | + int target = 1; |
| 58 | + |
| 59 | + int result = MetaBinarySearch.metaBinarySearch(sortedArray, target); |
| 60 | + assertEquals(0, result, "The target element should be found at the first index"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testLastElement() { |
| 65 | + int[] sortedArray = {1, 2, 3, 4, 5}; |
| 66 | + int target = 5; |
| 67 | + |
| 68 | + int result = MetaBinarySearch.metaBinarySearch(sortedArray, target); |
| 69 | + assertEquals(4, result, "The target element should be found at the last index"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testLargeArray() { |
| 74 | + int[] largeArray = new int[1000]; |
| 75 | + for (int i = 0; i < 1000; i++) { |
| 76 | + largeArray[i] = i * 2; // Even numbers from 0 to 1998 |
| 77 | + } |
| 78 | + int target = 998; |
| 79 | + |
| 80 | + int result = MetaBinarySearch.metaBinarySearch(largeArray, target); |
| 81 | + assertEquals(499, result, "The target element should be found at index 499"); |
| 82 | + } |
| 83 | +} |
0 commit comments