|
| 1 | +package com.thealgorithms.searches; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +class LowerBoundTest { |
| 8 | + |
| 9 | + /** |
| 10 | + * Test finding the lower bound for an element present in the array. |
| 11 | + */ |
| 12 | + @Test |
| 13 | + void testLowerBoundElementPresent() { |
| 14 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 15 | + LowerBound lowerBound = new LowerBound(); |
| 16 | + |
| 17 | + // Test for a value that is present |
| 18 | + assertEquals(2, lowerBound.find(array, 3), "Lower bound for 3 should be at index 2"); |
| 19 | + assertEquals(0, lowerBound.find(array, 1), "Lower bound for 1 should be at index 0"); |
| 20 | + assertEquals(4, lowerBound.find(array, 5), "Lower bound for 5 should be at index 4"); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Test finding the lower bound for a value greater than the maximum element in the array. |
| 25 | + */ |
| 26 | + @Test |
| 27 | + void testLowerBoundElementGreaterThanMax() { |
| 28 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 29 | + LowerBound lowerBound = new LowerBound(); |
| 30 | + |
| 31 | + // Test for a value greater than the maximum |
| 32 | + assertEquals(4, lowerBound.find(array, 6), "Lower bound for 6 should be at index 4"); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Test finding the lower bound for a value less than the minimum element in the array. |
| 37 | + */ |
| 38 | + @Test |
| 39 | + void testLowerBoundElementLessThanMin() { |
| 40 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 41 | + LowerBound lowerBound = new LowerBound(); |
| 42 | + |
| 43 | + // Test for a value less than the minimum |
| 44 | + assertEquals(0, lowerBound.find(array, 0), "Lower bound for 0 should be at index 0"); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test finding the lower bound for a non-existent value that falls between two elements. |
| 49 | + */ |
| 50 | + @Test |
| 51 | + void testLowerBoundNonExistentValue() { |
| 52 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 53 | + LowerBound lowerBound = new LowerBound(); |
| 54 | + |
| 55 | + // Test for a value that is not present |
| 56 | + assertEquals(4, lowerBound.find(array, 7), "Lower bound for 7 should be at index 4"); |
| 57 | + assertEquals(0, lowerBound.find(array, 0), "Lower bound for 0 should be at index 0"); |
| 58 | + } |
| 59 | +} |
0 commit comments