|
| 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 | +/** |
| 8 | + * Unit tests for the IterativeTernarySearch class. |
| 9 | + */ |
| 10 | +class IterativeTernarySearchTest { |
| 11 | + |
| 12 | + /** |
| 13 | + * Test for basic ternary search functionality when the element is found. |
| 14 | + */ |
| 15 | + @Test |
| 16 | + void testTernarySearchFound() { |
| 17 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 18 | + Integer[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; |
| 19 | + Integer key = 128; |
| 20 | + int expectedIndex = 7; // Index of the key in the array |
| 21 | + assertEquals(expectedIndex, ternarySearch.find(array, key), "The index of the found element should be 7."); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Test for ternary search when the element is not present in the array. |
| 26 | + */ |
| 27 | + @Test |
| 28 | + void testTernarySearchNotFound() { |
| 29 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 30 | + Integer[] array = {1, 2, 4, 8, 16}; |
| 31 | + Integer key = 6; // Element not present in the array |
| 32 | + assertEquals(-1, ternarySearch.find(array, key), "The element should not be found in the array."); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Test for ternary search with the first element as the key. |
| 37 | + */ |
| 38 | + @Test |
| 39 | + void testTernarySearchFirstElement() { |
| 40 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 41 | + Integer[] array = {1, 2, 4, 8, 16}; |
| 42 | + Integer key = 1; // First element |
| 43 | + assertEquals(0, ternarySearch.find(array, key), "The index of the first element should be 0."); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test for ternary search with the last element as the key. |
| 48 | + */ |
| 49 | + @Test |
| 50 | + void testTernarySearchLastElement() { |
| 51 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 52 | + Integer[] array = {1, 2, 4, 8, 16}; |
| 53 | + Integer key = 16; // Last element |
| 54 | + assertEquals(4, ternarySearch.find(array, key), "The index of the last element should be 4."); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test for ternary search with a single element present. |
| 59 | + */ |
| 60 | + @Test |
| 61 | + void testTernarySearchSingleElementFound() { |
| 62 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 63 | + Integer[] array = {1}; |
| 64 | + Integer key = 1; // Only element present |
| 65 | + assertEquals(0, ternarySearch.find(array, key), "The index of the single element should be 0."); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test for ternary search with a single element not present. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + void testTernarySearchSingleElementNotFound() { |
| 73 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 74 | + Integer[] array = {1}; |
| 75 | + Integer key = 2; // Key not present |
| 76 | + assertEquals(-1, ternarySearch.find(array, key), "The element should not be found in the array."); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Test for ternary search with an empty array. |
| 81 | + */ |
| 82 | + @Test |
| 83 | + void testTernarySearchEmptyArray() { |
| 84 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 85 | + Integer[] array = {}; // Empty array |
| 86 | + Integer key = 1; // Key not present |
| 87 | + assertEquals(-1, ternarySearch.find(array, key), "The element should not be found in an empty array."); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test for ternary search on a large array. |
| 92 | + */ |
| 93 | + @Test |
| 94 | + void testTernarySearchLargeArray() { |
| 95 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 96 | + Integer[] array = new Integer[10000]; |
| 97 | + for (int i = 0; i < array.length; i++) { |
| 98 | + array[i] = i * 2; |
| 99 | + } // Array from 0 to 19998, step 2 |
| 100 | + Integer key = 9998; // Present in the array |
| 101 | + assertEquals(4999, ternarySearch.find(array, key), "The index of the found element should be 4999."); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test for ternary search on large array with a non-existent key. |
| 106 | + */ |
| 107 | + @Test |
| 108 | + void testTernarySearchLargeArrayNotFound() { |
| 109 | + IterativeTernarySearch ternarySearch = new IterativeTernarySearch(); |
| 110 | + Integer[] array = new Integer[10000]; |
| 111 | + for (int i = 0; i < array.length; i++) { |
| 112 | + array[i] = i * 2; |
| 113 | + } // Array from 0 to 19998, step 2 |
| 114 | + Integer key = 9999; // Key not present |
| 115 | + assertEquals(-1, ternarySearch.find(array, key), "The element should not be found in the array."); |
| 116 | + } |
| 117 | +} |
0 commit comments