Skip to content

Commit b1aeac5

Browse files
authored
Add tests, remove main in UpperBound (#5679)
1 parent 4ec2701 commit b1aeac5

File tree

3 files changed

+100
-25
lines changed

3 files changed

+100
-25
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@
10251025
* [TernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TernarySearchTest.java)
10261026
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
10271027
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
1028+
* [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
10281029
* sorts
10291030
* [BeadSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java)
10301031
* [BinaryInsertionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java)

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

-25
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 UpperBound method is used to return an index pointing to the first
@@ -25,28 +22,6 @@
2522
*/
2623
class UpperBound 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 upper bound is to be found
39-
int val = integers[r.nextInt(size - 1)] + 1;
40-
41-
UpperBound search = new UpperBound();
42-
int atIndex = search.find(integers, val);
43-
44-
System.out.printf("Val: %d. Upper 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("Upper 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 UpperBound value is to be found
5227
* @param key is an element for which the UpperBound is to be found
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.Random;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
class UpperBoundTest {
11+
12+
private UpperBound upperBound;
13+
private Integer[] sortedArray;
14+
15+
@BeforeEach
16+
void setUp() {
17+
upperBound = new UpperBound();
18+
19+
// Generate a sorted array of random integers for testing
20+
Random random = new Random();
21+
int size = 100;
22+
int maxElement = 100;
23+
sortedArray = random.ints(size, 1, maxElement)
24+
.distinct() // Ensure all elements are unique
25+
.sorted()
26+
.boxed()
27+
.toArray(Integer[] ::new);
28+
}
29+
30+
@Test
31+
void testUpperBoundFound() {
32+
int key = sortedArray[sortedArray.length - 1] + 1; // Test with a key larger than max element
33+
int index = upperBound.find(sortedArray, key);
34+
35+
// The upper bound should be equal to the length of the array
36+
assertEquals(sortedArray.length - 1, index, "Upper bound for a larger key should be the size of the array.");
37+
}
38+
39+
@Test
40+
void testUpperBoundExactMatch() {
41+
int key = sortedArray[sortedArray.length / 2]; // Choose a key from the middle of the array
42+
int index = upperBound.find(sortedArray, key);
43+
44+
// The index should point to the first element greater than the key
45+
assertTrue(index < sortedArray.length, "Upper bound should not exceed array length.");
46+
assertTrue(sortedArray[index] > key, "The element at the index should be greater than the key.");
47+
}
48+
49+
@Test
50+
void testUpperBoundMultipleValues() {
51+
Integer[] arrayWithDuplicates = new Integer[] {1, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9}; // Test array with duplicates
52+
int key = 4;
53+
int index = upperBound.find(arrayWithDuplicates, key);
54+
55+
assertTrue(index < arrayWithDuplicates.length, "Upper bound index should be valid.");
56+
assertEquals(6, index, "The upper bound for 4 should be the index of the first 5.");
57+
assertTrue(arrayWithDuplicates[index] > key, "Element at the upper bound index should be greater than the key.");
58+
}
59+
60+
@Test
61+
void testUpperBoundLowerThanMin() {
62+
int key = 0; // Test with a key lower than the minimum element
63+
int index = upperBound.find(sortedArray, key);
64+
65+
assertEquals(0, index, "Upper bound for a key lower than minimum should be 0.");
66+
assertTrue(sortedArray[index] > key, "The element at index 0 should be greater than the key.");
67+
}
68+
69+
@Test
70+
void testUpperBoundHigherThanMax() {
71+
int key = sortedArray[sortedArray.length - 1] + 1; // Test with a key higher than maximum element
72+
int index = upperBound.find(sortedArray, key);
73+
74+
assertEquals(sortedArray.length - 1, index, "Upper bound for a key higher than maximum should be the size of the array.");
75+
}
76+
77+
@Test
78+
void testUpperBoundEdgeCase() {
79+
// Edge case: empty array
80+
Integer[] emptyArray = {};
81+
int index = upperBound.find(emptyArray, 5);
82+
83+
assertEquals(0, index, "Upper bound for an empty array should be 0.");
84+
}
85+
86+
@Test
87+
void testUpperBoundSingleElementArray() {
88+
Integer[] singleElementArray = {10};
89+
int index = upperBound.find(singleElementArray, 5);
90+
91+
assertEquals(0, index, "Upper bound for 5 in a single element array should be 0.");
92+
93+
index = upperBound.find(singleElementArray, 10);
94+
assertEquals(0, index, "Upper bound for 10 in a single element array should be 0.");
95+
96+
index = upperBound.find(singleElementArray, 15);
97+
assertEquals(0, index, "Upper bound for 15 in a single element array should be 0.");
98+
}
99+
}

0 commit comments

Comments
 (0)