mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Add tests, remove main
in LowerBound
(#5672)
This commit is contained in:
@ -1036,6 +1036,7 @@
|
|||||||
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
|
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
|
||||||
* [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java)
|
* [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java)
|
||||||
* [LinearSearchThreadTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchThreadTest.java)
|
* [LinearSearchThreadTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchThreadTest.java)
|
||||||
|
* [LowerBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LowerBoundTest.java)
|
||||||
* [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java)
|
* [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java)
|
||||||
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
|
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
|
||||||
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
|
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.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 LowerBound method is used to return an index pointing to the first
|
* The LowerBound method is used to return an index pointing to the first
|
||||||
@ -25,28 +22,6 @@ import java.util.stream.IntStream;
|
|||||||
*/
|
*/
|
||||||
class LowerBound implements SearchAlgorithm {
|
class LowerBound 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 lower bound is to be found
|
|
||||||
int val = integers[r.nextInt(size - 1)] + 1;
|
|
||||||
|
|
||||||
LowerBound search = new LowerBound();
|
|
||||||
int atIndex = search.find(integers, val);
|
|
||||||
|
|
||||||
System.out.printf("Val: %d. Lower 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("Lower Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array is an array where the LowerBound value is to be found
|
* @param array is an array where the LowerBound value is to be found
|
||||||
* @param key is an element for which the LowerBound is to be found
|
* @param key is an element for which the LowerBound is to be found
|
||||||
|
59
src/test/java/com/thealgorithms/searches/LowerBoundTest.java
Normal file
59
src/test/java/com/thealgorithms/searches/LowerBoundTest.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class LowerBoundTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test finding the lower bound for an element present in the array.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testLowerBoundElementPresent() {
|
||||||
|
Integer[] array = {1, 2, 3, 4, 5};
|
||||||
|
LowerBound lowerBound = new LowerBound();
|
||||||
|
|
||||||
|
// Test for a value that is present
|
||||||
|
assertEquals(2, lowerBound.find(array, 3), "Lower bound for 3 should be at index 2");
|
||||||
|
assertEquals(0, lowerBound.find(array, 1), "Lower bound for 1 should be at index 0");
|
||||||
|
assertEquals(4, lowerBound.find(array, 5), "Lower bound for 5 should be at index 4");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test finding the lower bound for a value greater than the maximum element in the array.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testLowerBoundElementGreaterThanMax() {
|
||||||
|
Integer[] array = {1, 2, 3, 4, 5};
|
||||||
|
LowerBound lowerBound = new LowerBound();
|
||||||
|
|
||||||
|
// Test for a value greater than the maximum
|
||||||
|
assertEquals(4, lowerBound.find(array, 6), "Lower bound for 6 should be at index 4");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test finding the lower bound for a value less than the minimum element in the array.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testLowerBoundElementLessThanMin() {
|
||||||
|
Integer[] array = {1, 2, 3, 4, 5};
|
||||||
|
LowerBound lowerBound = new LowerBound();
|
||||||
|
|
||||||
|
// Test for a value less than the minimum
|
||||||
|
assertEquals(0, lowerBound.find(array, 0), "Lower bound for 0 should be at index 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test finding the lower bound for a non-existent value that falls between two elements.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testLowerBoundNonExistentValue() {
|
||||||
|
Integer[] array = {1, 2, 3, 4, 5};
|
||||||
|
LowerBound lowerBound = new LowerBound();
|
||||||
|
|
||||||
|
// Test for a value that is not present
|
||||||
|
assertEquals(4, lowerBound.find(array, 7), "Lower bound for 7 should be at index 4");
|
||||||
|
assertEquals(0, lowerBound.find(array, 0), "Lower bound for 0 should be at index 0");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user