- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathBinarySearchTest.java
108 lines (97 loc) · 3.78 KB
/
BinarySearchTest.java
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
packagecom.thealgorithms.searches;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importjava.util.stream.IntStream;
importorg.junit.jupiter.api.Test;
/**
* Unit tests for the BinarySearch class.
*/
classBinarySearchTest {
/**
* Test for basic binary search functionality.
*/
@Test
voidtestBinarySearchFound() {
BinarySearchbinarySearch = newBinarySearch();
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
intkey = 7;
intexpectedIndex = 6; // Index of the key in the array
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 6.");
}
/**
* Test for binary search when the element is not present.
*/
@Test
voidtestBinarySearchNotFound() {
BinarySearchbinarySearch = newBinarySearch();
Integer[] array = {1, 2, 3, 4, 5};
intkey = 6; // Element not present in the array
intexpectedIndex = -1; // Key not found
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
}
/**
* Test for binary search with first element as the key.
*/
@Test
voidtestBinarySearchFirstElement() {
BinarySearchbinarySearch = newBinarySearch();
Integer[] array = {1, 2, 3, 4, 5};
intkey = 1; // First element
intexpectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the first element should be 0.");
}
/**
* Test for binary search with last element as the key.
*/
@Test
voidtestBinarySearchLastElement() {
BinarySearchbinarySearch = newBinarySearch();
Integer[] array = {1, 2, 3, 4, 5};
intkey = 5; // Last element
intexpectedIndex = 4; // Index of the key in the array
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 4.");
}
/**
* Test for binary search with a single element present.
*/
@Test
voidtestBinarySearchSingleElementFound() {
BinarySearchbinarySearch = newBinarySearch();
Integer[] array = {1};
intkey = 1; // Only element present
intexpectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the single element should be 0.");
}
/**
* Test for binary search with a single element not present.
*/
@Test
voidtestBinarySearchSingleElementNotFound() {
BinarySearchbinarySearch = newBinarySearch();
Integer[] array = {1};
intkey = 2; // Key not present
intexpectedIndex = -1; // Key not found
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
}
/**
* Test for binary search with an empty array.
*/
@Test
voidtestBinarySearchEmptyArray() {
BinarySearchbinarySearch = newBinarySearch();
Integer[] array = {}; // Empty array
intkey = 1; // Key not present
intexpectedIndex = -1; // Key not found
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in an empty array.");
}
/**
* Test for binary search on large array.
*/
@Test
voidtestBinarySearchLargeArray() {
BinarySearchbinarySearch = newBinarySearch();
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
intkey = 9999; // Last element
intexpectedIndex = 9999; // Index of the last element
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999.");
}
}