-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxSubarraySumTest.java
51 lines (42 loc) · 1.65 KB
/
MaxSubarraySumTest.java
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
51
package com.company.MaxSubarraySum;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MaxSubarraySumTest {
@Test
public void testEmptyArray() throws Exception {
assertEquals("Empty arrays should have a max of 0", 0, MaxSubarraySum.sequence(new int[]{}));
}
@Test
public void testExampleArray() throws Exception {
assertEquals("Example array should have a max of 6", 6, MaxSubarraySum.sequence(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
}
@Test
public void testNegativeExampleArray() throws Exception {
assertEquals("Example array with all negative values should have a max of 0", 0, MaxSubarraySum.sequence(new int[]{-2, -1, -3, -4, -1, -2, -1, -5, -4}));
}
@Test
public void testCompleteArray() throws Exception {
assertEquals("Should work on this too", 155, MaxSubarraySum.sequence(new int[]{7, 4, 11, -11, 39, 36, 10, -6, 37, -10, -32, 44, -26, -34, 43, 43}));
}
@Test
public void testRandomArrays() throws Exception {
for (int i = 0; i < 50; i++) {
int[] rand = randArr((int) (Math.random() * 10 + 50));
assertEquals("Random array solution not as expected: ", solution(rand), MaxSubarraySum.sequence(rand));
}
}
private int[] randArr(int size) {
int[] rand = new int[size];
for (int i = 0; i < size; i++) rand[i] = (int) (Math.random() * 60 - 30);
return rand;
}
private int solution(int[] arr) {
int m = 0, a = 0, s = 0;
for (int e : arr) {
s += e;
m = Math.min(s, m);
a = Math.max(a, s - m);
}
return a;
}
}