11package com .thealgorithms .searches ;
22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertTrue ;
45
56import java .util .stream .IntStream ;
67import org .junit .jupiter .api .Test ;
@@ -58,6 +59,17 @@ void testExponentialSearchSingleElementFound() {
5859 assertEquals (expectedIndex , exponentialSearch .find (array , key ), "The index of the single element should be 0." );
5960 }
6061
62+ /**
63+ * Test for exponential search with a single element not present.
64+ */
65+ @ Test
66+ void testExponentialSearchSingleElementNotFound () {
67+ ExponentialSearch exponentialSearch = new ExponentialSearch ();
68+ Integer [] array = {1 };
69+ int key = 2 ; // Key not present
70+ assertEquals (-1 , exponentialSearch .find (array , key ), "The element should not be found in the array." );
71+ }
72+
6173 /**
6274 * Test for exponential search with an empty array.
6375 */
@@ -70,6 +82,25 @@ void testExponentialSearchEmptyArray() {
7082 assertEquals (expectedIndex , exponentialSearch .find (array , key ), "The element should not be found in an empty array." );
7183 }
7284
85+ /**
86+ * Test for exponential search with a null array.
87+ */
88+ @ Test
89+ void testExponentialSearchNullArray () {
90+ ExponentialSearch exponentialSearch = new ExponentialSearch ();
91+ assertEquals (-1 , exponentialSearch .find (null , 5 ), "Should return -1 for null array." );
92+ }
93+
94+ /**
95+ * Test for exponential search with a null key.
96+ */
97+ @ Test
98+ void testExponentialSearchNullKey () {
99+ ExponentialSearch exponentialSearch = new ExponentialSearch ();
100+ Integer [] array = {1 , 2 , 3 , 4 , 5 };
101+ assertEquals (-1 , exponentialSearch .find (array , null ), "Should return -1 for null key." );
102+ }
103+
73104 /**
74105 * Test for exponential search on large array.
75106 */
@@ -123,4 +154,26 @@ void testExponentialSearchNotFoundReturnsMinusOne() {
123154 assertEquals (-1 , exponentialSearch .find (array , 0 ), "A key below the minimum should give -1." );
124155 assertEquals (-1 , exponentialSearch .find (array , 12 ), "A key above the maximum should give -1." );
125156 }
157+
158+ /**
159+ * Test for duplicate elements in the array.
160+ */
161+ @ Test
162+ void testExponentialSearchDuplicates () {
163+ ExponentialSearch exponentialSearch = new ExponentialSearch ();
164+ Integer [] array = {1 , 2 , 2 , 2 , 3 };
165+ int index = exponentialSearch .find (array , 2 );
166+ assertTrue (index >= 1 && index <= 3 , "Index should be one of the occurrences of 2." );
167+ }
168+
169+ /**
170+ * Test for generic Comparable types such as Strings.
171+ */
172+ @ Test
173+ void testExponentialSearchStrings () {
174+ ExponentialSearch exponentialSearch = new ExponentialSearch ();
175+ String [] words = {"apple" , "banana" , "cherry" , "date" , "elderberry" };
176+ assertEquals (2 , exponentialSearch .find (words , "cherry" ), "Should find 'cherry' at index 2." );
177+ assertEquals (-1 , exponentialSearch .find (words , "fig" ), "Should return -1 for absent word." );
178+ }
126179}
0 commit comments