Skip to content

Commit 6123598

Browse files
committed
Add null key and null array test cases for IterativeBinarySearch
1 parent cfc0963 commit 6123598

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,23 @@ void testBinarySearchLargeArrayNotFound() {
114114
Integer key = 9999; // Key not present
115115
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
116116
}
117+
118+
/**
119+
* Test for binary search with a null key.
120+
*/
121+
@Test
122+
void testBinarySearchNullKey() {
123+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
124+
Integer[] array = {1, 2, 4, 8, 16};
125+
assertEquals(-1, binarySearch.find(array, null), "A null key should return -1.");
126+
}
127+
128+
/**
129+
* Test for binary search with a null array.
130+
*/
131+
@Test
132+
void testBinarySearchNullArray() {
133+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
134+
assertEquals(-1, binarySearch.find(null, 1), "A null array should return -1.");
135+
}
117136
}

0 commit comments

Comments
 (0)