|
| 1 | +package array.largest_elements_in_array; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.DisplayName; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.*; |
| 7 | + |
| 8 | +class LargestElementTest { |
| 9 | + |
| 10 | + private final LargestElement solution = new LargestElement(); |
| 11 | + |
| 12 | + // Test cases for maxElement() |
| 13 | + |
| 14 | + @Test |
| 15 | + @DisplayName("maxElement should find the largest element in a positive integer array") |
| 16 | + void testMaxElementPositiveNumbers() { |
| 17 | + int[] nums = {1, 8, 3, 10, 4}; |
| 18 | + assertEquals(10, solution.maxElement(nums)); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + @DisplayName("maxElement should find the largest element in a negative integer array") |
| 23 | + void testMaxElementNegativeNumbers() { |
| 24 | + int[] nums = {-5, -2, -10, -1}; |
| 25 | + assertEquals(-1, solution.maxElement(nums)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + @DisplayName("maxElement should find the largest element in an array with mixed positive and negative numbers") |
| 30 | + void testMaxElementMixedNumbers() { |
| 31 | + int[] nums = {-10, 5, 0, -2, 8}; |
| 32 | + assertEquals(8, solution.maxElement(nums)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("maxElement should return the element itself for a single-element array") |
| 37 | + void testMaxElementSingleElement() { |
| 38 | + int[] nums = {42}; |
| 39 | + assertEquals(42, solution.maxElement(nums)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + @DisplayName("maxElement should handle an array containing Integer.MIN_VALUE") |
| 44 | + void testMaxElementWithMinValue() { |
| 45 | + int[] nums = {-1, -5, Integer.MIN_VALUE}; |
| 46 | + assertEquals(-1, solution.maxElement(nums)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @DisplayName("maxElement should throw IllegalArgumentException for a null array") |
| 51 | + void testMaxElementNullArray() { |
| 52 | + assertThrows(IllegalArgumentException.class, () -> solution.maxElement(null)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @DisplayName("maxElement should throw IllegalArgumentException for an empty array") |
| 57 | + void testMaxElementEmptyArray() { |
| 58 | + int[] nums = {}; |
| 59 | + assertThrows(IllegalArgumentException.class, () -> solution.maxElement(nums)); |
| 60 | + } |
| 61 | + |
| 62 | + // Test cases for secondLargestElement() |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("secondLargestElement should find the second-largest element in a standard case") |
| 66 | + void testSecondLargestElementStandardCase() { |
| 67 | + int[] nums = {1, 8, 3, 10, 4}; |
| 68 | + assertEquals(8, solution.secondLargestElement(nums)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @DisplayName("secondLargestElement should return -1 if all elements are the same") |
| 73 | + void testSecondLargestElementAllElementsSame() { |
| 74 | + int[] nums = {5, 5, 5, 5}; |
| 75 | + assertEquals(-1, solution.secondLargestElement(nums)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("secondLargestElement should handle an array with two distinct elements") |
| 80 | + void testSecondLargestElementTwoElements() { |
| 81 | + int[] nums = {10, 5}; |
| 82 | + assertEquals(5, solution.secondLargestElement(nums)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @DisplayName("secondLargestElement should handle negative numbers correctly") |
| 87 | + void testSecondLargestElementNegativeNumbers() { |
| 88 | + int[] nums = {-5, -2, -10, -1}; |
| 89 | + assertEquals(-2, solution.secondLargestElement(nums)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @DisplayName("secondLargestElement should return -1 for an array with fewer than two elements") |
| 94 | + void testSecondLargestElementLessThanTwoElements() { |
| 95 | + int[] nums = {10}; |
| 96 | + assertEquals(-1, solution.secondLargestElement(nums)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @DisplayName("secondLargestElement should return -1 for a null array") |
| 101 | + void testSecondLargestElementNullArray() { |
| 102 | + assertEquals(-1, solution.secondLargestElement(null)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + @DisplayName("secondLargestElement should return -1 for an empty array") |
| 107 | + void testSecondLargestElementEmptyArray() { |
| 108 | + int[] nums = {}; |
| 109 | + assertEquals(-1, solution.secondLargestElement(nums)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @DisplayName("Failing Case: secondLargestElement should handle an array where the second-largest element is Integer.MIN_VALUE") |
| 114 | + void testSecondLargestElementFailingCaseWithMinValue() { |
| 115 | + // This test exposes a flaw in the logic. The current implementation returns -1 |
| 116 | + // because it uses Integer.MIN_VALUE as a sentinel for "not found", but it can |
| 117 | + // also be a valid second-largest number. |
| 118 | + int[] nums = {10, Integer.MIN_VALUE}; |
| 119 | + assertEquals(Integer.MIN_VALUE, solution.secondLargestElement(nums), |
| 120 | + "The method fails to correctly identify Integer.MIN_VALUE as the second-largest element."); |
| 121 | + } |
| 122 | +} |
0 commit comments