2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
4
5
- import org .junit .jupiter .api .Test ;
6
-
7
5
import java .util .stream .IntStream ;
6
+ import org .junit .jupiter .api .Test ;
8
7
9
8
/**
10
9
* Unit tests for the InterpolationSearch class.
@@ -20,8 +19,7 @@ void testInterpolationSearchFound() {
20
19
int [] array = {1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 };
21
20
int key = 128 ;
22
21
int expectedIndex = 7 ; // Index of the key in the array
23
- assertEquals (expectedIndex , interpolationSearch .find (array , key ),
24
- "The index of the found element should be 7." );
22
+ assertEquals (expectedIndex , interpolationSearch .find (array , key ), "The index of the found element should be 7." );
25
23
}
26
24
27
25
/**
@@ -32,9 +30,7 @@ void testInterpolationSearchNotFound() {
32
30
InterpolationSearch interpolationSearch = new InterpolationSearch ();
33
31
int [] array = {1 , 2 , 4 , 8 , 16 };
34
32
int key = 6 ; // Element not present in the array
35
- int expectedIndex = -1 ; // Key not found
36
- assertEquals (expectedIndex , interpolationSearch .find (array , key ),
37
- "The element should not be found in the array." );
33
+ assertEquals (-1 , interpolationSearch .find (array , key ), "The element should not be found in the array." );
38
34
}
39
35
40
36
/**
@@ -45,9 +41,7 @@ void testInterpolationSearchFirstElement() {
45
41
InterpolationSearch interpolationSearch = new InterpolationSearch ();
46
42
int [] array = {1 , 2 , 4 , 8 , 16 };
47
43
int key = 1 ; // First element
48
- int expectedIndex = 0 ; // Index of the key in the array
49
- assertEquals (expectedIndex , interpolationSearch .find (array , key ),
50
- "The index of the first element should be 0." );
44
+ assertEquals (0 , interpolationSearch .find (array , key ), "The index of the first element should be 0." );
51
45
}
52
46
53
47
/**
@@ -58,9 +52,7 @@ void testInterpolationSearchSingleElementNotFound() {
58
52
InterpolationSearch interpolationSearch = new InterpolationSearch ();
59
53
int [] array = {1 };
60
54
int key = 2 ; // Key not present
61
- int expectedIndex = -1 ; // Key not found
62
- assertEquals (expectedIndex , interpolationSearch .find (array , key ),
63
- "The element should not be found in the array." );
55
+ assertEquals (-1 , interpolationSearch .find (array , key ), "The element should not be found in the array." );
64
56
}
65
57
66
58
/**
@@ -71,9 +63,7 @@ void testInterpolationSearchEmptyArray() {
71
63
InterpolationSearch interpolationSearch = new InterpolationSearch ();
72
64
int [] array = {}; // Empty array
73
65
int key = 1 ; // Key not present
74
- int expectedIndex = -1 ; // Key not found
75
- assertEquals (expectedIndex , interpolationSearch .find (array , key ),
76
- "The element should not be found in an empty array." );
66
+ assertEquals (-1 , interpolationSearch .find (array , key ), "The element should not be found in an empty array." );
77
67
}
78
68
79
69
/**
@@ -84,9 +74,7 @@ void testInterpolationSearchLargeUniformArray() {
84
74
InterpolationSearch interpolationSearch = new InterpolationSearch ();
85
75
int [] array = IntStream .range (0 , 10000 ).map (i -> i * 2 ).toArray (); // Array from 0 to 19998, step 2
86
76
int key = 9998 ; // Last even number in the array
87
- int expectedIndex = 4999 ; // Index of the last element
88
- assertEquals (expectedIndex , interpolationSearch .find (array , key ),
89
- "The index of the last element should be 4999." );
77
+ assertEquals (4999 , interpolationSearch .find (array , key ), "The index of the last element should be 4999." );
90
78
}
91
79
92
80
/**
@@ -97,8 +85,6 @@ void testInterpolationSearchLargeNonUniformArray() {
97
85
InterpolationSearch interpolationSearch = new InterpolationSearch ();
98
86
int [] array = {1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 }; // Fibonacci numbers
99
87
int key = 21 ; // Present in the array
100
- int expectedIndex = 6 ; // Index of the key in the array
101
- assertEquals (expectedIndex , interpolationSearch .find (array , key ),
102
- "The index of the found element should be 6." );
88
+ assertEquals (6 , interpolationSearch .find (array , key ), "The index of the found element should be 6." );
103
89
}
104
90
}
0 commit comments