-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTreeTest.java
83 lines (69 loc) · 2.71 KB
/
RTreeTest.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
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
package edu.ecnu.wclong;
import edu.ecnu.wclong.sdo.Bound;
import edu.ecnu.wclong.sdo.BoundVector;
import edu.ecnu.wclong.sdo.RTreeEntry;
import edu.ecnu.wclong.sdo.Rectangle;
import edu.ecnu.wclong.util.RTreeUtil;
import edu.ecnu.wclong.util.RandomUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Set;
class RTreeTest {
private RTree<Integer> rTree;
private static final int DIMENSION = 2;
@BeforeEach
public void initRTree() {
rTree = new RTree<>(5);
insert();
}
@Test
public void test(){
insert();
RTreeUtil.printRTreeRectangle(rTree);
}
@Test
public void search() {
BoundVector boundVector = new BoundVector(DIMENSION);
int value = RandomUtil.getIntegerRandomNumber(DIMENSION, 100);
for (int i = 0; i < DIMENSION; ++i) {
boundVector.setDimensionBound(i, new Bound(value - 1 - i, value + 1 + i));
}
Set<RTreeEntry<Integer>> results = rTree.search(new Rectangle(boundVector));
}
public void insert() {
for (int i = 0; i < 10; ++i) {
RTreeEntry<Integer> entry = generateRandomEntry();
rTree.insert(entry);
}
}
@Test
public void delete() {
int value = RandomUtil.getIntegerRandomNumber(DIMENSION, 100);
BoundVector boundVector = new BoundVector(DIMENSION);
for (int i = 0; i < DIMENSION; ++i) {
boundVector.setDimensionBound(i, new Bound(value - 1 - i, value + 1 + i));
}
RTreeEntry<Integer> rTreeEntry = new RTreeEntry<>(new Rectangle(boundVector),value);
System.out.println(rTree.delete(rTreeEntry));
}
@Test
public void update() {
BoundVector boundVector = new BoundVector(DIMENSION);
int value = RandomUtil.getIntegerRandomNumber(DIMENSION, 100);
for (int i = 0; i < DIMENSION; ++i) {
boundVector.setDimensionBound(i, new Bound(value - 1 - i, value + 1 + i));
}
Set<RTreeEntry<Integer>> results = rTree.search(new Rectangle(boundVector));
RTreeEntry<Integer> rTreeEntry = results.stream().findAny().orElseThrow(() -> new RuntimeException("find nothing"));
rTree.update(rTreeEntry.getId(),RandomUtil.getIntegerRandomNumber());
}
private RTreeEntry<Integer> generateRandomEntry() {
int value = RandomUtil.getIntegerRandomNumber(DIMENSION, 50);
BoundVector boundVector = new BoundVector(DIMENSION);
for (int i = 0; i < DIMENSION; ++i) {
boundVector.setDimensionBound(i, new Bound(value - 1 - i, value + 1 + i));
}
Rectangle rectangle = new Rectangle(boundVector);
return new RTreeEntry<>(rectangle, value);
}
}