Skip to content

Commit 933d661

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
Segment tree test
1 parent 766d35d commit 933d661

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

src/main/java/com/williamfiset/algorithms/utils/TestUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ public static int[] randomIntegerArray(int sz, int min, int max) {
1212
return ar;
1313
}
1414

15+
// Generates an array of random values where every number is between
16+
// [min, max) and there are possible repeats.
17+
public static long[] randomLongArray(int sz, long min, long max) {
18+
long[] ar = new long[sz];
19+
for (int i = 0; i < sz; i++) ar[i] = randValue(min, max);
20+
return ar;
21+
}
22+
1523
// Generates a list of random values where every number is between
1624
// [min, max) and there are possible repeats.
1725
public static List<Integer> randomIntegerList(int sz, int min, int max) {
@@ -33,4 +41,9 @@ public static List<Integer> randomUniformUniqueIntegerList(int sz) {
3341
public static int randValue(int min, int max) {
3442
return min + (int) (Math.random() * ((max - min)));
3543
}
44+
45+
// Generates a random number between [min, max)
46+
public static long randValue(long min, long max) {
47+
return min + (long) (Math.random() * ((max - min)));
48+
}
3649
}
Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package com.williamfiset.algorithms.datastructures.segmenttree;
22

3+
import static com.google.common.truth.Truth.assertThat;
34
import static org.junit.Assert.*;
45

6+
import com.williamfiset.algorithms.utils.TestUtils;
57
import org.junit.Before;
68
import org.junit.Test;
79

810
public class SegmentTreeWithPointersTest {
911

10-
static final int LOOPS = 50;
11-
static final int TEST_SZ = 1000;
12-
static final int MIN_RAND_NUM = 0;
13-
static final int MAX_RAND_NUM = +2000;
14-
1512
@Before
1613
public void setup() {}
1714

@@ -28,28 +25,37 @@ public void testIllegalSegmentTreeCreation2() {
2825

2926
@Test
3027
public void testSumQuery() {
31-
3228
int[] values = {1, 2, 3, 4, 5};
3329
Node tree = new Node(values);
3430

35-
assertEquals(1, tree.sum(0, 1));
36-
assertEquals(2, tree.sum(1, 2));
37-
assertEquals(3, tree.sum(2, 3));
38-
assertEquals(4, tree.sum(3, 4));
39-
assertEquals(5, tree.sum(4, 5));
31+
assertThat(tree.sum(0, 1)).isEqualTo(1);
32+
assertThat(tree.sum(1, 2)).isEqualTo(2);
33+
assertThat(tree.sum(2, 3)).isEqualTo(3);
34+
assertThat(tree.sum(3, 4)).isEqualTo(4);
35+
assertThat(tree.sum(4, 5)).isEqualTo(5);
4036
}
4137

42-
// Select a lower bound index for the Fenwick tree
43-
public static int lowBound(int N) {
44-
return (int) (Math.random() * N);
45-
}
46-
47-
// Select an upper bound index for the Fenwick tree
48-
public static int highBound(int low, int N) {
49-
return Math.min(N, low + (int) (Math.random() * N));
38+
@Test
39+
public void testAllSumQueries() {
40+
int n = 100;
41+
int[] ar = TestUtils.randomIntegerArray(n, -1000, +1000);
42+
Node tree = new Node(ar);
43+
44+
for (int i = 0; i < n; i++) {
45+
for (int j = i + 1; j < n; j++) {
46+
long bfSum = bruteForceSum(ar, i, j);
47+
long segTreeSum = tree.sum(i, j);
48+
assertThat(bfSum).isEqualTo(segTreeSum);
49+
}
50+
}
5051
}
5152

52-
public static long randValue() {
53-
return (long) (Math.random() * MAX_RAND_NUM * 2) + MIN_RAND_NUM;
53+
// Finds the sum in an array between [l, r) in the `values` array
54+
private static long bruteForceSum(int[] values, int l, int r) {
55+
long s = 0;
56+
for (int i = l; i < r; i++) {
57+
s += values[i];
58+
}
59+
return s;
5460
}
5561
}

0 commit comments

Comments
 (0)