Ejemplo n.º 1
0
        public Graph <char> ReadAndBuildRoutes()
        {
            WriteInputInstructions();
            var graphBuilder = new DirectedGraphBuilder <char>();

            string routeInput;

            while ((routeInput = ReadRouteInput()) != "OK")
            {
                try
                {
                    var routeModel = ParseAndValidateRouteInput(graphBuilder, routeInput);
                    AddRoute(graphBuilder, routeModel);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine($"Error: {ex.Message}\n");
                }
            }

            var routesGraph = graphBuilder.GetGraph();

            WriteInputSummary(routesGraph);

            return(routesGraph);
        }
Ejemplo n.º 2
0
        private RouteModel ParseAndValidateRouteInput(DirectedGraphBuilder <char> graphBuilder, string routeInput)
        {
            var routeModel = routeInputParser.ParseInputRouteToModel(routeInput);

            routeValidator.ValidateRoute(routeModel, graphBuilder.GetGraph());

            return(routeModel);
        }
Ejemplo n.º 3
0
        public void ValidateRoute_RepeatedRoute_ThrowsException()
        {
            var graphBuilder = new DirectedGraphBuilder <char>();
            var routeModel   = new RouteModel {
                Source = 'A', Destination = 'B', Distance = 5
            };

            graphBuilder.AddEdge(routeModel.Source, routeModel.Destination, routeModel.Distance);

            var routeValidator = new RouteValidator();

            routeValidator.ValidateRoute(routeModel, graphBuilder.GetGraph());
        }
Ejemplo n.º 4
0
        public void Execute_EdgeNotExists_DefaultResult()
        {
            var graphBuilder = new DirectedGraphBuilder <char>();

            graphBuilder.AddEdge('A', 'C', 5);

            var calculator = new RouteDistanceCalculator <char>(graphBuilder.GetGraph());

            var route          = new char[] { 'A', 'B' };
            var expectedResult = -1;

            var result = calculator.Execute(route);

            Assert.AreEqual(expectedResult, result);
        }
Ejemplo n.º 5
0
        public static Graph <char> BuildDefaultTestGraph()
        {
            var graphBuilder = new DirectedGraphBuilder <char>();

            graphBuilder.AddEdge('A', 'B', 5);
            graphBuilder.AddEdge('B', 'C', 4);
            graphBuilder.AddEdge('C', 'D', 8);
            graphBuilder.AddEdge('D', 'C', 8);
            graphBuilder.AddEdge('D', 'E', 6);
            graphBuilder.AddEdge('A', 'D', 5);
            graphBuilder.AddEdge('C', 'E', 2);
            graphBuilder.AddEdge('E', 'B', 3);
            graphBuilder.AddEdge('A', 'E', 7);

            return(graphBuilder.GetGraph());
        }
Ejemplo n.º 6
0
        public void AddEdge_NewSourceAndDestination_VerticesCreatedAndDirectedEdgeAdded()
        {
            var graphBuilder = new DirectedGraphBuilder <char>();
            var graph        = graphBuilder.GetGraph();

            var source      = 'A';
            var destination = 'B';
            var distance    = 5;

            graphBuilder.AddEdge(source, destination, distance);

            Assert.IsTrue(graph.HasVertex(source));
            Assert.IsTrue(graph.HasVertex(destination));

            Assert.IsTrue(graph.GetVertex(source).HasEdge(destination));
            Assert.IsFalse(graph.GetVertex(destination).HasEdge(source));
        }
Ejemplo n.º 7
0
        public void AddEdge_ExistingEdge_EdgeNotAdded()
        {
            var graphBuilder = new DirectedGraphBuilder <char>();
            var graph        = graphBuilder.GetGraph();

            var source      = 'A';
            var destination = 'B';
            var distance    = 5;

            var sourceVertex      = new Vertex <char>(source);
            var destinationVertex = new Vertex <char>(destination);

            sourceVertex.AddEdge(new Edge <char>(destinationVertex, distance));

            graph.Vertices.Add(sourceVertex);
            graph.Vertices.Add(destinationVertex);

            graphBuilder.AddEdge(source, destination, distance);

            Assert.AreEqual(1, sourceVertex.EdgesCount);
        }
Ejemplo n.º 8
0
        public void AddEdge_ExistingSourceAndDestination_VerticesNotCreatedAndDirectedEdgeAdded()
        {
            var graphBuilder = new DirectedGraphBuilder <char>();
            var graph        = graphBuilder.GetGraph();

            var source      = 'A';
            var destination = 'B';
            var distance    = 5;

            var sourceVertex      = new Vertex <char>(source);
            var destinationVertex = new Vertex <char>(destination);

            graph.Vertices.Add(sourceVertex);
            graph.Vertices.Add(destinationVertex);

            graphBuilder.AddEdge(source, destination, distance);

            Assert.AreEqual(2, graph.VerticesCount);
            Assert.IsTrue(sourceVertex.HasEdge(destination));
            Assert.IsFalse(destinationVertex.HasEdge(source));
        }