OsmAnd/DataExtractionOSM/src-tests/net/osmand/router/RouterTestsSuite.java

240 lines
9.7 KiB
Java
Raw Normal View History

package net.osmand.router;
2011-07-08 14:32:57 +02:00
2012-07-06 00:35:39 +02:00
import java.io.File;
import java.io.FileInputStream;
2011-07-08 14:32:57 +02:00
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
2012-07-06 00:35:39 +02:00
import java.util.ArrayList;
2011-07-08 14:32:57 +02:00
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
2012-07-13 09:51:00 +02:00
import net.osmand.NativeLibrary;
2011-07-08 14:32:57 +02:00
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.router.BinaryRoutePlanner;
2012-07-06 00:35:39 +02:00
import net.osmand.router.BinaryRoutePlanner.RouteSegment;
2011-07-08 14:32:57 +02:00
import net.osmand.router.RouteSegmentResult;
2012-06-16 12:06:04 +02:00
import net.osmand.router.RoutingConfiguration;
2011-07-08 14:32:57 +02:00
import net.osmand.router.RoutingContext;
2012-07-06 00:35:39 +02:00
import net.osmand.swing.DataExtractionSettings;
2011-07-08 14:32:57 +02:00
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class RouterTestsSuite {
2012-07-07 14:31:47 +02:00
private static class Parameters {
public File obfDir;
public List<File> tests = new ArrayList<File>();
public RoutingConfiguration.Builder configBuilder;
public static Parameters init(String[] args) throws SAXException, IOException {
Parameters p = new Parameters();
String routingXmlFile = null;
String obfDirectory = null;
BinaryRoutePlanner.PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST = false;
for (String a : args) {
if (a.startsWith("-routingXmlPath=")) {
routingXmlFile = a.substring("-routingXmlPath=".length());
} else if (a.startsWith("-verbose")) {
BinaryRoutePlanner.PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST = true;
} else if (a.startsWith("-obfDir=")) {
obfDirectory = a.substring("-obfDir=".length());
} else if (a.startsWith("-testDir=")) {
String testDirectory = a.substring("-testDir=".length());
for(File f : new File(testDirectory).listFiles()) {
if(f.getName().endsWith(".test.xml")){
p.tests.add(f);
}
}
} else if(!a.startsWith("-")){
p.tests.add(new File(a));
}
}
if (obfDirectory == null) {
obfDirectory = DataExtractionSettings.getSettings().getBinaryFilesDir();
}
if (obfDirectory != null && obfDirectory.length() > 0) {
p.obfDir = new File(obfDirectory);
}
if (routingXmlFile == null) {
routingXmlFile = DataExtractionSettings.getSettings().getRoutingXmlPath();
}
if (routingXmlFile.equals("routing.xml")) {
p.configBuilder = RoutingConfiguration.getDefault();
} else {
p.configBuilder = RoutingConfiguration.parseFromInputStream(new FileInputStream(routingXmlFile));
}
return p;
}
}
2011-07-08 14:32:57 +02:00
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
2012-07-07 14:31:47 +02:00
Parameters params = Parameters.init(args);
if(params.tests.isEmpty() || params.obfDir == null) {
println("Run router tests is console utility to test route calculation for osmand.");
println("\nUsage for run tests : runTestsSuite [-routingXmlPath=PATH] [-verbose] [-obfDir=PATH] [-testDir=PATH] {individualTestPath}");
return;
}
2012-07-06 00:35:39 +02:00
List<File> files = new ArrayList<File>();
2012-07-07 14:31:47 +02:00
for (File f : params.obfDir.listFiles()) {
2012-07-06 00:35:39 +02:00
if (f.getName().endsWith(".obf")) {
files.add(f);
}
}
BinaryMapIndexReader[] rs = new BinaryMapIndexReader[files.size()];
int it = 0;
for (File f : files) {
RandomAccessFile raf = new RandomAccessFile(f, "r"); //$NON-NLS-1$ //$NON-NLS-2$
rs[it++] = new BinaryMapIndexReader(raf, false);
}
2011-07-08 14:32:57 +02:00
boolean allSuccess = true;
2012-07-07 14:31:47 +02:00
for(File f : params.tests) {
2012-07-13 09:51:00 +02:00
allSuccess &= test(null, new FileInputStream(f), rs, params.configBuilder);
2012-07-06 00:35:39 +02:00
}
2011-07-08 14:32:57 +02:00
if (allSuccess) {
System.out.println("All is successfull");
}
}
2012-07-07 14:31:47 +02:00
private static void println(String string) {
System.out.println(string);
}
2012-07-13 09:51:00 +02:00
public static boolean test(NativeLibrary lib, InputStream resource, BinaryMapIndexReader[] rs, RoutingConfiguration.Builder config) throws SAXException, IOException, ParserConfigurationException {
2011-07-08 14:32:57 +02:00
Document testSuite = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(resource));
NodeList tests = testSuite.getElementsByTagName("test");
2012-07-07 14:31:47 +02:00
for (int i = 0; i < tests.getLength(); i++) {
2011-07-08 14:32:57 +02:00
Element e = (Element) tests.item(i);
2012-07-13 09:51:00 +02:00
BinaryRoutePlanner router = new BinaryRoutePlanner(lib, rs);
2012-07-14 17:25:26 +02:00
RoutingConfiguration.DEFAULT_DESIRABLE_TILES_IN_MEMORY = 50;
2012-07-07 14:31:47 +02:00
testRoute(e, router, config);
2011-07-08 14:32:57 +02:00
}
2012-07-07 14:31:47 +02:00
2011-07-08 14:32:57 +02:00
return true;
2012-07-06 00:35:39 +02:00
}
private static float parseFloat(Element e, String attr) {
if(e.getAttribute(attr) == null || e.getAttribute(attr).length() == 0){
return 0;
}
return Float.parseFloat(e.getAttribute(attr));
}
2012-07-07 14:31:47 +02:00
private static boolean isInOrLess(float expected, float value, float percent){
2012-07-06 00:35:39 +02:00
if(Math.abs(value/expected - 1) < percent / 100){
return true;
}
2012-07-07 14:31:47 +02:00
if(value < expected) {
System.err.println("Test could be adjusted value " + value + " is much less then expected " + expected);
return true;
}
2012-07-06 00:35:39 +02:00
return false;
2011-07-08 14:32:57 +02:00
}
2012-07-07 14:31:47 +02:00
private static void testRoute(Element testCase, BinaryRoutePlanner planner, RoutingConfiguration.Builder config) throws IOException, SAXException {
2011-07-08 14:32:57 +02:00
String vehicle = testCase.getAttribute("vehicle");
2012-07-06 00:35:39 +02:00
int loadedTiles = (int) parseFloat(testCase, "loadedTiles");
int visitedSegments = (int) parseFloat(testCase, "visitedSegments");
int complete_time = (int) parseFloat(testCase, "complete_time");
int complete_distance = (int) parseFloat(testCase, "complete_distance");
float percent = parseFloat(testCase, "best_percent");
2011-07-08 14:32:57 +02:00
String testDescription = testCase.getAttribute("description");
2012-07-06 00:35:39 +02:00
if(percent == 0){
System.err.println("\n\n!! Skipped test case '" + testDescription + "' because 'best_percent' attribute is not specified \n\n" );
return;
}
2012-07-07 14:31:47 +02:00
RoutingContext ctx = new RoutingContext(config.build(vehicle, true));
String skip = testCase.getAttribute("skip_comment");
if (skip != null && skip.length() > 0) {
System.err.println("\n\n!! Skipped test case '" + testDescription + "' because '" + skip + "'\n\n" );
return;
}
2012-07-06 00:35:39 +02:00
System.out.println("Run test " + testDescription);
2011-07-08 14:32:57 +02:00
double startLat = Double.parseDouble(testCase.getAttribute("start_lat"));
double startLon = Double.parseDouble(testCase.getAttribute("start_lon"));
RouteSegment startSegment = planner.findRouteSegment(startLat, startLon, ctx);
double endLat = Double.parseDouble(testCase.getAttribute("target_lat"));
double endLon = Double.parseDouble(testCase.getAttribute("target_lon"));
RouteSegment endSegment = planner.findRouteSegment(endLat, endLon, ctx);
if(startSegment == null){
throw new IllegalArgumentException("Start segment is not found for test : " + testDescription);
}
if(endSegment == null){
throw new IllegalArgumentException("End segment is not found for test : " + testDescription);
}
2012-06-21 09:18:08 +02:00
List<RouteSegmentResult> route = planner.searchRoute(ctx, startSegment, endSegment, false);
2012-07-06 00:35:39 +02:00
float completeTime = 0;
float completeDistance = 0;
for (int i = 0; i < route.size(); i++) {
completeTime += route.get(i).getSegmentTime();
completeDistance += route.get(i).getDistance();
}
2012-07-07 14:31:47 +02:00
if(complete_time > 0 && !isInOrLess(complete_time, completeTime, percent)) {
2012-07-06 00:35:39 +02:00
throw new IllegalArgumentException(String.format("Complete time (expected) %s != %s (original) : %s", complete_time, completeTime, testDescription));
}
2012-07-07 14:31:47 +02:00
if(complete_distance > 0 && !isInOrLess(complete_distance, completeDistance, percent)) {
2012-07-06 00:35:39 +02:00
throw new IllegalArgumentException(String.format("Complete distance (expected) %s != %s (original) : %s", complete_distance, completeDistance, testDescription));
}
2012-07-07 14:31:47 +02:00
if(visitedSegments > 0 && !isInOrLess(visitedSegments, ctx.visitedSegments, percent)) {
2012-07-06 00:35:39 +02:00
throw new IllegalArgumentException(String.format("Visited segments (expected) %s != %s (original) : %s", visitedSegments, ctx.visitedSegments, testDescription));
}
2012-07-07 14:31:47 +02:00
if(loadedTiles > 0 && !isInOrLess(loadedTiles, ctx.loadedTiles, percent)) {
2012-07-06 00:35:39 +02:00
throw new IllegalArgumentException(String.format("Loaded tiles (expected) %s != %s (original) : %s", loadedTiles, ctx.loadedTiles, testDescription));
}
2011-07-08 14:32:57 +02:00
2012-07-06 00:35:39 +02:00
// NodeList segments = compareBySegment(testCase, testDescription, route);
// if(segments.getLength() < route.size()){
// throw new IllegalArgumentException("Expected route is shorter than calculated for test : " + testDescription);
// } else if(segments.getLength() > route.size()){
// throw new IllegalArgumentException("Expected route is more lengthy than calculated for test : " + testDescription);
// }
}
2012-07-07 14:31:47 +02:00
protected static NodeList compareBySegment(Element testCase, String testDescription, List<RouteSegmentResult> route) {
2011-07-08 14:32:57 +02:00
NodeList segments = testCase.getElementsByTagName("segment");
int i = 0;
while (i < segments.getLength() && i < route.size()) {
Element segment = (Element) segments.item(i);
long expectedId = Long.parseLong(segment.getAttribute("id"));
int expectedStart = Integer.parseInt(segment.getAttribute("start"));
int expectedEnd = Integer.parseInt(segment.getAttribute("end"));
RouteSegmentResult segmentResult = route.get(i);
2012-06-11 22:28:57 +02:00
if (expectedId != segmentResult.getObject().getId() >> 1) {
2011-07-08 14:32:57 +02:00
throw new IllegalArgumentException("Test : '" + testDescription + "' on segment " + (i + 1) + " : " + "\n"
2012-06-11 22:28:57 +02:00
+ "(expected route id) " + expectedId + " != " + (segmentResult.getObject().getId() >> 1) + " (actual route id)");
2011-07-08 14:32:57 +02:00
}
2012-06-11 22:28:57 +02:00
if (expectedStart != segmentResult.getStartPointIndex()) {
2011-07-08 14:32:57 +02:00
throw new IllegalArgumentException("Test : '" + testDescription + "' on segment " + (i + 1) + " : " + "\n"
2012-06-11 22:28:57 +02:00
+ "(expected start index) " + expectedStart + " != " + segmentResult.getStartPointIndex() + " (actual start index)");
2011-07-08 14:32:57 +02:00
}
2012-06-11 22:28:57 +02:00
if (expectedEnd != segmentResult.getEndPointIndex()) {
2011-07-08 14:32:57 +02:00
throw new IllegalArgumentException("Test : '" + testDescription + "' on segment " + (i + 1) + " : " + "\n"
2012-06-11 22:28:57 +02:00
+ "(expected end index) " + expectedEnd + " != " + segmentResult.getEndPointIndex() + " (actual end index)");
2011-07-08 14:32:57 +02:00
}
i++;
}
2012-07-06 00:35:39 +02:00
return segments;
2011-07-08 14:32:57 +02:00
}
}