|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 |
|
5 | 5 | import java.util.stream.Stream; |
| 6 | +import org.junit.jupiter.api.Test; |
6 | 7 | import org.junit.jupiter.params.ParameterizedTest; |
7 | 8 | import org.junit.jupiter.params.provider.Arguments; |
8 | 9 | import org.junit.jupiter.params.provider.MethodSource; |
9 | 10 |
|
10 | | -public class MaximumSumOfDistinctSubarraysWithLengthKTest { |
| 11 | +/** |
| 12 | + * Test class for {@link MaximumSumOfDistinctSubarraysWithLengthK}. |
| 13 | + * |
| 14 | + * This class contains comprehensive test cases to verify the correctness of the |
| 15 | + * maximum subarray sum algorithm with distinct elements constraint. |
| 16 | + */ |
| 17 | +class MaximumSumOfDistinctSubarraysWithLengthKTest { |
11 | 18 |
|
| 19 | + /** |
| 20 | + * Parameterized test for various input scenarios. |
| 21 | + * |
| 22 | + * @param expected the expected maximum sum |
| 23 | + * @param k the subarray size |
| 24 | + * @param arr the input array |
| 25 | + */ |
12 | 26 | @ParameterizedTest |
13 | 27 | @MethodSource("inputStream") |
14 | | - void testMaximumSubarraySum(int expected, int k, int[] arr) { |
| 28 | + void testMaximumSubarraySum(long expected, int k, int[] arr) { |
15 | 29 | assertEquals(expected, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(k, arr)); |
16 | 30 | } |
17 | 31 |
|
| 32 | + /** |
| 33 | + * Provides test cases for the parameterized test. |
| 34 | + * |
| 35 | + * Test cases cover: |
| 36 | + * - Normal cases with distinct and duplicate elements |
| 37 | + * - Edge cases (empty array, k = 0, k > array length) |
| 38 | + * - Single element arrays |
| 39 | + * - Arrays with all duplicates |
| 40 | + * - Negative numbers |
| 41 | + * - Large sums |
| 42 | + * |
| 43 | + * @return stream of test arguments |
| 44 | + */ |
18 | 45 | private static Stream<Arguments> inputStream() { |
19 | | - return Stream.of(Arguments.of(15, 3, new int[] {1, 5, 4, 2, 9, 9, 9}), Arguments.of(0, 3, new int[] {4, 4, 4}), Arguments.of(12, 3, new int[] {9, 9, 9, 1, 2, 3}), Arguments.of(0, 0, new int[] {9, 9, 9}), Arguments.of(0, 5, new int[] {9, 9, 9}), Arguments.of(9, 1, new int[] {9, 2, 3, 7}), |
20 | | - Arguments.of(15, 5, new int[] {1, 2, 3, 4, 5}), Arguments.of(6, 3, new int[] {-1, 2, 3, 1, -2, 4}), Arguments.of(10, 1, new int[] {10}), Arguments.of(0, 2, new int[] {7, 7, 7, 7}), Arguments.of(0, 3, new int[] {}), Arguments.of(0, 10, new int[] {1, 2, 3})); |
| 46 | + return Stream.of( |
| 47 | + // Normal case: [5, 4, 2] has distinct elements with sum 11, but [4, 2, 9] also |
| 48 | + // distinct with sum 15 |
| 49 | + Arguments.of(15L, 3, new int[] {1, 5, 4, 2, 9, 9, 9}), |
| 50 | + // All elements are same, no distinct subarray of size 3 |
| 51 | + Arguments.of(0L, 3, new int[] {4, 4, 4}), |
| 52 | + // First three have duplicates, but [1, 2, 3] are distinct with sum 6, wait |
| 53 | + // [9,1,2] has sum 12 |
| 54 | + Arguments.of(12L, 3, new int[] {9, 9, 9, 1, 2, 3}), |
| 55 | + // k = 0, should return 0 |
| 56 | + Arguments.of(0L, 0, new int[] {9, 9, 9}), |
| 57 | + // k > array length, should return 0 |
| 58 | + Arguments.of(0L, 5, new int[] {9, 9, 9}), |
| 59 | + // k = 1, single element (always distinct) |
| 60 | + Arguments.of(9L, 1, new int[] {9, 2, 3, 7}), |
| 61 | + // All distinct elements, size matches array |
| 62 | + Arguments.of(15L, 5, new int[] {1, 2, 3, 4, 5}), |
| 63 | + // Array with negative numbers |
| 64 | + Arguments.of(6L, 3, new int[] {-1, 2, 3, 1, -2, 4}), |
| 65 | + // Single element array |
| 66 | + Arguments.of(10L, 1, new int[] {10}), |
| 67 | + // All duplicates with k = 2 |
| 68 | + Arguments.of(0L, 2, new int[] {7, 7, 7, 7}), |
| 69 | + // Empty array |
| 70 | + Arguments.of(0L, 3, new int[] {}), |
| 71 | + // k much larger than array length |
| 72 | + Arguments.of(0L, 10, new int[] {1, 2, 3})); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test with a larger array and larger k value. |
| 77 | + */ |
| 78 | + @Test |
| 79 | + void testLargerArray() { |
| 80 | + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 81 | + long result = MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(5, arr); |
| 82 | + // Maximum sum with 5 distinct elements: [6,7,8,9,10] = 40 |
| 83 | + assertEquals(40L, result); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Test with negative k value. |
| 88 | + */ |
| 89 | + @Test |
| 90 | + void testNegativeK() { |
| 91 | + int[] arr = new int[] {1, 2, 3, 4, 5}; |
| 92 | + long result = MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(-1, arr); |
| 93 | + assertEquals(0L, result); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test with null array. |
| 98 | + */ |
| 99 | + @Test |
| 100 | + void testNullArray() { |
| 101 | + int[] nullArray = null; |
| 102 | + long result = MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, new int[][] {nullArray}[0]); |
| 103 | + assertEquals(0L, result); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test with array containing duplicates at boundaries. |
| 108 | + */ |
| 109 | + @Test |
| 110 | + void testDuplicatesAtBoundaries() { |
| 111 | + int[] arr = new int[] {1, 1, 2, 3, 4, 4}; |
| 112 | + // [2, 3, 4] is the only valid window with sum 9 |
| 113 | + long result = MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, arr); |
| 114 | + assertEquals(9L, result); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test with large numbers to verify long return type. |
| 119 | + */ |
| 120 | + @Test |
| 121 | + void testLargeNumbers() { |
| 122 | + int[] arr = new int[] {1000000, 2000000, 3000000, 4000000}; |
| 123 | + // All elements are distinct, max sum with k=3 is [2000000, 3000000, 4000000] = |
| 124 | + // 9000000 |
| 125 | + long result = MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, arr); |
| 126 | + assertEquals(9000000L, result); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Test where multiple windows have the same maximum sum. |
| 131 | + */ |
| 132 | + @Test |
| 133 | + void testMultipleMaxWindows() { |
| 134 | + int[] arr = new int[] {1, 2, 3, 4, 3, 2, 1}; |
| 135 | + // Windows [1,2,3], [2,3,4], [4,3,2], [3,2,1] - max is [2,3,4] = 9 |
| 136 | + long result = MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, arr); |
| 137 | + assertEquals(9L, result); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Test with only two elements and k=2. |
| 142 | + */ |
| 143 | + @Test |
| 144 | + void testTwoElementsDistinct() { |
| 145 | + int[] arr = new int[] {5, 10}; |
| 146 | + long result = MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(2, arr); |
| 147 | + assertEquals(15L, result); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Test with only two elements (duplicates) and k=2. |
| 152 | + */ |
| 153 | + @Test |
| 154 | + void testTwoElementsDuplicate() { |
| 155 | + int[] arr = new int[] {5, 5}; |
| 156 | + long result = MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(2, arr); |
| 157 | + assertEquals(0L, result); |
21 | 158 | } |
22 | 159 | } |
0 commit comments