Ejemplo n.º 1
0
        public RouteService(RouteContext context)
        {
            _context = context;

            var segments = context.Segments.Include(s => s.Locations);

            _locations = segments.SelectMany(s => s.Locations);

            _pathUtil = new ShortestPathUtil(segments, _locations);
        }
        public void WhenFastestIsSearchedBD_ThenCorrectPathIsFound()
        {
            // Arrange
            var segments  = MockSegments();
            var locations = Locations();


            var sut = new ShortestPathUtil(segments, locations);

            var start = locations.Single(l => l.CityName == "B");
            var end   = locations.Single(l => l.CityName == "D");

            // Act
            var path = sut.FindFastest(start, end);

            // Assert
            Assert.IsTrue(path.Origin.Id == "B");
            Assert.IsTrue(path.Destination.Id == "D");
            var time = path.Segments.Select(s => s.Weight).Aggregate((s, w) => s + w);

            var route = new List <string>();

            foreach (var node in path.Segments)
            {
                if (!route.Contains(node.Origin.Id))
                {
                    route.Add(node.Origin.Id);
                }
                if (!route.Contains(node.Destination.Id))
                {
                    route.Add(node.Destination.Id);
                }
            }

            var stringRoute = string.Join(",", route);

            Assert.IsTrue(stringRoute == "B,C,D");
            Assert.IsTrue(time == 3.0);
        }