|
| 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 | +import java.util.stream.IntStream; |
| 8 | + |
| 9 | +/** |
| 10 | + * Unit tests for the InterpolationSearch class. |
| 11 | + */ |
| 12 | +class InterpolationSearchTest { |
| 13 | + |
| 14 | + /** |
| 15 | + * Test for basic interpolation search functionality when the element is found. |
| 16 | + */ |
| 17 | + @Test |
| 18 | + void testInterpolationSearchFound() { |
| 19 | + InterpolationSearch interpolationSearch = new InterpolationSearch(); |
| 20 | + int[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; |
| 21 | + int key = 128; |
| 22 | + int expectedIndex = 7; // Index of the key in the array |
| 23 | + assertEquals(expectedIndex, interpolationSearch.find(array, key), |
| 24 | + "The index of the found element should be 7."); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Test for interpolation search when the element is not present in the array. |
| 29 | + */ |
| 30 | + @Test |
| 31 | + void testInterpolationSearchNotFound() { |
| 32 | + InterpolationSearch interpolationSearch = new InterpolationSearch(); |
| 33 | + int[] array = {1, 2, 4, 8, 16}; |
| 34 | + int key = 6; // Element not present in the array |
| 35 | + int expectedIndex = -1; // Key not found |
| 36 | + assertEquals(expectedIndex, interpolationSearch.find(array, key), |
| 37 | + "The element should not be found in the array."); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Test for interpolation search with the first element as the key. |
| 42 | + */ |
| 43 | + @Test |
| 44 | + void testInterpolationSearchFirstElement() { |
| 45 | + InterpolationSearch interpolationSearch = new InterpolationSearch(); |
| 46 | + int[] array = {1, 2, 4, 8, 16}; |
| 47 | + int key = 1; // First element |
| 48 | + int expectedIndex = 0; // Index of the key in the array |
| 49 | + assertEquals(expectedIndex, interpolationSearch.find(array, key), |
| 50 | + "The index of the first element should be 0."); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Test for interpolation search with a single element not present. |
| 55 | + */ |
| 56 | + @Test |
| 57 | + void testInterpolationSearchSingleElementNotFound() { |
| 58 | + InterpolationSearch interpolationSearch = new InterpolationSearch(); |
| 59 | + int[] array = {1}; |
| 60 | + int key = 2; // Key not present |
| 61 | + int expectedIndex = -1; // Key not found |
| 62 | + assertEquals(expectedIndex, interpolationSearch.find(array, key), |
| 63 | + "The element should not be found in the array."); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test for interpolation search with an empty array. |
| 68 | + */ |
| 69 | + @Test |
| 70 | + void testInterpolationSearchEmptyArray() { |
| 71 | + InterpolationSearch interpolationSearch = new InterpolationSearch(); |
| 72 | + int[] array = {}; // Empty array |
| 73 | + int key = 1; // Key not present |
| 74 | + int expectedIndex = -1; // Key not found |
| 75 | + assertEquals(expectedIndex, interpolationSearch.find(array, key), |
| 76 | + "The element should not be found in an empty array."); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Test for interpolation search on large uniformly distributed array. |
| 81 | + */ |
| 82 | + @Test |
| 83 | + void testInterpolationSearchLargeUniformArray() { |
| 84 | + InterpolationSearch interpolationSearch = new InterpolationSearch(); |
| 85 | + int[] array = IntStream.range(0, 10000).map(i -> i * 2).toArray(); // Array from 0 to 19998, step 2 |
| 86 | + int key = 9998; // Last even number in the array |
| 87 | + int expectedIndex = 4999; // Index of the last element |
| 88 | + assertEquals(expectedIndex, interpolationSearch.find(array, key), |
| 89 | + "The index of the last element should be 4999."); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test for interpolation search on large non-uniformly distributed array. |
| 94 | + */ |
| 95 | + @Test |
| 96 | + void testInterpolationSearchLargeNonUniformArray() { |
| 97 | + InterpolationSearch interpolationSearch = new InterpolationSearch(); |
| 98 | + int[] array = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144}; // Fibonacci numbers |
| 99 | + int key = 21; // Present in the array |
| 100 | + int expectedIndex = 6; // Index of the key in the array |
| 101 | + assertEquals(expectedIndex, interpolationSearch.find(array, key), |
| 102 | + "The index of the found element should be 6."); |
| 103 | + } |
| 104 | +} |
0 commit comments