forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefixSumTest.java
More file actions
80 lines (65 loc) · 2.46 KB
/
PrefixSumTest.java
File metadata and controls
80 lines (65 loc) · 2.46 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.thealgorithms.prefixsum;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class PrefixSumTest {
@Test
@DisplayName("Test basic sum with positive integers")
void testStandardCase() {
int[] input = {1, 2, 3, 4, 5};
PrefixSum ps = new PrefixSum(input);
// Sum of range [0, 4] -> 15
assertEquals(15L, ps.sumRange(0, 4));
// Sum of range [1, 3] -> 9
assertEquals(9L, ps.sumRange(1, 3));
}
@Test
@DisplayName("Test array with negative numbers and zeros")
void testNegativeAndZeros() {
int[] input = {-2, 0, 3, -5, 2, -1};
PrefixSum ps = new PrefixSum(input);
assertEquals(1L, ps.sumRange(0, 2));
assertEquals(-1L, ps.sumRange(2, 5));
assertEquals(0L, ps.sumRange(1, 1));
}
@Test
@DisplayName("Test with large integers to verify overflow handling")
void testLargeNumbers() {
// Two values that fit in int, but their sum exceeds Integer.MAX_VALUE
// Integer.MAX_VALUE is approx 2.14 billion.
int val = 2_000_000_000;
int[] input = {val, val, val};
PrefixSum ps = new PrefixSum(input);
// Sum of three 2 billion values is 6 billion (fits in long, overflows int)
assertEquals(6_000_000_000L, ps.sumRange(0, 2));
}
@Test
@DisplayName("Test single element array")
void testSingleElement() {
int[] input = {42};
PrefixSum ps = new PrefixSum(input);
assertEquals(42L, ps.sumRange(0, 0));
}
@Test
@DisplayName("Test constructor with null input")
void testNullInput() {
assertThrows(IllegalArgumentException.class, () -> new PrefixSum(null));
}
@Test
@DisplayName("Test empty array behavior")
void testEmptyArray() {
int[] input = {};
PrefixSum ps = new PrefixSum(input);
assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRange(0, 0));
}
@Test
@DisplayName("Test invalid range indices")
void testInvalidIndices() {
int[] input = {10, 20, 30};
PrefixSum ps = new PrefixSum(input);
assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRange(-1, 1));
assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRange(0, 3));
assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRange(2, 1));
}
}