Skip to content

Commit 68862a7

Browse files
committed
Add tests, remove main in BinarySearch
1 parent b54cc21 commit 68862a7

File tree

2 files changed

+116
-26
lines changed

2 files changed

+116
-26
lines changed

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +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.concurrent.ThreadLocalRandom;
7-
import java.util.stream.IntStream;
84

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

0 commit comments

Comments
 (0)