Ejemplo n.º 1
0
        private void VerifyExpectedPaths(
            Vertex startVertex,
            Vertex endVertex,
            IList <Edge> edges,
            PathFinderFactory pathFinderFactory,
            ExpectedPath[] expectedShortestPaths
            )
        {
            PathFinder pathFinder = pathFinderFactory.CreatePathFinder(
                edges,
                GraphEdgesValidationDesired.NO             // do the validation one time instead of doing it for each pathFinderFactory
                );
            IList <Path> actualShortestPaths = pathFinder.FindShortestPaths(startVertex, endVertex, 10);

            Assert.AreEqual(expectedShortestPaths.Length, actualShortestPaths.Count);

            string errorContext = pathFinderFactory.GetType().Name;

            for (int i = 0; i < expectedShortestPaths.Length; i++)
            {
                errorContext += " , i: " + i;
                ExpectedPath expectedPath = expectedShortestPaths[i];
                Path         actualPath   = actualShortestPaths[i];
                Assert.AreEqual(expectedPath.totalWeight, actualPath.TotalWeightForPath.WeightValue, SMALL_DELTA_VALUE_FOR_WEIGHT_COMPARISONS, errorContext);
                ExpectedEdge[] expectedEdgesForPath = expectedPath.expectedEdges;
                IList <Edge>   actualEdgesForPath   = actualPath.EdgesForPath;
                Assert.AreEqual(expectedEdgesForPath.Length, actualEdgesForPath.Count, errorContext);
                for (int j = 0; j < expectedEdgesForPath.Length; j++)
                {
                    errorContext += " , j=" + j;
                    ExpectedEdge expectedEdge = expectedEdgesForPath[j];
                    Edge         actualEdge   = actualEdgesForPath[j];
                    Assert.NotNull(actualEdge, errorContext);
                    Assert.NotNull(actualEdge.EdgeWeight, errorContext);
                    Assert.NotNull(actualEdge.StartVertex, errorContext);
                    Assert.NotNull(actualEdge.EndVertex, errorContext);
                    Assert.AreEqual(expectedEdge.weight, actualEdge.EdgeWeight.WeightValue, SMALL_DELTA_VALUE_FOR_WEIGHT_COMPARISONS, errorContext);
                    Assert.AreEqual(expectedEdge.startVertexId, actualEdge.StartVertex.VertexId, errorContext);
                    Assert.AreEqual(expectedEdge.endVertexId, actualEdge.EndVertex.VertexId, errorContext);
                    Assert.AreEqual(expectedEdge.edgeId, actualEdge.EdgeId, errorContext);
                }
            }
        }
Ejemplo n.º 2
0
        /**
         * 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
                    );
            }
        }