Skip to content

Commit ca5fbbf

Browse files
authored
Add tests, remove main in BinarySearch (#5663)
1 parent ac2c88c commit ca5fbbf

File tree

3 files changed

+109
-26
lines changed

3 files changed

+109
-26
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@
997997
* [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
998998
* searches
999999
* [BinarySearch2dArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java)
1000+
* [BinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearchTest.java)
10001001
* [BM25InvertedIndexTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java)
10011002
* [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java)
10021003
* [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java)

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: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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), "The index of the found element should be 6.");
23+
}
24+
25+
/**
26+
* Test for binary search when the element is not present.
27+
*/
28+
@Test
29+
void testBinarySearchNotFound() {
30+
BinarySearch binarySearch = new BinarySearch();
31+
Integer[] array = {1, 2, 3, 4, 5};
32+
int key = 6; // Element not present in the array
33+
int expectedIndex = -1; // Key not found
34+
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
35+
}
36+
37+
/**
38+
* Test for binary search with first element as the key.
39+
*/
40+
@Test
41+
void testBinarySearchFirstElement() {
42+
BinarySearch binarySearch = new BinarySearch();
43+
Integer[] array = {1, 2, 3, 4, 5};
44+
int key = 1; // First element
45+
int expectedIndex = 0; // Index of the key in the array
46+
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the first element should be 0.");
47+
}
48+
49+
/**
50+
* Test for binary search with last element as the key.
51+
*/
52+
@Test
53+
void testBinarySearchLastElement() {
54+
BinarySearch binarySearch = new BinarySearch();
55+
Integer[] array = {1, 2, 3, 4, 5};
56+
int key = 5; // Last element
57+
int expectedIndex = 4; // Index of the key in the array
58+
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 4.");
59+
}
60+
61+
/**
62+
* Test for binary search with a single element present.
63+
*/
64+
@Test
65+
void testBinarySearchSingleElementFound() {
66+
BinarySearch binarySearch = new BinarySearch();
67+
Integer[] array = {1};
68+
int key = 1; // Only element present
69+
int expectedIndex = 0; // Index of the key in the array
70+
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the single element should be 0.");
71+
}
72+
73+
/**
74+
* Test for binary search with a single element not present.
75+
*/
76+
@Test
77+
void testBinarySearchSingleElementNotFound() {
78+
BinarySearch binarySearch = new BinarySearch();
79+
Integer[] array = {1};
80+
int key = 2; // Key not present
81+
int expectedIndex = -1; // Key not found
82+
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
83+
}
84+
85+
/**
86+
* Test for binary search with an empty array.
87+
*/
88+
@Test
89+
void testBinarySearchEmptyArray() {
90+
BinarySearch binarySearch = new BinarySearch();
91+
Integer[] array = {}; // Empty array
92+
int key = 1; // Key not present
93+
int expectedIndex = -1; // Key not found
94+
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in an empty array.");
95+
}
96+
97+
/**
98+
* Test for binary search on large array.
99+
*/
100+
@Test
101+
void testBinarySearchLargeArray() {
102+
BinarySearch binarySearch = new BinarySearch();
103+
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
104+
int key = 9999; // Last element
105+
int expectedIndex = 9999; // Index of the last element
106+
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999.");
107+
}
108+
}

0 commit comments

Comments
 (0)