@@ -121,4 +121,53 @@ void testFibonacciSearchLargeArray() {
121121 int expectedIndex = 9999 ;
122122 assertEquals (expectedIndex , fibonacciSearch .find (array , key ), "The index of the last element should be 9999." );
123123 }
124+
125+ /**
126+ * A key greater than every element used to throw {@link ArrayIndexOutOfBoundsException},
127+ * because the final probe read {@code array[offset + 1]} without checking the bound.
128+ */
129+ @ Test
130+ void testFibonacciSearchKeyGreaterThanLastElement () {
131+ FibonacciSearch fibonacciSearch = new FibonacciSearch ();
132+ for (int length = 1 ; length <= 50 ; length ++) {
133+ Integer [] array = new Integer [length ];
134+ for (int i = 0 ; i < length ; i ++) {
135+ array [i ] = i ;
136+ }
137+ assertEquals (-1 , fibonacciSearch .find (array , length ), "A key above the maximum should not be found for length " + length + "." );
138+ }
139+ }
140+
141+ /**
142+ * The final probe used reference equality, so a key that is equal but not identical to the
143+ * stored element was reported as missing. Values above 127 are outside the {@link Integer}
144+ * cache and therefore are not the same object as the boxed array element.
145+ */
146+ @ Test
147+ void testFibonacciSearchFindsEqualButNotIdenticalKey () {
148+ FibonacciSearch fibonacciSearch = new FibonacciSearch ();
149+ Integer [] array = {10 , 20 , 300 };
150+ assertEquals (2 , fibonacciSearch .find (array , Integer .valueOf (300 )), "The index of the found element should be 2." );
151+
152+ String [] words = {"a" , "b" , "c" };
153+ String equalButDistinct = new StringBuilder ("c" ).toString ();
154+ assertEquals (2 , fibonacciSearch .find (words , equalButDistinct ), "The index of the found element should be 2." );
155+ }
156+
157+ /**
158+ * Every element must be found regardless of the array length.
159+ */
160+ @ Test
161+ void testFibonacciSearchFindsEveryElement () {
162+ FibonacciSearch fibonacciSearch = new FibonacciSearch ();
163+ for (int length = 1 ; length <= 50 ; length ++) {
164+ Integer [] array = new Integer [length ];
165+ for (int i = 0 ; i < length ; i ++) {
166+ array [i ] = 1000 + i * 2 ;
167+ }
168+ for (int i = 0 ; i < length ; i ++) {
169+ assertEquals (i , fibonacciSearch .find (array , Integer .valueOf (1000 + i * 2 )), "Element at index " + i + " should be found for length " + length + "." );
170+ }
171+ }
172+ }
124173}
0 commit comments