|
| 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