Skip to content

Commit cc4ad69

Browse files
author
Thi Nguyen
committed
Add test for ensureCapacity
1 parent 297c796 commit cc4ad69

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

core/src/test/java/com/graphhopper/storage/GraphHopperStorageWithTurnCostsTest.java

+46-5
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
*/
1818
package com.graphhopper.storage;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
22-
2320
import java.io.IOException;
24-
25-
import org.junit.Test;
21+
import java.util.Random;
2622

2723
import com.graphhopper.util.EdgeIteratorState;
2824
import com.graphhopper.util.Helper;
25+
import org.junit.Test;
26+
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertTrue;
2929

3030
/**
3131
*
@@ -93,4 +93,45 @@ public void testSave_and_fileFormat_withTurnCostEntries() throws IOException
9393
graph.edge(3, 4, 123, true).setWayGeometry(Helper.createPointList(4.4, 5.5, 6.6, 7.7));
9494
checkGraph(graph);
9595
}
96+
97+
@Test
98+
public void testEnsureCapacity() throws IOException {
99+
graph = newGraph(new MMapDirectory(defaultGraphLoc), false);
100+
graph.setSegmentSize(128);
101+
graph.create(100); // 100 is the minimum size
102+
103+
// assert that turnCostStorage can hold 104 turn cost entries at the beginning
104+
assertEquals(104, turnCostStorage.getCapacity() / 16);
105+
106+
Random r = new Random();
107+
108+
NodeAccess na = graph.getNodeAccess();
109+
for (int i = 0; i < 100; i++) {
110+
double randomLat = 90 * r.nextDouble();
111+
double randomLon = 180 * r.nextDouble();
112+
113+
na.setNode(i, randomLat, randomLon);
114+
}
115+
116+
// Make node 50 the 'center' node
117+
for (int nodeId = 51; nodeId < 100; nodeId++) {
118+
graph.edge(50, nodeId, r.nextDouble(), true);
119+
}
120+
for (int nodeId = 0; nodeId < 50; nodeId++) {
121+
graph.edge(nodeId, 50, r.nextDouble(), true);
122+
}
123+
124+
// add 100 turn cost entries around node 50
125+
for (int edgeId = 0; edgeId < 50; edgeId++) {
126+
turnCostStorage.addTurnInfo(50, edgeId, edgeId + 50, 1337);
127+
turnCostStorage.addTurnInfo(50, edgeId + 50, edgeId, 1337);
128+
}
129+
130+
turnCostStorage.addTurnInfo(50, 0, 1, 1337);
131+
assertEquals(104, turnCostStorage.getCapacity() / 16); // we are still good here
132+
133+
turnCostStorage.addTurnInfo(50, 0, 2, 1337);
134+
// A new segment should be added, which will support 128 / 16 = 8 more entries.
135+
assertEquals(112, turnCostStorage.getCapacity() / 16);
136+
}
96137
}

0 commit comments

Comments
 (0)