|
| 1 | +package com.thealgorithms.searches; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.stream.IntStream; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +/** |
| 10 | + * Unit tests for the FibonacciSearch class. |
| 11 | + */ |
| 12 | +class FibonacciSearchTest { |
| 13 | + |
| 14 | + /** |
| 15 | + * Test for basic Fibonacci search functionality. |
| 16 | + */ |
| 17 | + @Test |
| 18 | + void testFibonacciSearchFound() { |
| 19 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 20 | + Integer[] 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, fibonacciSearch.find(array, key), "The index of the found element should be 7."); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Test for Fibonacci search when the element is not present. |
| 28 | + */ |
| 29 | + @Test |
| 30 | + void testFibonacciSearchNotFound() { |
| 31 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 32 | + Integer[] array = {1, 2, 4, 8, 16}; |
| 33 | + int key = 6; // Element not present in the array |
| 34 | + int expectedIndex = -1; // Key not found |
| 35 | + assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The element should not be found in the array."); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Test for Fibonacci search with the first element as the key. |
| 40 | + */ |
| 41 | + @Test |
| 42 | + void testFibonacciSearchFirstElement() { |
| 43 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 44 | + Integer[] array = {1, 2, 4, 8, 16}; |
| 45 | + int key = 1; // First element |
| 46 | + int expectedIndex = 0; // Index of the key in the array |
| 47 | + assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the first element should be 0."); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test for Fibonacci search with the last element as the key. |
| 52 | + */ |
| 53 | + @Test |
| 54 | + void testFibonacciSearchLastElement() { |
| 55 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 56 | + Integer[] array = {1, 2, 4, 8, 16}; |
| 57 | + int key = 16; // Last element |
| 58 | + int expectedIndex = 4; // Index of the key in the array |
| 59 | + assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 4."); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test for Fibonacci search with a single element present. |
| 64 | + */ |
| 65 | + @Test |
| 66 | + void testFibonacciSearchSingleElementFound() { |
| 67 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 68 | + Integer[] array = {1}; |
| 69 | + int key = 1; // Only element present |
| 70 | + int expectedIndex = 0; // Index of the key in the array |
| 71 | + assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the single element should be 0."); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test for Fibonacci search with a single element not present. |
| 76 | + */ |
| 77 | + @Test |
| 78 | + void testFibonacciSearchSingleElementNotFound() { |
| 79 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 80 | + Integer[] array = {1}; |
| 81 | + int key = 2; // Key not present |
| 82 | + int expectedIndex = -1; // Key not found |
| 83 | + assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The element should not be found in the array."); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Test for Fibonacci search with an empty array. |
| 88 | + */ |
| 89 | + @Test |
| 90 | + void testFibonacciSearchEmptyArray() { |
| 91 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 92 | + Integer[] array = {}; // Empty array |
| 93 | + int key = 1; // Key not present |
| 94 | + assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "An empty array should throw an IllegalArgumentException."); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testFibonacciSearchUnsortedArray() { |
| 99 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 100 | + Integer[] array = {2, 1, 4, 3, 6, 5}; |
| 101 | + int key = 3; // Key not present |
| 102 | + assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "An unsorted array should throw an IllegalArgumentException."); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testFibonacciSearchNullKey() { |
| 107 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 108 | + Integer[] array = {1, 2, 4, 8, 16}; |
| 109 | + Integer key = null; // Null key |
| 110 | + assertThrows(IllegalArgumentException.class, () -> fibonacciSearch.find(array, key), "A null key should throw an IllegalArgumentException."); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Test for Fibonacci search on large array. |
| 115 | + */ |
| 116 | + @Test |
| 117 | + void testFibonacciSearchLargeArray() { |
| 118 | + FibonacciSearch fibonacciSearch = new FibonacciSearch(); |
| 119 | + Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999 |
| 120 | + int key = 9999; |
| 121 | + int expectedIndex = 9999; |
| 122 | + assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 9999."); |
| 123 | + } |
| 124 | +} |
0 commit comments