Skip to content

Commit 7f4ca96

Browse files
JUnit Tests added
1 parent c1f52e9 commit 7f4ca96

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.thealgorithms.slidingwindow;
2+
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import org.junit.jupiter.api.Test;
4+
/**
5+
* Unit tests for the MinSumKSizeSubarray class.
6+
*
7+
* @author Rashi Dashore (https://github.com/rashi07dashore)
8+
*/
9+
class MinSumKSizeSubarrayTest {
10+
/**
11+
* Test for the basic case of finding the minimum sum.
12+
*/
13+
@Test
14+
void testMinSumKSizeSubarray() {
15+
int[] arr = {2, 1, 5, 1, 3, 2};
16+
int k = 3;
17+
int expectedMinSum = 3; // 1 + 1 + 1
18+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
19+
}
20+
/**
21+
* Test for a different array and subarray size.
22+
*/
23+
@Test
24+
void testMinSumKSizeSubarrayWithDifferentValues() {
25+
int[] arr = {1, 2, 3, 4, 5};
26+
int k = 2;
27+
int expectedMinSum = 3; // 1 + 2
28+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
29+
}
30+
/**
31+
* Test for edge case with insufficient elements.
32+
*/
33+
@Test
34+
void testMinSumKSizeSubarrayWithInsufficientElements() {
35+
int[] arr = {1, 2};
36+
int k = 3; // Not enough elements
37+
int expectedMinSum = -1; // Edge case
38+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
39+
}
40+
/**
41+
* Test for large array.
42+
*/
43+
@Test
44+
void testMinSumKSizeSubarrayWithLargeArray() {
45+
int[] arr = {5, 4, 3, 2, 1, 0, -1, -2, -3, -4};
46+
int k = 5;
47+
int expectedMinSum = -10; // -1 + -2 + -3 + -4 + 0
48+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
49+
}
50+
/**
51+
* Test for array with negative numbers.
52+
*/
53+
@Test
54+
void testMinSumKSizeSubarrayWithNegativeNumbers() {
55+
int[] arr = {-1, -2, -3, -4, -5};
56+
int k = 2;
57+
int expectedMinSum = -9; // -4 + -5
58+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
59+
}
60+
/**
61+
* Test for the case where k equals the array length.
62+
*/
63+
@Test
64+
void testMinSumKSizeSubarrayWithKEqualToArrayLength() {
65+
int[] arr = {1, 2, 3, 4, 5};
66+
int k = 5;
67+
int expectedMinSum = 15; // 1 + 2 + 3 + 4 + 5
68+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
69+
}
70+
}

0 commit comments

Comments
 (0)