|
| 1 | +package com.thealgorithms.searches; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.stream.IntStream; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +/** |
| 9 | + * Unit tests for the BinarySearch class. |
| 10 | + */ |
| 11 | +class BinarySearchTest { |
| 12 | + |
| 13 | + /** |
| 14 | + * Test for basic binary search functionality. |
| 15 | + */ |
| 16 | + @Test |
| 17 | + void testBinarySearchFound() { |
| 18 | + BinarySearch binarySearch = new BinarySearch(); |
| 19 | + Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 20 | + int key = 7; |
| 21 | + int expectedIndex = 6; // Index of the key in the array |
| 22 | + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 6."); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Test for binary search when the element is not present. |
| 27 | + */ |
| 28 | + @Test |
| 29 | + void testBinarySearchNotFound() { |
| 30 | + BinarySearch binarySearch = new BinarySearch(); |
| 31 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 32 | + int key = 6; // Element not present in the array |
| 33 | + int expectedIndex = -1; // Key not found |
| 34 | + assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array."); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Test for binary search with first element as the key. |
| 39 | + */ |
| 40 | + @Test |
| 41 | + void testBinarySearchFirstElement() { |
| 42 | + BinarySearch binarySearch = new BinarySearch(); |
| 43 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 44 | + int key = 1; // First element |
| 45 | + int expectedIndex = 0; // Index of the key in the array |
| 46 | + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the first element should be 0."); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Test for binary search with last element as the key. |
| 51 | + */ |
| 52 | + @Test |
| 53 | + void testBinarySearchLastElement() { |
| 54 | + BinarySearch binarySearch = new BinarySearch(); |
| 55 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 56 | + int key = 5; // Last element |
| 57 | + int expectedIndex = 4; // Index of the key in the array |
| 58 | + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 4."); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Test for binary search with a single element present. |
| 63 | + */ |
| 64 | + @Test |
| 65 | + void testBinarySearchSingleElementFound() { |
| 66 | + BinarySearch binarySearch = new BinarySearch(); |
| 67 | + Integer[] array = {1}; |
| 68 | + int key = 1; // Only element present |
| 69 | + int expectedIndex = 0; // Index of the key in the array |
| 70 | + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the single element should be 0."); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Test for binary search with a single element not present. |
| 75 | + */ |
| 76 | + @Test |
| 77 | + void testBinarySearchSingleElementNotFound() { |
| 78 | + BinarySearch binarySearch = new BinarySearch(); |
| 79 | + Integer[] array = {1}; |
| 80 | + int key = 2; // Key not present |
| 81 | + int expectedIndex = -1; // Key not found |
| 82 | + assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array."); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Test for binary search with an empty array. |
| 87 | + */ |
| 88 | + @Test |
| 89 | + void testBinarySearchEmptyArray() { |
| 90 | + BinarySearch binarySearch = new BinarySearch(); |
| 91 | + Integer[] array = {}; // Empty array |
| 92 | + int key = 1; // Key not present |
| 93 | + int expectedIndex = -1; // Key not found |
| 94 | + assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in an empty array."); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test for binary search on large array. |
| 99 | + */ |
| 100 | + @Test |
| 101 | + void testBinarySearchLargeArray() { |
| 102 | + BinarySearch binarySearch = new BinarySearch(); |
| 103 | + Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999 |
| 104 | + int key = 9999; // Last element |
| 105 | + int expectedIndex = 9999; // Index of the last element |
| 106 | + assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999."); |
| 107 | + } |
| 108 | +} |
0 commit comments