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