-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathSmallestSubarrayWithSumTest.java
More file actions
50 lines (43 loc) · 1.4 KB
/
SmallestSubarrayWithSumTest.java
File metadata and controls
50 lines (43 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.thealgorithms.slidingwindow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link SmallestSubarrayWithSum}.
*/
public class SmallestSubarrayWithSumTest {
@Test
public void testBasicCase() {
int[] arr = {2, 3, 1, 2, 4, 3};
int target = 7;
int expected = 2; // subarray [4, 3]
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
}
@Test
public void testNoValidSubarray() {
int[] arr = {1, 1, 1, 1};
int target = 10;
int expected = 0;
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
}
@Test
public void testSingleElement() {
int[] arr = {5};
int target = 5;
int expected = 1;
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
}
@Test
public void testAllElementsSame() {
int[] arr = {2, 2, 2, 2, 2};
int target = 6;
int expected = 3;
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
}
@Test
public void testLargeInput() {
int[] arr = {1, 2, 3, 4, 5, 6};
int target = 11;
int expected = 2; // subarray [5, 6]
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
}
}