forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciSearchTest.java
More file actions
173 lines (156 loc) · 6.78 KB
/
Copy pathFibonacciSearchTest.java
File metadata and controls
173 lines (156 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
/**
* Unit tests for the FibonacciSearch class.
*/
class FibonacciSearchTest {
/**
* Test for basic Fibonacci search functionality.
*/
@Test
void testFibonacciSearchFound() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
int key = 128;
int expectedIndex = 7; // Index of the key in the array
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the found element should be 7.");
}
/**
* Test for Fibonacci search when the element is not present.
*/
@Test
void testFibonacciSearchNotFound() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16};
int key = 6; // Element not present in the array
int expectedIndex = -1; // Key not found
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The element should not be found in the array.");
}
/**
* Test for Fibonacci search with the first element as the key.
*/
@Test
void testFibonacciSearchFirstElement() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16};
int key = 1; // First element
int expectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the first element should be 0.");
}
/**
* Test for Fibonacci search with the last element as the key.
*/
@Test
void testFibonacciSearchLastElement() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16};
int key = 16; // Last element
int expectedIndex = 4; // Index of the key in the array
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 4.");
}
/**
* Test for Fibonacci search with a single element present.
*/
@Test
void testFibonacciSearchSingleElementFound() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1};
int key = 1; // Only element present
int expectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the single element should be 0.");
}
/**
* Test for Fibonacci search with a single element not present.
*/
@Test
void testFibonacciSearchSingleElementNotFound() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1};
int key = 2; // Key not present
int expectedIndex = -1; // Key not found
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The element should not be found in the array.");
}
/**
* Test for Fibonacci search with an empty array.
*/
@Test
void testFibonacciSearchEmptyArray() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {}; // Empty array
int key = 1; // Key not present
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "An empty array should throw an IllegalArgumentException.");
}
@Test
void testFibonacciSearchUnsortedArray() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {2, 1, 4, 3, 6, 5};
int key = 3; // Key not present
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "An unsorted array should throw an IllegalArgumentException.");
}
@Test
void testFibonacciSearchNullKey() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {1, 2, 4, 8, 16};
Integer key = null; // Null key
assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "A null key should throw an IllegalArgumentException.");
}
/**
* Test for Fibonacci search on large array.
*/
@Test
void testFibonacciSearchLargeArray() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
int key = 9999;
int expectedIndex = 9999;
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 9999.");
}
/**
* A key greater than every element used to throw {@link ArrayIndexOutOfBoundsException},
* because the final probe read {@code array[offset + 1]} without checking the bound.
*/
@Test
void testFibonacciSearchKeyGreaterThanLastElement() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
for (int length = 1; length <= 50; length++) {
Integer[] array = new Integer[length];
for (int i = 0; i < length; i++) {
array[i] = i;
}
assertEquals(-1, fibonacciSearch.find(array, length), "A key above the maximum should not be found for length " + length + ".");
}
}
/**
* The final probe used reference equality, so a key that is equal but not identical to the
* stored element was reported as missing. Values above 127 are outside the {@link Integer}
* cache and therefore are not the same object as the boxed array element.
*/
@Test
void testFibonacciSearchFindsEqualButNotIdenticalKey() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
Integer[] array = {10, 20, 300};
assertEquals(2, fibonacciSearch.find(array, Integer.valueOf(300)), "The index of the found element should be 2.");
String[] words = {"a", "b", "c"};
String equalButDistinct = new StringBuilder("c").toString();
assertEquals(2, fibonacciSearch.find(words, equalButDistinct), "The index of the found element should be 2.");
}
/**
* Every element must be found regardless of the array length.
*/
@Test
void testFibonacciSearchFindsEveryElement() {
FibonacciSearch fibonacciSearch = new FibonacciSearch();
for (int length = 1; length <= 50; length++) {
Integer[] array = new Integer[length];
for (int i = 0; i < length; i++) {
array[i] = 1000 + i * 2;
}
for (int i = 0; i < length; i++) {
assertEquals(i, fibonacciSearch.find(array, Integer.valueOf(1000 + i * 2)), "Element at index " + i + " should be found for length " + length + ".");
}
}
}
}