|
| 1 | +package com.thealgorithms.searches; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +class RandomSearchTest { |
| 10 | + |
| 11 | + private RandomSearch randomSearch; |
| 12 | + |
| 13 | + @BeforeEach |
| 14 | + void setUp() { |
| 15 | + randomSearch = new RandomSearch(); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + void testElementFound() { |
| 20 | + Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 21 | + Integer key = 5; |
| 22 | + int index = randomSearch.find(array, key); |
| 23 | + |
| 24 | + assertNotEquals(-1, index, "Element should be found in the array."); |
| 25 | + assertEquals(key, array[index], "Element found should match the key."); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void testElementNotFound() { |
| 30 | + Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 31 | + Integer key = 11; |
| 32 | + int index = randomSearch.find(array, key); |
| 33 | + |
| 34 | + assertEquals(-1, index, "Element not present in the array should return -1."); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testEmptyArray() { |
| 39 | + Integer[] emptyArray = {}; |
| 40 | + Integer key = 5; |
| 41 | + int index = randomSearch.find(emptyArray, key); |
| 42 | + |
| 43 | + assertEquals(-1, index, "Searching in an empty array should return -1."); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testSingleElementArrayFound() { |
| 48 | + Integer[] array = {5}; |
| 49 | + Integer key = 5; |
| 50 | + int index = randomSearch.find(array, key); |
| 51 | + |
| 52 | + assertEquals(0, index, "The key should be found at index 0 in a single-element array."); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testSingleElementArrayNotFound() { |
| 57 | + Integer[] array = {1}; |
| 58 | + Integer key = 5; |
| 59 | + int index = randomSearch.find(array, key); |
| 60 | + |
| 61 | + assertEquals(-1, index, "The key should not be found in a single-element array if it does not match."); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testDuplicateElementsFound() { |
| 66 | + Integer[] array = {1, 2, 3, 4, 5, 5, 5, 7, 8, 9, 10}; |
| 67 | + Integer key = 5; |
| 68 | + int index = randomSearch.find(array, key); |
| 69 | + |
| 70 | + assertNotEquals(-1, index, "The key should be found in the array with duplicates."); |
| 71 | + assertEquals(key, array[index], "The key found should be 5."); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testLargeArray() { |
| 76 | + Integer[] largeArray = new Integer[1000]; |
| 77 | + for (int i = 0; i < largeArray.length; i++) { |
| 78 | + largeArray[i] = i + 1; // Fill with values 1 to 1000 |
| 79 | + } |
| 80 | + |
| 81 | + Integer key = 500; |
| 82 | + int index = randomSearch.find(largeArray, key); |
| 83 | + |
| 84 | + assertNotEquals(-1, index, "The key should be found in the large array."); |
| 85 | + assertEquals(key, largeArray[index], "The key found should match 500."); |
| 86 | + } |
| 87 | +} |
0 commit comments