Skip to content

Commit 0a7065d

Browse files
authored
Add tests, remove main in IterativeBinarySearch (#5667)
1 parent ba3d3bf commit 0a7065d

File tree

3 files changed

+118
-22
lines changed

3 files changed

+118
-22
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@
10051005
* [FibonacciSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java)
10061006
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
10071007
* [InterpolationSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java)
1008+
* [IterativeBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java)
10081009
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
10091010
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
10101011
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)

src/main/java/com/thealgorithms/searches/IterativeBinarySearch.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.thealgorithms.searches;
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4-
import java.util.Arrays;
5-
import java.util.Random;
6-
import java.util.stream.Stream;
74

85
/**
96
* Binary search is one of the most popular algorithms This class represents
@@ -55,23 +52,4 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
5552

5653
return -1;
5754
}
58-
59-
// Only a main method for test purpose
60-
public static void main(String[] args) {
61-
Random r = new Random();
62-
int size = 100;
63-
int maxElement = 100000;
64-
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[] ::new);
65-
66-
// the element that should be found
67-
Integer shouldBeFound = integers[r.nextInt(size - 1)];
68-
69-
IterativeBinarySearch search = new IterativeBinarySearch();
70-
int atIndex = search.find(integers, shouldBeFound);
71-
72-
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
73-
74-
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
75-
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
76-
}
7755
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the IterativeBinarySearch class.
9+
*/
10+
class IterativeBinarySearchTest {
11+
12+
/**
13+
* Test for basic binary search functionality when the element is found.
14+
*/
15+
@Test
16+
void testBinarySearchFound() {
17+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
18+
Integer[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
19+
Integer key = 128;
20+
int expectedIndex = 7; // Index of the key in the array
21+
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 7.");
22+
}
23+
24+
/**
25+
* Test for binary search when the element is not present in the array.
26+
*/
27+
@Test
28+
void testBinarySearchNotFound() {
29+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
30+
Integer[] array = {1, 2, 4, 8, 16};
31+
Integer key = 6; // Element not present in the array
32+
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
33+
}
34+
35+
/**
36+
* Test for binary search with the first element as the key.
37+
*/
38+
@Test
39+
void testBinarySearchFirstElement() {
40+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
41+
Integer[] array = {1, 2, 4, 8, 16};
42+
Integer key = 1; // First element
43+
assertEquals(0, binarySearch.find(array, key), "The index of the first element should be 0.");
44+
}
45+
46+
/**
47+
* Test for binary search with the last element as the key.
48+
*/
49+
@Test
50+
void testBinarySearchLastElement() {
51+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
52+
Integer[] array = {1, 2, 4, 8, 16};
53+
Integer key = 16; // Last element
54+
assertEquals(4, binarySearch.find(array, key), "The index of the last element should be 4.");
55+
}
56+
57+
/**
58+
* Test for binary search with a single element present.
59+
*/
60+
@Test
61+
void testBinarySearchSingleElementFound() {
62+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
63+
Integer[] array = {1};
64+
Integer key = 1; // Only element present
65+
assertEquals(0, binarySearch.find(array, key), "The index of the single element should be 0.");
66+
}
67+
68+
/**
69+
* Test for binary search with a single element not present.
70+
*/
71+
@Test
72+
void testBinarySearchSingleElementNotFound() {
73+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
74+
Integer[] array = {1};
75+
Integer key = 2; // Key not present
76+
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
77+
}
78+
79+
/**
80+
* Test for binary search with an empty array.
81+
*/
82+
@Test
83+
void testBinarySearchEmptyArray() {
84+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
85+
Integer[] array = {}; // Empty array
86+
Integer key = 1; // Key not present
87+
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in an empty array.");
88+
}
89+
90+
/**
91+
* Test for binary search on a large array.
92+
*/
93+
@Test
94+
void testBinarySearchLargeArray() {
95+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
96+
Integer[] array = new Integer[10000];
97+
for (int i = 0; i < array.length; i++) {
98+
array[i] = i * 2;
99+
} // Array from 0 to 19998, step 2
100+
Integer key = 9998; // Present in the array
101+
assertEquals(4999, binarySearch.find(array, key), "The index of the found element should be 4999.");
102+
}
103+
104+
/**
105+
* Test for binary search on large array with a non-existent key.
106+
*/
107+
@Test
108+
void testBinarySearchLargeArrayNotFound() {
109+
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
110+
Integer[] array = new Integer[10000];
111+
for (int i = 0; i < array.length; i++) {
112+
array[i] = i * 2;
113+
} // Array from 0 to 19998, step 2
114+
Integer key = 9999; // Key not present
115+
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
116+
}
117+
}

0 commit comments

Comments
 (0)