Skip to content

Commit d78df2f

Browse files
authored
Create MetaBinarySearchTest.java
1 parent d3b112f commit d78df2f

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
class MetaBinarySearchTest {
7+
8+
private final MetaBinarySearch metaBinarySearch = new MetaBinarySearch();
9+
10+
@Test
11+
void testTargetFound() {
12+
int[] sortedArray = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21 };
13+
int target = 13;
14+
int expectedIndex = 6; // 13 is at index 6
15+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
16+
}
17+
18+
@Test
19+
void testTargetNotFound() {
20+
int[] sortedArray = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21 };
21+
int target = 14; // 14 is not in the array
22+
int expectedIndex = -1;
23+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
24+
}
25+
26+
@Test
27+
void testEmptyArray() {
28+
int[] sortedArray = {};
29+
int target = 5;
30+
int expectedIndex = -1;
31+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
32+
}
33+
34+
@Test
35+
void testSingleElementArrayFound() {
36+
int[] sortedArray = { 10 };
37+
int target = 10;
38+
int expectedIndex = 0;
39+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
40+
}
41+
42+
@Test
43+
void testSingleElementArrayNotFound() {
44+
int[] sortedArray = { 10 };
45+
int target = 5;
46+
int expectedIndex = -1;
47+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
48+
}
49+
50+
@Test
51+
void testTargetAtStart() {
52+
int[] sortedArray = { 2, 4, 6, 8, 10 };
53+
int target = 2;
54+
int expectedIndex = 0; // 2 is at the start
55+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
56+
}
57+
58+
@Test
59+
void testTargetAtEnd() {
60+
int[] sortedArray = { 2, 4, 6, 8, 10 };
61+
int target = 10;
62+
int expectedIndex = 4; // 10 is at the end
63+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
64+
}
65+
66+
@Test
67+
void testArrayWithNegativeNumbers() {
68+
int[] sortedArray = { -10, -5, 0, 5, 10, 15 };
69+
int target = 5;
70+
int expectedIndex = 3;
71+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
72+
}
73+
74+
@Test
75+
void testArrayWithAllNegativeNumbers() {
76+
int[] sortedArray = { -20, -15, -10, -5, -1 };
77+
int target = -10;
78+
int expectedIndex = 2;
79+
assertEquals(expectedIndex, metaBinarySearch.metaBinarySearch(sortedArray, target));
80+
}
81+
82+
@Test
83+
void testArrayWithDuplicates() {
84+
int[] sortedArray = { 1, 3, 5, 5, 5, 7, 9 };
85+
int target = 5;
86+
// Meta Binary Search can return any index where 5 is found (2, 3, or 4).
87+
int actualIndex = metaBinarySearch.metaBinarySearch(sortedArray, target);
88+
// Assert that the index is valid (any occurrence of 5)
89+
boolean isValidIndex = actualIndex == 2 || actualIndex == 3 || actualIndex == 4;
90+
assertEquals(true, isValidIndex, "The index should be any occurrence of 5 (2, 3, or 4).");
91+
}
92+
93+
}

0 commit comments

Comments
 (0)