From 68862a7c0d39b0c40bb82ef07ba47e1f54f95ff2 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 11:39:49 +0530 Subject: [PATCH 1/4] Add tests, remove `main` in `BinarySearch` --- .../thealgorithms/searches/BinarySearch.java | 26 ---- .../searches/BinarySearchTest.java | 116 ++++++++++++++++++ 2 files changed, 116 insertions(+), 26 deletions(-) create mode 100644 src/test/java/com/thealgorithms/searches/BinarySearchTest.java diff --git a/src/main/java/com/thealgorithms/searches/BinarySearch.java b/src/main/java/com/thealgorithms/searches/BinarySearch.java index 22096307d144..bedad1667f33 100644 --- a/src/main/java/com/thealgorithms/searches/BinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/BinarySearch.java @@ -1,10 +1,6 @@ package com.thealgorithms.searches; import com.thealgorithms.devutils.searches.SearchAlgorithm; -import java.util.Arrays; -import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; -import java.util.stream.IntStream; /** * Binary search is one of the most popular algorithms The algorithm finds the @@ -57,26 +53,4 @@ private > int search(T[] array, T key, int left, int rig return search(array, key, median + 1, right); } } - - // 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 that should be found - int shouldBeFound = integers[r.nextInt(size - 1)]; - - BinarySearch search = new BinarySearch(); - int atIndex = search.find(integers, shouldBeFound); - - System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size); - - int toCheck = Arrays.binarySearch(integers, shouldBeFound); - System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex); - } } diff --git a/src/test/java/com/thealgorithms/searches/BinarySearchTest.java b/src/test/java/com/thealgorithms/searches/BinarySearchTest.java new file mode 100644 index 000000000000..f3ef18d45aa5 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/BinarySearchTest.java @@ -0,0 +1,116 @@ +package com.thealgorithms.searches; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.IntStream; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the BinarySearch class. + */ +class BinarySearchTest { + + /** + * Test for basic binary search functionality. + */ + @Test + void testBinarySearchFound() { + BinarySearch binarySearch = new BinarySearch(); + Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int key = 7; + int expectedIndex = 6; // Index of the key in the array + assertEquals(expectedIndex, binarySearch.find(array, key), + "The index of the found element should be 6."); + } + + /** + * Test for binary search when the element is not present. + */ + @Test + void testBinarySearchNotFound() { + BinarySearch binarySearch = new BinarySearch(); + Integer[] array = {1, 2, 3, 4, 5}; + int key = 6; // Element not present in the array + int expectedIndex = -1; // Key not found + assertEquals(expectedIndex, binarySearch.find(array, key), + "The element should not be found in the array."); + } + + /** + * Test for binary search with first element as the key. + */ + @Test + void testBinarySearchFirstElement() { + BinarySearch binarySearch = new BinarySearch(); + Integer[] array = {1, 2, 3, 4, 5}; + int key = 1; // First element + int expectedIndex = 0; // Index of the key in the array + assertEquals(expectedIndex, binarySearch.find(array, key), + "The index of the first element should be 0."); + } + + /** + * Test for binary search with last element as the key. + */ + @Test + void testBinarySearchLastElement() { + BinarySearch binarySearch = new BinarySearch(); + Integer[] array = {1, 2, 3, 4, 5}; + int key = 5; // Last element + int expectedIndex = 4; // Index of the key in the array + assertEquals(expectedIndex, binarySearch.find(array, key), + "The index of the last element should be 4."); + } + + /** + * Test for binary search with a single element present. + */ + @Test + void testBinarySearchSingleElementFound() { + BinarySearch binarySearch = new BinarySearch(); + Integer[] array = {1}; + int key = 1; // Only element present + int expectedIndex = 0; // Index of the key in the array + assertEquals(expectedIndex, binarySearch.find(array, key), + "The index of the single element should be 0."); + } + + /** + * Test for binary search with a single element not present. + */ + @Test + void testBinarySearchSingleElementNotFound() { + BinarySearch binarySearch = new BinarySearch(); + Integer[] array = {1}; + int key = 2; // Key not present + int expectedIndex = -1; // Key not found + assertEquals(expectedIndex, binarySearch.find(array, key), + "The element should not be found in the array."); + } + + /** + * Test for binary search with an empty array. + */ + @Test + void testBinarySearchEmptyArray() { + BinarySearch binarySearch = new BinarySearch(); + Integer[] array = {}; // Empty array + int key = 1; // Key not present + int expectedIndex = -1; // Key not found + assertEquals(expectedIndex, binarySearch.find(array, key), + "The element should not be found in an empty array."); + } + + /** + * Test for binary search on large array. + */ + @Test + void testBinarySearchLargeArray() { + BinarySearch binarySearch = new BinarySearch(); + Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[]::new); // Array from 0 to 9999 + int key = 9999; // Last element + int expectedIndex = 9999; // Index of the last element + assertEquals(expectedIndex, binarySearch.find(array, key), + "The index of the last element should be 9999."); + } +} From c8c80be5f7db6ac602662e80b2549cc50d71f96f Mon Sep 17 00:00:00 2001 From: Hardvan Date: Wed, 9 Oct 2024 06:10:08 +0000 Subject: [PATCH 2/4] Update directory --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6042dd1b5e0d..42e3bd1d03b8 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) @@ -965,6 +967,7 @@ * [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java) * searches * [BinarySearch2dArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java) + * [BinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearchTest.java) * [BM25InvertedIndexTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java) * [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java) * [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java) From 3180ab781441bd479e2a6fc5600908939d72f417 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 11:41:19 +0530 Subject: [PATCH 3/4] Fix --- .../searches/BinarySearchTest.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/BinarySearchTest.java b/src/test/java/com/thealgorithms/searches/BinarySearchTest.java index f3ef18d45aa5..6ceb66eac86f 100644 --- a/src/test/java/com/thealgorithms/searches/BinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/BinarySearchTest.java @@ -19,8 +19,7 @@ void testBinarySearchFound() { Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int key = 7; int expectedIndex = 6; // Index of the key in the array - assertEquals(expectedIndex, binarySearch.find(array, key), - "The index of the found element should be 6."); + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 6."); } /** @@ -32,8 +31,7 @@ void testBinarySearchNotFound() { Integer[] array = {1, 2, 3, 4, 5}; int key = 6; // Element not present in the array int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, binarySearch.find(array, key), - "The element should not be found in the array."); + assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array."); } /** @@ -45,8 +43,7 @@ void testBinarySearchFirstElement() { Integer[] array = {1, 2, 3, 4, 5}; int key = 1; // First element int expectedIndex = 0; // Index of the key in the array - assertEquals(expectedIndex, binarySearch.find(array, key), - "The index of the first element should be 0."); + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the first element should be 0."); } /** @@ -58,8 +55,7 @@ void testBinarySearchLastElement() { Integer[] array = {1, 2, 3, 4, 5}; int key = 5; // Last element int expectedIndex = 4; // Index of the key in the array - assertEquals(expectedIndex, binarySearch.find(array, key), - "The index of the last element should be 4."); + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 4."); } /** @@ -71,8 +67,7 @@ void testBinarySearchSingleElementFound() { Integer[] array = {1}; int key = 1; // Only element present int expectedIndex = 0; // Index of the key in the array - assertEquals(expectedIndex, binarySearch.find(array, key), - "The index of the single element should be 0."); + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the single element should be 0."); } /** @@ -84,8 +79,7 @@ void testBinarySearchSingleElementNotFound() { Integer[] array = {1}; int key = 2; // Key not present int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, binarySearch.find(array, key), - "The element should not be found in the array."); + assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array."); } /** @@ -97,8 +91,7 @@ void testBinarySearchEmptyArray() { Integer[] array = {}; // Empty array int key = 1; // Key not present int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, binarySearch.find(array, key), - "The element should not be found in an empty array."); + assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in an empty array."); } /** @@ -110,7 +103,6 @@ void testBinarySearchLargeArray() { Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[]::new); // Array from 0 to 9999 int key = 9999; // Last element int expectedIndex = 9999; // Index of the last element - assertEquals(expectedIndex, binarySearch.find(array, key), - "The index of the last element should be 9999."); + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999."); } } From 6c077c72979841026f97dca8a04660748004002d Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 11:45:26 +0530 Subject: [PATCH 4/4] Fix --- src/test/java/com/thealgorithms/searches/BinarySearchTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/searches/BinarySearchTest.java b/src/test/java/com/thealgorithms/searches/BinarySearchTest.java index 6ceb66eac86f..bd4620a7fa7d 100644 --- a/src/test/java/com/thealgorithms/searches/BinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/BinarySearchTest.java @@ -100,7 +100,7 @@ void testBinarySearchEmptyArray() { @Test void testBinarySearchLargeArray() { BinarySearch binarySearch = new BinarySearch(); - Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[]::new); // Array from 0 to 9999 + Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999 int key = 9999; // Last element int expectedIndex = 9999; // Index of the last element assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999.");