|
| 1 | +package com.thealgorithms.slidingwindow; |
| 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 MinSumKSizeSubarray class. |
| 9 | + * |
| 10 | + * @author Rashi Dashore (https://github.com/rashi07dashore) |
| 11 | + */ |
| 12 | +class MinSumKSizeSubarrayTest { |
| 13 | + |
| 14 | + /** |
| 15 | + * Test for the basic case of finding the minimum sum. |
| 16 | + */ |
| 17 | + @Test |
| 18 | + void testMinSumKSizeSubarray() { |
| 19 | + int[] arr = {2, 1, 5, 1, 3, 2}; |
| 20 | + int k = 3; |
| 21 | + int expectedMinSum = 6; // Corrected: Minimum sum of a subarray of size 3 |
| 22 | + assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k)); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Test for a different array and subarray size. |
| 27 | + */ |
| 28 | + @Test |
| 29 | + void testMinSumKSizeSubarrayWithDifferentValues() { |
| 30 | + int[] arr = {1, 2, 3, 4, 5}; |
| 31 | + int k = 2; |
| 32 | + int expectedMinSum = 3; // 1 + 2 |
| 33 | + assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k)); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Test for edge case with insufficient elements. |
| 38 | + */ |
| 39 | + @Test |
| 40 | + void testMinSumKSizeSubarrayWithInsufficientElements() { |
| 41 | + int[] arr = {1, 2}; |
| 42 | + int k = 3; // Not enough elements |
| 43 | + int expectedMinSum = -1; // Edge case |
| 44 | + assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test for large array. |
| 49 | + */ |
| 50 | + @Test |
| 51 | + void testMinSumKSizeSubarrayWithLargeArray() { |
| 52 | + int[] arr = {5, 4, 3, 2, 1, 0, -1, -2, -3, -4}; |
| 53 | + int k = 5; |
| 54 | + int expectedMinSum = -10; // -1 + -2 + -3 + -4 + 0 |
| 55 | + assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test for array with negative numbers. |
| 60 | + */ |
| 61 | + @Test |
| 62 | + void testMinSumKSizeSubarrayWithNegativeNumbers() { |
| 63 | + int[] arr = {-1, -2, -3, -4, -5}; |
| 64 | + int k = 2; |
| 65 | + int expectedMinSum = -9; // -4 + -5 |
| 66 | + assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test for the case where k equals the array length. |
| 71 | + */ |
| 72 | + @Test |
| 73 | + void testMinSumKSizeSubarrayWithKEqualToArrayLength() { |
| 74 | + int[] arr = {1, 2, 3, 4, 5}; |
| 75 | + int k = 5; |
| 76 | + int expectedMinSum = 15; // 1 + 2 + 3 + 4 + 5 |
| 77 | + assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k)); |
| 78 | + } |
| 79 | +} |
0 commit comments