Skip to content

Commit 702ee53

Browse files
committed
Add tests, remove main in LowerBound
1 parent b54cc21 commit 702ee53

File tree

2 files changed

+83
-25
lines changed

2 files changed

+83
-25
lines changed

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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.thealgorithms.searches;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
import java.util.Arrays;
8+
import java.util.Random;
9+
10+
class LowerBoundTest {
11+
12+
/**
13+
* Test finding the lower bound for an element present in the array.
14+
*/
15+
@Test
16+
void testLowerBoundElementPresent() {
17+
Integer[] array = {1, 2, 3, 4, 5};
18+
LowerBound lowerBound = new LowerBound();
19+
20+
// Test for a value that is present
21+
assertEquals(2, lowerBound.find(array, 3), "Lower bound for 3 should be at index 2");
22+
assertEquals(0, lowerBound.find(array, 1), "Lower bound for 1 should be at index 0");
23+
assertEquals(4, lowerBound.find(array, 5), "Lower bound for 5 should be at index 4");
24+
}
25+
26+
/**
27+
* Test finding the lower bound for a value greater than the maximum element in the array.
28+
*/
29+
@Test
30+
void testLowerBoundElementGreaterThanMax() {
31+
Integer[] array = {1, 2, 3, 4, 5};
32+
LowerBound lowerBound = new LowerBound();
33+
34+
// Test for a value greater than the maximum
35+
assertEquals(4, lowerBound.find(array, 6), "Lower bound for 6 should be at index 4");
36+
}
37+
38+
/**
39+
* Test finding the lower bound for a value less than the minimum element in the array.
40+
*/
41+
@Test
42+
void testLowerBoundElementLessThanMin() {
43+
Integer[] array = {1, 2, 3, 4, 5};
44+
LowerBound lowerBound = new LowerBound();
45+
46+
// Test for a value less than the minimum
47+
assertEquals(0, lowerBound.find(array, 0), "Lower bound for 0 should be at index 0");
48+
}
49+
50+
/**
51+
* Test finding the lower bound for a non-existent value that falls between two elements.
52+
*/
53+
@Test
54+
void testLowerBoundNonExistentValue() {
55+
Integer[] array = {1, 2, 3, 4, 5};
56+
LowerBound lowerBound = new LowerBound();
57+
58+
// Test for a value that is not present
59+
assertEquals(4, lowerBound.find(array, 7), "Lower bound for 7 should be at index 4");
60+
assertEquals(0, lowerBound.find(array, 0), "Lower bound for 0 should be at index 0");
61+
}
62+
63+
/**
64+
* Test finding the lower bound in a large sorted array with random integers.
65+
*/
66+
@Test
67+
void testLowerBoundRandomNumbers() {
68+
Random random = new Random();
69+
int size = 100;
70+
Integer[] array = random.ints(size, 1, 100).sorted().boxed().toArray(Integer[]::new);
71+
72+
int target = random.nextInt(100) + 1; // Random target value between 1 and 100
73+
74+
Arrays.sort(array); // Ensure the array is sorted
75+
LowerBound lowerBound = new LowerBound();
76+
int lowerBoundIndex = lowerBound.find(array, target);
77+
78+
// Check if the found index is valid
79+
if (lowerBoundIndex < size) {
80+
assertTrue(array[lowerBoundIndex] >= target, "Lower bound index should point to a value >= target.");
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)