Skip to content

Commit 169fcdd

Browse files
committed
test(searches): cover null input cases in IterativeBinarySearch
1 parent 1edf319 commit 169fcdd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ void testBinarySearchEmptyArray() {
8787
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in an empty array.");
8888
}
8989

90+
/**
91+
* Test for binary search with a null array.
92+
*/
93+
@Test
94+
void testBinarySearchNullArray() {
95+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
96+
Integer key = 1;
97+
assertEquals(-1, binarySearch.find(null, key), "The element should not be found in a null array.");
98+
}
99+
100+
/**
101+
* Test for binary search with a null key.
102+
*/
103+
@Test
104+
void testBinarySearchNullKey() {
105+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
106+
Integer[] array = {1, 2, 4, 8, 16};
107+
assertEquals(-1, binarySearch.find(array, null), "A null search key should return -1.");
108+
}
109+
90110
/**
91111
* Test for binary search on a large array.
92112
*/

0 commit comments

Comments
 (0)