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); }
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); }
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."); } }
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)); }
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); }