Skip to content

Commit 819a9dd

Browse files
committed
Move some tests from node contractor to witness path searcher
1 parent 4f5d251 commit 819a9dd

File tree

2 files changed

+119
-68
lines changed

2 files changed

+119
-68
lines changed

core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -69,74 +69,6 @@ private NodeContractor createNodeContractor(PrepareCHGraph chGraph) {
6969
return nodeContractor;
7070
}
7171

72-
private void createExampleGraph() {
73-
//5-1-----2
74-
// \ __/|
75-
// 0 |
76-
// / |
77-
// 4-----3
78-
//
79-
graph.edge(0, 1, 1, true);
80-
graph.edge(0, 2, 1, true);
81-
graph.edge(0, 4, 3, true);
82-
graph.edge(1, 2, 3, true);
83-
graph.edge(2, 3, 1, true);
84-
graph.edge(4, 3, 2, true);
85-
graph.edge(5, 1, 2, true);
86-
graph.freeze();
87-
}
88-
89-
@Test
90-
public void testShortestPathSkipNode() {
91-
createExampleGraph();
92-
final double normalDist = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(4, 2).getDistance();
93-
NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher(pg);
94-
95-
setMaxLevelOnAllNodes();
96-
97-
algo.ignoreNode(3);
98-
algo.setWeightLimit(100);
99-
int nodeEntry = algo.findEndNode(4, 2);
100-
assertTrue(algo.getWeight(nodeEntry) > normalDist);
101-
102-
algo.clear();
103-
algo.setMaxVisitedNodes(1);
104-
nodeEntry = algo.findEndNode(4, 2);
105-
assertEquals(-1, nodeEntry);
106-
}
107-
108-
@Test
109-
public void testShortestPathSkipNode2() {
110-
createExampleGraph();
111-
final double normalDist = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(4, 2).getDistance();
112-
assertEquals(3, normalDist, 1e-5);
113-
NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher(pg);
114-
115-
setMaxLevelOnAllNodes();
116-
117-
algo.ignoreNode(3);
118-
algo.setWeightLimit(10);
119-
int nodeEntry = algo.findEndNode(4, 2);
120-
assertEquals(4, algo.getWeight(nodeEntry), 1e-5);
121-
122-
nodeEntry = algo.findEndNode(4, 1);
123-
assertEquals(4, algo.getWeight(nodeEntry), 1e-5);
124-
}
125-
126-
@Test
127-
public void testShortestPathLimit() {
128-
createExampleGraph();
129-
NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher(pg);
130-
131-
setMaxLevelOnAllNodes();
132-
133-
algo.ignoreNode(0);
134-
algo.setWeightLimit(2);
135-
int endNode = algo.findEndNode(4, 1);
136-
// did not reach endNode
137-
assertNotEquals(1, endNode);
138-
}
139-
14072
@Test
14173
public void testDirectedGraph() {
14274
//5 6 7
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Licensed to GraphHopper GmbH under one or more contributor
3+
* license agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership.
5+
*
6+
* GraphHopper GmbH licenses this file to you under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except in
8+
* compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.graphhopper.routing.ch;
20+
21+
import com.graphhopper.routing.Dijkstra;
22+
import com.graphhopper.routing.util.CarFlagEncoder;
23+
import com.graphhopper.routing.util.EncodingManager;
24+
import com.graphhopper.routing.util.TraversalMode;
25+
import com.graphhopper.routing.weighting.ShortestWeighting;
26+
import com.graphhopper.routing.weighting.Weighting;
27+
import com.graphhopper.storage.CHConfig;
28+
import com.graphhopper.storage.CHGraph;
29+
import com.graphhopper.storage.GraphBuilder;
30+
import com.graphhopper.storage.GraphHopperStorage;
31+
import org.junit.jupiter.api.Test;
32+
33+
import static org.junit.jupiter.api.Assertions.*;
34+
35+
class NodeBasedWitnessPathSearcherTest {
36+
37+
private final CarFlagEncoder encoder = new CarFlagEncoder();
38+
private final EncodingManager encodingManager = EncodingManager.create(encoder);
39+
private final Weighting weighting = new ShortestWeighting(encoder);
40+
private final GraphHopperStorage graph = new GraphBuilder(encodingManager).setCHConfigs(CHConfig.nodeBased("profile", weighting)).create();
41+
private final CHGraph lg = graph.getCHGraph();
42+
private final PrepareCHGraph pg = PrepareCHGraph.nodeBased(lg, weighting);
43+
44+
private void createExampleGraph() {
45+
//5-1-----2
46+
// \ __/|
47+
// 0 |
48+
// / |
49+
// 4-----3
50+
//
51+
graph.edge(0, 1, 1, true);
52+
graph.edge(0, 2, 1, true);
53+
graph.edge(0, 4, 3, true);
54+
graph.edge(1, 2, 3, true);
55+
graph.edge(2, 3, 1, true);
56+
graph.edge(4, 3, 2, true);
57+
graph.edge(5, 1, 2, true);
58+
graph.freeze();
59+
}
60+
61+
@Test
62+
public void testShortestPathSkipNode() {
63+
createExampleGraph();
64+
final double normalDist = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(4, 2).getDistance();
65+
NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher(pg);
66+
67+
setMaxLevelOnAllNodes();
68+
69+
algo.ignoreNode(3);
70+
algo.setWeightLimit(100);
71+
int nodeEntry = algo.findEndNode(4, 2);
72+
assertTrue(algo.getWeight(nodeEntry) > normalDist);
73+
74+
algo.clear();
75+
algo.setMaxVisitedNodes(1);
76+
nodeEntry = algo.findEndNode(4, 2);
77+
assertEquals(-1, nodeEntry);
78+
}
79+
80+
@Test
81+
public void testShortestPathSkipNode2() {
82+
createExampleGraph();
83+
final double normalDist = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(4, 2).getDistance();
84+
assertEquals(3, normalDist, 1e-5);
85+
NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher(pg);
86+
87+
setMaxLevelOnAllNodes();
88+
89+
algo.ignoreNode(3);
90+
algo.setWeightLimit(10);
91+
int nodeEntry = algo.findEndNode(4, 2);
92+
assertEquals(4, algo.getWeight(nodeEntry), 1e-5);
93+
94+
nodeEntry = algo.findEndNode(4, 1);
95+
assertEquals(4, algo.getWeight(nodeEntry), 1e-5);
96+
}
97+
98+
@Test
99+
public void testShortestPathLimit() {
100+
createExampleGraph();
101+
NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher(pg);
102+
103+
setMaxLevelOnAllNodes();
104+
105+
algo.ignoreNode(0);
106+
algo.setWeightLimit(2);
107+
int endNode = algo.findEndNode(4, 1);
108+
// did not reach endNode
109+
assertNotEquals(1, endNode);
110+
}
111+
112+
private void setMaxLevelOnAllNodes() {
113+
int nodes = pg.getNodes();
114+
for (int node = 0; node < nodes; node++) {
115+
pg.setLevel(node, nodes);
116+
}
117+
}
118+
119+
}

0 commit comments

Comments
 (0)