Beispiel #1
0
        public void GivenSingleStepPathsWhenFindShortestPathsThenReturnSimple()
        {
            // arrange
            var start = new SimpleNode("START");
            var a     = new SimpleNode("A");
            var b     = new SimpleNode("B");
            var c     = new SimpleNode("C");

            var arcs = new IArcWithLength <SimpleNode>[]
            {
                new SimpleArcWithLength <SimpleNode>(start, a, 1),
                new SimpleArcWithLength <SimpleNode>(start, b, 2),
                new SimpleArcWithLength <SimpleNode>(start, c, 3),
            };

            // act
            var foundPaths = arcs.FindShortestPaths(start);

            // assert
            Assert.AreEqual(3, foundPaths.Count);
            Assert.IsTrue(foundPaths.All(p => p.From == start && p.Path.Count == 1));
            Assert.AreEqual(1, foundPaths.First(p => p.To == a).Lenght);
            Assert.AreEqual(2, foundPaths.First(p => p.To == b).Lenght);
            Assert.AreEqual(3, foundPaths.First(p => p.To == c).Lenght);
        }
Beispiel #2
0
        public void GivenCyclesWhenFindShortestPathsThenWorksFine()
        {
            // arrange
            var start = new SimpleNode("START");
            var a     = new SimpleNode("A");
            var b     = new SimpleNode("B");
            var c     = new SimpleNode("C");

            var arcs = new IArcWithLength <SimpleNode>[]
            {
                new SimpleArcWithLength <SimpleNode>(start, a, 1),
                new SimpleArcWithLength <SimpleNode>(start, b, 10),
                new SimpleArcWithLength <SimpleNode>(start, c, 10),
                new SimpleArcWithLength <SimpleNode>(a, b, 1),
                new SimpleArcWithLength <SimpleNode>(b, c, 1),
            };

            // act
            var foundPaths = arcs.FindShortestPaths(start);

            // assert
            Assert.AreEqual(3, foundPaths.Count);
            Assert.AreEqual(1, foundPaths.First(p => p.To == a).Lenght);
            Assert.AreEqual(2, foundPaths.First(p => p.To == b).Lenght);
            Assert.AreEqual(3, foundPaths.First(p => p.To == c).Lenght);
        }
Beispiel #3
0
 public SimplePathWithLenght(SimplePathWithLenght <NodeT> path, IArcWithLength <NodeT> arc)
     : this(path.From, arc.To, new List <IArcWithLength <NodeT> >(path.Path) { arc }, path.Lenght + arc.Lenght)
 {
     if (path.To != arc.From)
     {
         throw new InvalidOperationException("To path node has to be the same as arcs From.");
     }
 }
Beispiel #4
0
        public void GivenNegaativeLenghtArcWhenTryToFindShortestPathsThenThrowError()
        {
            // arrange
            var start = new SimpleNode("START");
            var a     = new SimpleNode("A");

            var arcs = new IArcWithLength <SimpleNode>[]
            {
                new SimpleArcWithLength <SimpleNode>(start, a, -1),
            };

            // act & assert
            Assert.Throws <InvalidOperationException>(() => arcs.FindShortestPaths(start));
        }
Beispiel #5
0
        public void GivenNoArcsFromStartWhenFindShortestPathsThenReturnEmpty()
        {
            // arrange
            var start = new SimpleNode("START");
            var a     = new SimpleNode("A");
            var b     = new SimpleNode("B");

            var arcs = new IArcWithLength <SimpleNode>[]
            {
                new SimpleArcWithLength <SimpleNode>(a, b, 10),
            };

            // act
            var foundPaths = arcs.FindShortestPaths(start);

            // assert
            Assert.AreEqual(0, foundPaths.Count);
        }