private IDictionary <string, E> mapWithEdges; // lazy loaded protected GraphGenericsImpl( IList <E> edges, GraphEdgesValidationDesired graphEdgesValidationDesired ) { this.edges = new ReadOnlyCollection <E>(edges); if (graphEdgesValidationDesired == GraphEdgesValidationDesired.YES) { GraphEdgesValidator <PathGenerics <E, V, W>, E, V, W> .ValidateEdgesForGraphCreation <PathGenerics <E, V, W>, E, V, W>(this.edges); } }
private void VerifyExpectedResults(IList <Edge> edges) { // the parameter GraphEdgesValidationDesired.NO will be used so therefore do the validation once externally here first GraphEdgesValidator <Path, Edge, Vertex, Weight> .ValidateEdgesForGraphCreation <Path, Edge, Vertex, Weight>(edges); ExpectedPath[] expectedPaths = GetExpectedPaths(); IList <PathFinderFactory> pathFinderFactories = PathFinderFactories.CreatePathFinderFactories(); foreach (PathFinderFactory pathFinderFactory in pathFinderFactories) { VerifyExpectedPaths(a, d, edges, pathFinderFactory, expectedPaths); } }
private static void PerformRoadRoutingForTheImplementations( IList <Road> roads, City startCity, City endCity, IList <PathFinderFactoryGenerics <PathFinderGenerics <PathGenerics <Road, City, WeightDeterminedByRoadLengthAndQuality>, Road, City, WeightDeterminedByRoadLengthAndQuality>, PathGenerics <Road, City, WeightDeterminedByRoadLengthAndQuality>, Road, City, WeightDeterminedByRoadLengthAndQuality> > pathFinderFactories ) { // the parameter GraphEdgesValidationDesired.NO will be used so therefore do the validation once externally here first GraphEdgesValidator <PathGenerics <Road, City, WeightDeterminedByRoadLengthAndQuality>, Road, City, WeightDeterminedByRoadLengthAndQuality> .ValidateEdgesForGraphCreation <PathGenerics <Road, City, WeightDeterminedByRoadLengthAndQuality>, Road, City, WeightDeterminedByRoadLengthAndQuality>(roads); foreach (PathFinderFactoryGenerics <PathFinderGenerics <PathGenerics <Road, City, WeightDeterminedByRoadLengthAndQuality>, Road, City, WeightDeterminedByRoadLengthAndQuality>, PathGenerics <Road, City, WeightDeterminedByRoadLengthAndQuality>, Road, City, WeightDeterminedByRoadLengthAndQuality> pathFinderFactory in pathFinderFactories) { PerformRoadRouting(roads, startCity, endCity, pathFinderFactory); } }
private void runTestCaseDefinedInXmlFileWithPathIncludingDirectory( string pathToResourceXmlFile, IList <PathFinderFactory> pathFinderFactories, bool shouldAlsoTestResultsWithImplementationsAgainstEachOther = true ) { XmlDocument document = xmlFileReader.GetResourceFileAsXmlDocument(pathToResourceXmlFile); XmlNodeList nodeList = xmlFileReader.GetNodeListMatchingXPathExpression(document, "graphTestData/graphDefinition"); Assert.NotNull(nodeList); Assert.AreEqual(1, nodeList.Count); // should only be one graph definition per file XmlNode nodeWithGraphDefinition = nodeList.Item(0); Assert.NotNull(nodeWithGraphDefinition); IList <Edge> edgesForGraphPotentiallyIncludingDuplicatedEdges = edgeParser.FromMultiLinedStringToListOfEdges(nodeWithGraphDefinition.InnerText); // System.out.println("efetr fromMultiLinedStringToListOfEdges " + edgesForGraphPotentiallyIncludingDuplicatedEdges.get(0).getClass()); //System.out.println("edgesForGraphPotentiallyIncludingDuplicatedEdges " + edgesForGraphPotentiallyIncludingDuplicatedEdges.size()); // There can be duplicates in the list of edges, whcih would cause exception at validation, // so therefore below instead remove duplicated with a chosen strategy // and one example file with many duplicated edges is "small_road_network_01.xml" IList <Edge> edgesForGraph = edgeUtility.GetEdgesWithoutDuplicates(edgesForGraphPotentiallyIncludingDuplicatedEdges, SelectionStrategyWhenEdgesAreDuplicated.FIRST_IN_LIST_OF_EDGES); //System.out.println("edgesForGraph " + edgesForGraph.size()); // edgesForGraphPotentiallyIncludingDuplicatedEdges 28524 // edgesForGraph 28320 // The above output was the result when running tests for the file "small_road_network_01.xml" // System.out.println("efetr getEdgesWithoutDuplicates " + edgesForGraph.get(0).getClass()); PathParser <Path, Edge, Vertex, Weight> pathParser = PathParser <Path, Edge, Vertex, Weight> .CreatePathParserDefault(edgesForGraph); XmlNodeList nodeListWithTestCases = xmlFileReader.GetNodeListMatchingXPathExpression(document, "graphTestData/testCase"); Assert.NotNull(nodeListWithTestCases); // shouold be zero rather than null i.e. this is okay to test //assertEquals(1, nodeListWithTestCases.getLength()); // can be many (or zero, then just validat implementatins against each other) for (int i = 0; i < nodeListWithTestCases.Count; i++) { XmlNode itemWithTestCase = nodeListWithTestCases.Item(i); XmlNodeList nodeListWithInput = xmlFileReader.GetNodeListMatchingXPathExpression(itemWithTestCase, "input"); string outputExpectedAsMultiLinedString = xmlFileReader.GetTextContentNodeOfFirstSubNode(itemWithTestCase, "outputExpected"); // System.out.println("outputExpectedAsMultiLinedString " + outputExpectedAsMultiLinedString); //List fromStringToListOfPaths = pathParser.fromStringToListOfPaths(outputExpectedAsMultiLinedString); IList <Path> expectedListOfPaths = outputExpectedAsMultiLinedString == null || outputExpectedAsMultiLinedString.Trim().Equals("") ? null : pathParser.FromStringToListOfPaths(outputExpectedAsMultiLinedString); GraphEdgesValidator <Path, Edge, Vertex, Weight> edgeGraphEdgesValidator = GraphEdgesValidator <Path, Edge, Vertex, Weight> .CreateGraphEdgesValidator <Path, Edge, Vertex, Weight>(); edgeGraphEdgesValidator.ValidateEdgesAsAcceptableInputForGraphConstruction(edgesForGraph); if (expectedListOfPaths != null) { edgeGraphEdgesValidator.ValidateAllPathsOnlyContainEdgesDefinedInGraph(expectedListOfPaths, edgesForGraph); } Assert.AreEqual(1, nodeListWithInput.Count); // should only be one input element XmlNode nodeWithInputForTestCase = nodeListWithInput.Item(0); //final NodeList nodeListWithInput = xmlFileReader.getNodeListMatchingXPathExpression(itemWithTestCase, "input"); string startVertexId = xmlFileReader.GetTextContentNodeOfFirstSubNode(nodeWithInputForTestCase, "startVertex"); string endVertexId = xmlFileReader.GetTextContentNodeOfFirstSubNode(nodeWithInputForTestCase, "endVertex"); // System.out.println("startVertexId " + startVertexId); // System.out.println("endVertexId " + endVertexId); string maxNumberOfPathsAsString = xmlFileReader.GetTextContentNodeOfFirstSubNode(nodeWithInputForTestCase, "maxNumberOfPaths"); int maxNumberOfPaths = int.Parse(maxNumberOfPathsAsString); // System.out.println("maxNumberOfPaths " + maxNumberOfPaths); // System.out.println("innan graphShortestPathAssertionHelper.testResultsWithImplementationsAgainstEachOther " + edgesForGraph.get(0).getClass()); // pathToResourceXmlFile graphShortestPathAssertionHelper.AssertExpectedResultsOrAssertImplementationsWithEachOther( edgesForGraph, CreateVertex(startVertexId), CreateVertex(endVertexId), maxNumberOfPaths, pathFinderFactories, expectedListOfPaths, // null, // expectedListOfPaths , use null when we do not want to fail because of expected output according to xml but maybe instyead want to print output with below paaraeter pathToResourceXmlFile, shouldAlsoTestResultsWithImplementationsAgainstEachOther ); } }
/** * Overloaded method. Note that the last parameter can be null if we only want to compare * results from different implementations. * The second last parameter is used when we also have an expected path retrieved for example from an xml file. * The last parameter can be used temporarirly for "debugging" purposes when we want to display the results to the console * See comment at class level. */ public void AssertExpectedResultsOrAssertImplementationsWithEachOther( IList <Edge> edgesForBigGraph, Vertex startVertex, Vertex endVertex, int numberOfPathsToFind, IList <PathFinderFactory> pathFinderFactoriesForImplementationsToTest, IList <Path> expectedListOfPaths, string optionalPathToResourceXmlFile, bool shouldTestResultsWithImplementationsAgainstEachOther = false ) { // TODO: clean up this method e.g. regarding "shouldAlsoTestResultsWithImplementationsAgainstEachOther" //this.SetConsoleOutputDesired(ConsoleOutputDesired.TIME_MEASURE); string messagePrefixWithInformationAboutXmlSourcefileWithTestData = optionalPathToResourceXmlFile == null ? "" : "Xml file with test data: " + optionalPathToResourceXmlFile + " . "; output("Number of edges in the graph to be tested : " + edgesForBigGraph.Count); IDictionary <string, IList <Path> > shortestPathsPerImplementation = new Dictionary <string, IList <Path> >(); // the parameter GraphEdgesValidationDesired.NO will be used so therefore do the validation once externally here first GraphEdgesValidator <Path, Edge, Vertex, Weight> .ValidateEdgesForGraphCreation <Path, Edge, Vertex, Weight>(edgesForBigGraph); PathParser <Path, Edge, Vertex, Weight> pathParser = PathParser <Path, Edge, Vertex, Weight> .CreatePathParserDefault(edgesForBigGraph); //assertThat("At least some implementation should be used", pathFinderFactoriesForImplementationsToTest.size(), greaterThanOrEqualTo(1)); // TODO: "hamcrest" syntax similar to above java code Assert.That(pathFinderFactoriesForImplementationsToTest.Count >= 1, "At least some implementation should be used"); for (int i = 0; i < pathFinderFactoriesForImplementationsToTest.Count; i++) { PathFinderFactory pathFinderFactory = pathFinderFactoriesForImplementationsToTest[i]; output("Will now test file " + optionalPathToResourceXmlFile + " with impl " + pathFinderFactory.GetType().Name); TimeMeasurer tm = TimeMeasurer.Start(); PathFinder pathFinder = pathFinderFactory.CreatePathFinder( edgesForBigGraph, GraphEdgesValidationDesired.NO // do the validation one time instead of doing it for each pathFinderFactory ); IList <Path> shortestPaths = pathFinder.FindShortestPaths(startVertex, endVertex, numberOfPathsToFind); Assert.IsNotNull(shortestPaths); //assertThat("At least some path should be found", shortestPaths.size(), greaterThanOrEqualTo(1)); Assert.That(shortestPaths.Count >= 1, "At least some path should be found"); // TODO "hamcrest" syntax as java above output( messagePrefixWithInformationAboutXmlSourcefileWithTestData + "Seconds: " + tm.GetSeconds() + ". Implementation: " + pathFinder.GetType().Name, ConsoleOutputDesired.TIME_MEASURE ); if (isAllConsoleOutputDesired()) { DisplayListOfShortestPath(shortestPaths); output("Implementation class for above and below output: " + pathFinderFactory.GetType().Name); DisplayAsPathStringsWhichCanBeUsedInXml(shortestPaths, pathParser); } shortestPathsPerImplementation.Add(pathFinder.GetType().Name, shortestPaths); } IList <string> nameOfImplementations = new List <string>(shortestPathsPerImplementation.Keys); if (expectedListOfPaths != null) { AssertResultsWithExpectedPaths( expectedListOfPaths, optionalPathToResourceXmlFile, shortestPathsPerImplementation ); } else // if(expectedListOfPaths != null) { if (shouldTestResultsWithImplementationsAgainstEachOther) { AssertResultsWithImplementationsAgainstEachOther( optionalPathToResourceXmlFile, shortestPathsPerImplementation ); } }