Skip to content

Commit d2744b5

Browse files
authored
Fixes #7350: Binary Search Reliability Improvement (#7351)
1 parent bdbbece commit d2744b5

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/test/java/com/thealgorithms/searches/BinarySearchTest.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,90 @@ void testBinarySearchLargeArray() {
105105
int expectedIndex = 9999; // Index of the last element
106106
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999.");
107107
}
108+
109+
/**
110+
* Test for binary search with null array.
111+
*/
112+
@Test
113+
void testBinarySearchNullArray() {
114+
BinarySearch binarySearch = new BinarySearch();
115+
Integer[] array = null;
116+
int key = 5; // Key to search
117+
int expectedIndex = -1; // Key not found
118+
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in a null array.");
119+
}
120+
121+
/**
122+
* Test for binary search with duplicate elements.
123+
*/
124+
@Test
125+
void testBinarySearchWithDuplicates() {
126+
BinarySearch binarySearch = new BinarySearch();
127+
Integer[] array = {1, 2, 2, 2, 3};
128+
int key = 2; // Element present multiple times
129+
130+
int result = binarySearch.find(array, key);
131+
assertEquals(2, array[result], "The returned index should contain the searched element.");
132+
}
133+
134+
/**
135+
* Test for binary search where all elements are the same.
136+
*/
137+
@Test
138+
void testBinarySearchAllElementsSame() {
139+
BinarySearch binarySearch = new BinarySearch();
140+
Integer[] array = {5, 5, 5, 5, 5};
141+
int key = 5; // All elements match
142+
143+
int result = binarySearch.find(array, key);
144+
assertEquals(5, array[result], "The returned index should contain the searched element.");
145+
}
146+
147+
/**
148+
* Test for binary search with negative numbers.
149+
*/
150+
@Test
151+
void testBinarySearchNegativeNumbers() {
152+
BinarySearch binarySearch = new BinarySearch();
153+
Integer[] array = {-10, -5, 0, 5, 10};
154+
int key = -5; // Element present
155+
int expectedIndex = 1; // Index of the element
156+
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the element should be 1.");
157+
}
158+
159+
/**
160+
* Test for binary search when key is smaller than all elements.
161+
*/
162+
@Test
163+
void testBinarySearchKeySmallerThanAll() {
164+
BinarySearch binarySearch = new BinarySearch();
165+
Integer[] array = {10, 20, 30};
166+
int key = 5; // Smaller than all elements
167+
int expectedIndex = -1; // Key not found
168+
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
169+
}
170+
171+
/**
172+
* Test for binary search when key is larger than all elements.
173+
*/
174+
@Test
175+
void testBinarySearchKeyLargerThanAll() {
176+
BinarySearch binarySearch = new BinarySearch();
177+
Integer[] array = {10, 20, 30};
178+
int key = 40; // Larger than all elements
179+
int expectedIndex = -1; // Key not found
180+
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
181+
}
182+
183+
/**
184+
* Test for binary search with String array.
185+
*/
186+
@Test
187+
void testBinarySearchStrings() {
188+
BinarySearch binarySearch = new BinarySearch();
189+
String[] array = {"apple", "banana", "cherry", "date"};
190+
String key = "cherry"; // Element present
191+
int expectedIndex = 2; // Index of the element
192+
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the element should be 2.");
193+
}
108194
}

0 commit comments

Comments
 (0)