|
| 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 ExponentialSearch class. |
| 10 | + */ |
| 11 | +class ExponentialSearchTest { |
| 12 | + |
| 13 | + /** |
| 14 | + * Test for basic exponential search functionality. |
| 15 | + */ |
| 16 | + @Test |
| 17 | + void testExponentialSearchFound() { |
| 18 | + ExponentialSearch exponentialSearch = new ExponentialSearch(); |
| 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, exponentialSearch.find(array, key), |
| 23 | + "The index of the found element should be 6."); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Test for exponential search with the first element as the key. |
| 28 | + */ |
| 29 | + @Test |
| 30 | + void testExponentialSearchFirstElement() { |
| 31 | + ExponentialSearch exponentialSearch = new ExponentialSearch(); |
| 32 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 33 | + int key = 1; // First element |
| 34 | + int expectedIndex = 0; // Index of the key in the array |
| 35 | + assertEquals(expectedIndex, exponentialSearch.find(array, key), |
| 36 | + "The index of the first element should be 0."); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test for exponential search with the last element as the key. |
| 41 | + */ |
| 42 | + @Test |
| 43 | + void testExponentialSearchLastElement() { |
| 44 | + ExponentialSearch exponentialSearch = new ExponentialSearch(); |
| 45 | + Integer[] array = {1, 2, 3, 4, 5}; |
| 46 | + int key = 5; // Last element |
| 47 | + int expectedIndex = 4; // Index of the key in the array |
| 48 | + assertEquals(expectedIndex, exponentialSearch.find(array, key), |
| 49 | + "The index of the last element should be 4."); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test for exponential search with a single element present. |
| 54 | + */ |
| 55 | + @Test |
| 56 | + void testExponentialSearchSingleElementFound() { |
| 57 | + ExponentialSearch exponentialSearch = new ExponentialSearch(); |
| 58 | + Integer[] array = {1}; |
| 59 | + int key = 1; // Only element present |
| 60 | + int expectedIndex = 0; // Index of the key in the array |
| 61 | + assertEquals(expectedIndex, exponentialSearch.find(array, key), |
| 62 | + "The index of the single element should be 0."); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test for exponential search with an empty array. |
| 67 | + */ |
| 68 | + @Test |
| 69 | + void testExponentialSearchEmptyArray() { |
| 70 | + ExponentialSearch exponentialSearch = new ExponentialSearch(); |
| 71 | + Integer[] array = {}; // Empty array |
| 72 | + int key = 1; // Key not present |
| 73 | + int expectedIndex = -1; // Key not found |
| 74 | + assertEquals(expectedIndex, exponentialSearch.find(array, key), |
| 75 | + "The element should not be found in an empty array."); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test for exponential search on large array. |
| 80 | + */ |
| 81 | + @Test |
| 82 | + void testExponentialSearchLargeArray() { |
| 83 | + ExponentialSearch exponentialSearch = new ExponentialSearch(); |
| 84 | + Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[]::new); // Array from 0 to 9999 |
| 85 | + int key = 9999; // Last element |
| 86 | + int expectedIndex = 9999; // Index of the last element |
| 87 | + assertEquals(expectedIndex, exponentialSearch.find(array, key), |
| 88 | + "The index of the last element should be 9999."); |
| 89 | + } |
| 90 | +} |
0 commit comments