From 4bea8c8b1dee82ef286cbba3d4a15d03835965e9 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:43:44 +0530 Subject: [PATCH 1/9] Add tests, remove `main` in `UpperBound` --- .../thealgorithms/searches/UpperBound.java | 25 ----- .../searches/UpperBoundTest.java | 100 ++++++++++++++++++ 2 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 src/test/java/com/thealgorithms/searches/UpperBoundTest.java diff --git a/src/main/java/com/thealgorithms/searches/UpperBound.java b/src/main/java/com/thealgorithms/searches/UpperBound.java index bbce617a143b..ec52c7a0ae5c 100644 --- a/src/main/java/com/thealgorithms/searches/UpperBound.java +++ b/src/main/java/com/thealgorithms/searches/UpperBound.java @@ -1,9 +1,6 @@ package com.thealgorithms.searches; 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 @@ -25,28 +22,6 @@ */ 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 key is an element for which the UpperBound is to be found diff --git a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java new file mode 100644 index 000000000000..db57de345d50 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java @@ -0,0 +1,100 @@ +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() { + // Test with an array of a single element + 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."); + } +} From 6d995561ef2f38d48557d834fbda339f8a6ed03d Mon Sep 17 00:00:00 2001 From: Hardvan Date: Wed, 9 Oct 2024 07:14:02 +0000 Subject: [PATCH 2/9] Update directory --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6042dd1b5e0d..dbf146380406 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -31,6 +31,7 @@ * [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java) * [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java) * [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java) + * [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java) * [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java) * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java) * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) @@ -638,6 +639,7 @@ * [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java) * [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java) * [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java) + * [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java) * [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java) * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java) * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) @@ -978,6 +980,7 @@ * [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java) * [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java) * [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java) + * [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java) * sorts * [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) From 17fdced7a4d9b8ad4f8c0ed6c7777b28b8fb69f6 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:49:20 +0530 Subject: [PATCH 3/9] Fix --- src/test/java/com/thealgorithms/searches/UpperBoundTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java index db57de345d50..918f45ef6fe2 100644 --- a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java +++ b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java @@ -85,7 +85,6 @@ void testUpperBoundEdgeCase() { @Test void testUpperBoundSingleElementArray() { - // Test with an array of a single element Integer[] singleElementArray = {10}; int index = upperBound.find(singleElementArray, 5); From 3783ac5594af3e150106556b602638389a416326 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:50:53 +0530 Subject: [PATCH 4/9] Fix --- .../com/thealgorithms/searches/UpperBoundTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java index 918f45ef6fe2..8706a2530a3f 100644 --- a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java +++ b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java @@ -21,10 +21,10 @@ void setUp() { int size = 100; int maxElement = 100; sortedArray = random.ints(size, 1, maxElement) - .distinct() // Ensure all elements are unique - .sorted() - .boxed() - .toArray(Integer[]::new); + .distinct() // Ensure all elements are unique + .sorted() + .boxed() + .toArray(Integer[]::new); } @Test @@ -48,7 +48,7 @@ void testUpperBoundExactMatch() { @Test void testUpperBoundMultipleValues() { - Integer[] arrayWithDuplicates = new Integer[]{1, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9}; // Test array with duplicates + 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); From 9c99229e3ff0642c6a34603687c7a373e2923ffb Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:52:10 +0530 Subject: [PATCH 5/9] Fix --- .../java/com/thealgorithms/searches/UpperBoundTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java index 8706a2530a3f..793d4bdc5767 100644 --- a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java +++ b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java @@ -21,10 +21,10 @@ void setUp() { int size = 100; int maxElement = 100; sortedArray = random.ints(size, 1, maxElement) - .distinct() // Ensure all elements are unique - .sorted() - .boxed() - .toArray(Integer[]::new); + .distinct() // Ensure all elements are unique + .sorted() + .boxed() + .toArray(Integer[]::new); } @Test From 3ae5aba8f3c453a2993c90afb28d5277fded47c7 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:53:43 +0530 Subject: [PATCH 6/9] Fix --- .../java/com/thealgorithms/searches/UpperBoundTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java index 793d4bdc5767..2d41e8fe5ad1 100644 --- a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java +++ b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java @@ -21,10 +21,10 @@ void setUp() { int size = 100; int maxElement = 100; sortedArray = random.ints(size, 1, maxElement) - .distinct() // Ensure all elements are unique - .sorted() - .boxed() - .toArray(Integer[]::new); + .distinct() // Ensure all elements are unique + .sorted() + .boxed() + .toArray(Integer[]::new); } @Test From f32b896ad6c2f3a628d7e4a85f5726adb021083c Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:54:58 +0530 Subject: [PATCH 7/9] Fix --- .../java/com/thealgorithms/searches/UpperBoundTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java index 2d41e8fe5ad1..c5f16f573751 100644 --- a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java +++ b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java @@ -21,10 +21,10 @@ void setUp() { int size = 100; int maxElement = 100; sortedArray = random.ints(size, 1, maxElement) - .distinct() // Ensure all elements are unique - .sorted() - .boxed() - .toArray(Integer[]::new); + .distinct() // Ensure all elements are unique + .sorted() + .boxed() + .toArray(Integer[]::new); } @Test From 8471b94670d7a6de67e1172044fd171baae4349a Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:56:14 +0530 Subject: [PATCH 8/9] Fix --- src/test/java/com/thealgorithms/searches/UpperBoundTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java index c5f16f573751..dc0cbdd97e19 100644 --- a/src/test/java/com/thealgorithms/searches/UpperBoundTest.java +++ b/src/test/java/com/thealgorithms/searches/UpperBoundTest.java @@ -24,7 +24,7 @@ void setUp() { .distinct() // Ensure all elements are unique .sorted() .boxed() - .toArray(Integer[]::new); + .toArray(Integer[] ::new); } @Test From d4e3909a530ba9ec9eac13658d36f32fa5ced202 Mon Sep 17 00:00:00 2001 From: siriak Date: Thu, 10 Oct 2024 20:30:23 +0000 Subject: [PATCH 9/9] Update directory --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7935cbec48a8..ede624fe09f6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1023,8 +1023,8 @@ * [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java) * [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java) * [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java) - * [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.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 * [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)