-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathIterativeBinarySearchTest.java
More file actions
136 lines (122 loc) · 4.72 KB
/
IterativeBinarySearchTest.java
File metadata and controls
136 lines (122 loc) · 4.72 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
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Unit tests for the IterativeBinarySearch class.
*/
class IterativeBinarySearchTest {
/**
* Test for basic binary search functionality when the element is found.
*/
@Test
void testBinarySearchFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
Integer key = 128;
int expectedIndex = 7; // Index of the key in the array
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 7.");
}
/**
* Test for binary search when the element is not present in the array.
*/
@Test
void testBinarySearchNotFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16};
Integer key = 6; // Element not present in the array
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
}
/**
* Test for binary search with the first element as the key.
*/
@Test
void testBinarySearchFirstElement() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16};
Integer key = 1; // First element
assertEquals(0, binarySearch.find(array, key), "The index of the first element should be 0.");
}
/**
* Test for binary search with the last element as the key.
*/
@Test
void testBinarySearchLastElement() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16};
Integer key = 16; // Last element
assertEquals(4, binarySearch.find(array, key), "The index of the last element should be 4.");
}
/**
* Test for binary search with a single element present.
*/
@Test
void testBinarySearchSingleElementFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1};
Integer key = 1; // Only element present
assertEquals(0, binarySearch.find(array, key), "The index of the single element should be 0.");
}
/**
* Test for binary search with a single element not present.
*/
@Test
void testBinarySearchSingleElementNotFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1};
Integer key = 2; // Key not present
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
}
/**
* Test for binary search with an empty array.
*/
@Test
void testBinarySearchEmptyArray() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {}; // Empty array
Integer key = 1; // Key not present
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in an empty array.");
}
/**
* Test for binary search on a large array.
*/
@Test
void testBinarySearchLargeArray() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = new Integer[10000];
for (int i = 0; i < array.length; i++) {
array[i] = i * 2;
} // Array from 0 to 19998, step 2
Integer key = 9998; // Present in the array
assertEquals(4999, binarySearch.find(array, key), "The index of the found element should be 4999.");
}
/**
* Test for binary search on large array with a non-existent key.
*/
@Test
void testBinarySearchLargeArrayNotFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = new Integer[10000];
for (int i = 0; i < array.length; i++) {
array[i] = i * 2;
} // Array from 0 to 19998, step 2
Integer key = 9999; // Key not present
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
}
/**
* Test for binary search with a null key.
*/
@Test
void testBinarySearchNullKey() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16};
assertEquals(-1, binarySearch.find(array, null), "A null key should return -1.");
}
/**
* Test for binary search with a null array.
*/
@Test
void testBinarySearchNullArray() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
assertEquals(-1, binarySearch.find(null, 1), "A null array should return -1.");
}
}