Repository navigation
Expand file tree
/
Copy pathFenwickTreeTest.java
More file actions
115 lines (100 loc) · 3.33 KB
/
Copy pathFenwickTreeTest.java
File metadata and controls
115 lines (100 loc) · 3.33 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.thealgorithms.datastructures.trees;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Random;
import org.junit.jupiter.api.Test;
class FenwickTreeTest {
@Test
void queryOnFreshTreeReturnsZero() {
FenwickTree tree = new FenwickTree(5);
for (int i = 0; i < 5; i++) {
assertEquals(0, tree.query(i));
}
}
@Test
void singleElementTree() {
FenwickTree tree = new FenwickTree(1);
tree.update(0, 7);
assertEquals(7, tree.query(0));
}
@Test
void prefixSumsAfterUpdates() {
FenwickTree tree = new FenwickTree(5);
int[] values = {3, 2, -1, 6, 5};
for (int i = 0; i < values.length; i++) {
tree.update(i, values[i]);
}
assertEquals(3, tree.query(0));
assertEquals(5, tree.query(1));
assertEquals(4, tree.query(2));
assertEquals(10, tree.query(3));
assertEquals(15, tree.query(4));
}
@Test
void repeatedUpdatesOnSameIndexAccumulate() {
FenwickTree tree = new FenwickTree(4);
tree.update(2, 5);
tree.update(2, 3);
tree.update(2, -2);
assertEquals(0, tree.query(1));
assertEquals(6, tree.query(2));
assertEquals(6, tree.query(3));
}
@Test
void negativeValues() {
FenwickTree tree = new FenwickTree(3);
tree.update(0, -4);
tree.update(1, -6);
tree.update(2, 10);
assertEquals(-4, tree.query(0));
assertEquals(-10, tree.query(1));
assertEquals(0, tree.query(2));
}
@Test
void nonPowerOfTwoSize() {
int size = 13;
FenwickTree tree = new FenwickTree(size);
for (int i = 0; i < size; i++) {
tree.update(i, 1);
}
for (int i = 0; i < size; i++) {
assertEquals(i + 1, tree.query(i));
}
}
@Test
void matchesNaivePrefixSumOnRandomData() {
Random random = new Random(42);
int size = 100;
FenwickTree tree = new FenwickTree(size);
int[] naive = new int[size];
for (int step = 0; step < 500; step++) {
int index = random.nextInt(size);
int delta = random.nextInt(21) - 10;
tree.update(index, delta);
naive[index] += delta;
}
int running = 0;
for (int i = 0; i < size; i++) {
running += naive[i];
assertEquals(running, tree.query(i), "Mismatch at index " + i);
}
}
@Test
void zeroSizeTreeRejectsAnyIndex() {
FenwickTree tree = new FenwickTree(0);
assertThrows(IndexOutOfBoundsException.class, () -> tree.update(0, 1));
assertThrows(IndexOutOfBoundsException.class, () -> tree.query(0));
}
@Test
void negativeSizeThrows() {
assertThrows(IllegalArgumentException.class, () -> new FenwickTree(-1));
}
@Test
void outOfBoundsIndicesThrow() {
FenwickTree tree = new FenwickTree(5);
assertThrows(IndexOutOfBoundsException.class, () -> tree.update(-1, 1));
assertThrows(IndexOutOfBoundsException.class, () -> tree.update(5, 1));
assertThrows(IndexOutOfBoundsException.class, () -> tree.query(-1));
assertThrows(IndexOutOfBoundsException.class, () -> tree.query(5));
}
}