@@ -81,4 +81,46 @@ void testExponentialSearchLargeArray() {
8181 int expectedIndex = 9999 ;
8282 assertEquals (expectedIndex , exponentialSearch .find (array , key ), "The index of the last element should be 9999." );
8383 }
84+
85+ /**
86+ * An element sitting exactly on the doubling boundary used to be reported as missing, because
87+ * the binary search was handed {@code range} as its exclusive upper bound instead of
88+ * {@code range + 1}.
89+ */
90+ @ Test
91+ void testExponentialSearchElementOnRangeBoundary () {
92+ ExponentialSearch exponentialSearch = new ExponentialSearch ();
93+ Integer [] array = {-25 , -9 , 8 , 21 };
94+ assertEquals (2 , exponentialSearch .find (array , 8 ), "The index of the found element should be 2." );
95+ }
96+
97+ /**
98+ * Every element must be found regardless of the array length.
99+ */
100+ @ Test
101+ void testExponentialSearchFindsEveryElement () {
102+ ExponentialSearch exponentialSearch = new ExponentialSearch ();
103+ for (int length = 1 ; length <= 50 ; length ++) {
104+ Integer [] array = new Integer [length ];
105+ for (int i = 0 ; i < length ; i ++) {
106+ array [i ] = i * 2 ;
107+ }
108+ for (int i = 0 ; i < length ; i ++) {
109+ assertEquals (i , exponentialSearch .find (array , i * 2 ), "Element at index " + i + " should be found for length " + length + "." );
110+ }
111+ }
112+ }
113+
114+ /**
115+ * A missing key has to yield -1 rather than the negative insertion point that
116+ * {@link java.util.Arrays#binarySearch} returns.
117+ */
118+ @ Test
119+ void testExponentialSearchNotFoundReturnsMinusOne () {
120+ ExponentialSearch exponentialSearch = new ExponentialSearch ();
121+ Integer [] array = {1 , 3 , 5 , 7 , 9 , 11 };
122+ assertEquals (-1 , exponentialSearch .find (array , 4 ), "A key inside the range but absent should give -1." );
123+ assertEquals (-1 , exponentialSearch .find (array , 0 ), "A key below the minimum should give -1." );
124+ assertEquals (-1 , exponentialSearch .find (array , 12 ), "A key above the maximum should give -1." );
125+ }
84126}
0 commit comments