Skip to content

Commit b81671e

Browse files
authored
Add tests, remove main in LowerBound (#5672)
1 parent 138793d commit b81671e

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@
10361036
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
10371037
* [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java)
10381038
* [LinearSearchThreadTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchThreadTest.java)
1039+
* [LowerBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LowerBoundTest.java)
10391040
* [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java)
10401041
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
10411042
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)

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

Lines changed: 0 additions & 25 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.Random;
5-
import java.util.concurrent.ThreadLocalRandom;
6-
import java.util.stream.IntStream;
74

85
/**
96
* The LowerBound method is used to return an index pointing to the first
@@ -25,28 +22,6 @@
2522
*/
2623
class LowerBound implements SearchAlgorithm {
2724

28-
// Driver Program
29-
public static void main(String[] args) {
30-
// Just generate data
31-
Random r = ThreadLocalRandom.current();
32-
33-
int size = 100;
34-
int maxElement = 100000;
35-
36-
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
37-
38-
// The element for which the lower bound is to be found
39-
int val = integers[r.nextInt(size - 1)] + 1;
40-
41-
LowerBound search = new LowerBound();
42-
int atIndex = search.find(integers, val);
43-
44-
System.out.printf("Val: %d. Lower Bound Found %d at index %d. An array length %d%n", val, integers[atIndex], atIndex, size);
45-
46-
boolean toCheck = integers[atIndex] >= val || integers[size - 1] < val;
47-
System.out.printf("Lower Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
48-
}
49-
5025
/**
5126
* @param array is an array where the LowerBound value is to be found
5227
* @param key is an element for which the LowerBound is to be found
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
class LowerBoundTest {
8+
9+
/**
10+
* Test finding the lower bound for an element present in the array.
11+
*/
12+
@Test
13+
void testLowerBoundElementPresent() {
14+
Integer[] array = {1, 2, 3, 4, 5};
15+
LowerBound lowerBound = new LowerBound();
16+
17+
// Test for a value that is present
18+
assertEquals(2, lowerBound.find(array, 3), "Lower bound for 3 should be at index 2");
19+
assertEquals(0, lowerBound.find(array, 1), "Lower bound for 1 should be at index 0");
20+
assertEquals(4, lowerBound.find(array, 5), "Lower bound for 5 should be at index 4");
21+
}
22+
23+
/**
24+
* Test finding the lower bound for a value greater than the maximum element in the array.
25+
*/
26+
@Test
27+
void testLowerBoundElementGreaterThanMax() {
28+
Integer[] array = {1, 2, 3, 4, 5};
29+
LowerBound lowerBound = new LowerBound();
30+
31+
// Test for a value greater than the maximum
32+
assertEquals(4, lowerBound.find(array, 6), "Lower bound for 6 should be at index 4");
33+
}
34+
35+
/**
36+
* Test finding the lower bound for a value less than the minimum element in the array.
37+
*/
38+
@Test
39+
void testLowerBoundElementLessThanMin() {
40+
Integer[] array = {1, 2, 3, 4, 5};
41+
LowerBound lowerBound = new LowerBound();
42+
43+
// Test for a value less than the minimum
44+
assertEquals(0, lowerBound.find(array, 0), "Lower bound for 0 should be at index 0");
45+
}
46+
47+
/**
48+
* Test finding the lower bound for a non-existent value that falls between two elements.
49+
*/
50+
@Test
51+
void testLowerBoundNonExistentValue() {
52+
Integer[] array = {1, 2, 3, 4, 5};
53+
LowerBound lowerBound = new LowerBound();
54+
55+
// Test for a value that is not present
56+
assertEquals(4, lowerBound.find(array, 7), "Lower bound for 7 should be at index 4");
57+
assertEquals(0, lowerBound.find(array, 0), "Lower bound for 0 should be at index 0");
58+
}
59+
}

0 commit comments

Comments
 (0)