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