|
| 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 JumpSearch class. |
| 9 | + */ |
| 10 | +class JumpSearchTest { |
| 11 | + |
| 12 | + /** |
| 13 | + * Test for finding an element present in the array. |
| 14 | + */ |
| 15 | + @Test |
| 16 | + void testJumpSearchFound() { |
| 17 | + JumpSearch jumpSearch = new JumpSearch(); |
| 18 | + Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 19 | + Integer key = 5; // Element to find |
| 20 | + assertEquals(5, jumpSearch.find(array, key), "The index of the found element should be 5."); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Test for finding the first element in the array. |
| 25 | + */ |
| 26 | + @Test |
| 27 | + void testJumpSearchFirstElement() { |
| 28 | + JumpSearch jumpSearch = new JumpSearch(); |
| 29 | + Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 30 | + Integer key = 0; // First element |
| 31 | + assertEquals(0, jumpSearch.find(array, key), "The index of the first element should be 0."); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Test for finding the last element in the array. |
| 36 | + */ |
| 37 | + @Test |
| 38 | + void testJumpSearchLastElement() { |
| 39 | + JumpSearch jumpSearch = new JumpSearch(); |
| 40 | + Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 41 | + Integer key = 10; // Last element |
| 42 | + assertEquals(10, jumpSearch.find(array, key), "The index of the last element should be 10."); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Test for finding an element not present in the array. |
| 47 | + */ |
| 48 | + @Test |
| 49 | + void testJumpSearchNotFound() { |
| 50 | + JumpSearch jumpSearch = new JumpSearch(); |
| 51 | + Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 52 | + Integer key = -1; // Element not in the array |
| 53 | + assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array."); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test for finding an element in an empty array. |
| 58 | + */ |
| 59 | + @Test |
| 60 | + void testJumpSearchEmptyArray() { |
| 61 | + JumpSearch jumpSearch = new JumpSearch(); |
| 62 | + Integer[] array = {}; // Empty array |
| 63 | + Integer key = 1; // Key not present |
| 64 | + assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in an empty array."); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test for finding an element in a large array. |
| 69 | + */ |
| 70 | + @Test |
| 71 | + void testJumpSearchLargeArray() { |
| 72 | + JumpSearch jumpSearch = new JumpSearch(); |
| 73 | + Integer[] array = new Integer[1000]; |
| 74 | + for (int i = 0; i < array.length; i++) { |
| 75 | + array[i] = i * 2; // Fill the array with even numbers |
| 76 | + } |
| 77 | + Integer key = 256; // Present in the array |
| 78 | + assertEquals(128, jumpSearch.find(array, key), "The index of the found element should be 128."); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test for finding an element in a large array when it is not present. |
| 83 | + */ |
| 84 | + @Test |
| 85 | + void testJumpSearchLargeArrayNotFound() { |
| 86 | + JumpSearch jumpSearch = new JumpSearch(); |
| 87 | + Integer[] array = new Integer[1000]; |
| 88 | + for (int i = 0; i < array.length; i++) { |
| 89 | + array[i] = i * 2; // Fill the array with even numbers |
| 90 | + } |
| 91 | + Integer key = 999; // Key not present |
| 92 | + assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array."); |
| 93 | + } |
| 94 | +} |
0 commit comments