mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 04:26:27 +08:00
Add tests, remove main
in UpperBound
(#5679)
This commit is contained in:
@ -1025,6 +1025,7 @@
|
|||||||
* [TernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TernarySearchTest.java)
|
* [TernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TernarySearchTest.java)
|
||||||
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
|
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
|
||||||
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
|
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
|
||||||
|
* [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
|
||||||
* sorts
|
* sorts
|
||||||
* [BeadSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java)
|
* [BeadSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java)
|
||||||
* [BinaryInsertionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java)
|
* [BinaryInsertionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java)
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
|
||||||
import java.util.stream.IntStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The UpperBound method is used to return an index pointing to the first
|
* The UpperBound method is used to return an index pointing to the first
|
||||||
@ -25,28 +22,6 @@ import java.util.stream.IntStream;
|
|||||||
*/
|
*/
|
||||||
class UpperBound implements SearchAlgorithm {
|
class UpperBound implements SearchAlgorithm {
|
||||||
|
|
||||||
// Driver Program
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// Just generate data
|
|
||||||
Random r = ThreadLocalRandom.current();
|
|
||||||
|
|
||||||
int size = 100;
|
|
||||||
int maxElement = 100000;
|
|
||||||
|
|
||||||
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
|
|
||||||
|
|
||||||
// The element for which the upper bound is to be found
|
|
||||||
int val = integers[r.nextInt(size - 1)] + 1;
|
|
||||||
|
|
||||||
UpperBound search = new UpperBound();
|
|
||||||
int atIndex = search.find(integers, val);
|
|
||||||
|
|
||||||
System.out.printf("Val: %d. Upper Bound Found %d at index %d. An array length %d%n", val, integers[atIndex], atIndex, size);
|
|
||||||
|
|
||||||
boolean toCheck = integers[atIndex] > val || integers[size - 1] < val;
|
|
||||||
System.out.printf("Upper Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array is an array where the UpperBound value is to be found
|
* @param array is an array where the UpperBound value is to be found
|
||||||
* @param key is an element for which the UpperBound is to be found
|
* @param key is an element for which the UpperBound is to be found
|
||||||
|
99
src/test/java/com/thealgorithms/searches/UpperBoundTest.java
Normal file
99
src/test/java/com/thealgorithms/searches/UpperBoundTest.java
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class UpperBoundTest {
|
||||||
|
|
||||||
|
private UpperBound upperBound;
|
||||||
|
private Integer[] sortedArray;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
upperBound = new UpperBound();
|
||||||
|
|
||||||
|
// Generate a sorted array of random integers for testing
|
||||||
|
Random random = new Random();
|
||||||
|
int size = 100;
|
||||||
|
int maxElement = 100;
|
||||||
|
sortedArray = random.ints(size, 1, maxElement)
|
||||||
|
.distinct() // Ensure all elements are unique
|
||||||
|
.sorted()
|
||||||
|
.boxed()
|
||||||
|
.toArray(Integer[] ::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpperBoundFound() {
|
||||||
|
int key = sortedArray[sortedArray.length - 1] + 1; // Test with a key larger than max element
|
||||||
|
int index = upperBound.find(sortedArray, key);
|
||||||
|
|
||||||
|
// The upper bound should be equal to the length of the array
|
||||||
|
assertEquals(sortedArray.length - 1, index, "Upper bound for a larger key should be the size of the array.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpperBoundExactMatch() {
|
||||||
|
int key = sortedArray[sortedArray.length / 2]; // Choose a key from the middle of the array
|
||||||
|
int index = upperBound.find(sortedArray, key);
|
||||||
|
|
||||||
|
// The index should point to the first element greater than the key
|
||||||
|
assertTrue(index < sortedArray.length, "Upper bound should not exceed array length.");
|
||||||
|
assertTrue(sortedArray[index] > key, "The element at the index should be greater than the key.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpperBoundMultipleValues() {
|
||||||
|
Integer[] arrayWithDuplicates = new Integer[] {1, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9}; // Test array with duplicates
|
||||||
|
int key = 4;
|
||||||
|
int index = upperBound.find(arrayWithDuplicates, key);
|
||||||
|
|
||||||
|
assertTrue(index < arrayWithDuplicates.length, "Upper bound index should be valid.");
|
||||||
|
assertEquals(6, index, "The upper bound for 4 should be the index of the first 5.");
|
||||||
|
assertTrue(arrayWithDuplicates[index] > key, "Element at the upper bound index should be greater than the key.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpperBoundLowerThanMin() {
|
||||||
|
int key = 0; // Test with a key lower than the minimum element
|
||||||
|
int index = upperBound.find(sortedArray, key);
|
||||||
|
|
||||||
|
assertEquals(0, index, "Upper bound for a key lower than minimum should be 0.");
|
||||||
|
assertTrue(sortedArray[index] > key, "The element at index 0 should be greater than the key.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpperBoundHigherThanMax() {
|
||||||
|
int key = sortedArray[sortedArray.length - 1] + 1; // Test with a key higher than maximum element
|
||||||
|
int index = upperBound.find(sortedArray, key);
|
||||||
|
|
||||||
|
assertEquals(sortedArray.length - 1, index, "Upper bound for a key higher than maximum should be the size of the array.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpperBoundEdgeCase() {
|
||||||
|
// Edge case: empty array
|
||||||
|
Integer[] emptyArray = {};
|
||||||
|
int index = upperBound.find(emptyArray, 5);
|
||||||
|
|
||||||
|
assertEquals(0, index, "Upper bound for an empty array should be 0.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpperBoundSingleElementArray() {
|
||||||
|
Integer[] singleElementArray = {10};
|
||||||
|
int index = upperBound.find(singleElementArray, 5);
|
||||||
|
|
||||||
|
assertEquals(0, index, "Upper bound for 5 in a single element array should be 0.");
|
||||||
|
|
||||||
|
index = upperBound.find(singleElementArray, 10);
|
||||||
|
assertEquals(0, index, "Upper bound for 10 in a single element array should be 0.");
|
||||||
|
|
||||||
|
index = upperBound.find(singleElementArray, 15);
|
||||||
|
assertEquals(0, index, "Upper bound for 15 in a single element array should be 0.");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user