public void RetrievesAllEdgesInTheGraph() { EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = new EdgeListNode <int>(1); node1.AddEdgeTo(node1); EdgeListNode <int> node2 = new EdgeListNode <int>(2); EdgeListNode <int> node3 = new EdgeListNode <int>(3); node2.AddEdgeTo(node3); node2.AddEdgeTo(node2); graph.AddNode(node1); graph.AddNode(node2); List <EdgeListNode <int> > nodes = graph.AllNodes.ToList(); int count = nodes.Count; Assert.Equal(3, count); bool has1 = nodes.Contains(node1); Assert.True(has1); bool has2 = nodes.Contains(node2); Assert.True(has2); bool has3 = nodes.Contains(node3); Assert.True(has3); }
public void ProducesValidEdges() { //by value //correct reference passed EdgeListNode <int> node1 = new EdgeListNode <int>(1); Edge <EdgeListNode <int> > edge1 = node1.AddEdgeTo(1); Edge <EdgeListNode <int> > addResult1 = node1.AddEdgeTo(1); Assert.Equal(addResult1, edge1); //coherent 'from' and 'to' nodes EdgeListNode <int> node2 = new EdgeListNode <int>(2); Edge <EdgeListNode <int> > edge2 = node2.AddEdgeTo(1); EdgeListNode <int> from = edge2.From; EdgeListNode <int> to = edge2.To; Assert.Equal(from, node2); Assert.Equal(to.Value, 1); //by node //correct reference passed node1 = new EdgeListNode <int>(1); edge1 = node1.AddEdgeTo(node1); addResult1 = node1.AddEdgeTo(node1); Assert.Equal(addResult1, edge1); //coherent 'from' and 'to' nodes node2 = new EdgeListNode <int>(2); edge2 = node2.AddEdgeTo(node1); from = edge2.From; to = edge2.To; Assert.Equal(from, node2); Assert.Equal(to, node1); }
public void SortsCorrectlyGraphNodes() { EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = graph.AddNode(1); EdgeListNode <int> node2 = graph.AddNode(2); EdgeListNode <int> node3 = node2.AddEdgeTo(3).To; EdgeListNode <int> node4 = node2.AddEdgeTo(4).To; EdgeListNode <int> node5 = node4.AddEdgeTo(5).To; List <EdgeListNode <int> > result = graph.TopologicalSort().ToList(); int index1 = result.IndexOf(node1); int index2 = result.IndexOf(node2); int index3 = result.IndexOf(node3); int index4 = result.IndexOf(node4); int index5 = result.IndexOf(node5); Assert.True(index1 >= 0); Assert.True(index2 >= 0); Assert.True(index3 >= 0); Assert.True(index4 >= 0); Assert.True(index5 >= 0); Assert.InRange(index1, 0, 1); Assert.InRange(index2, 0, 1); Assert.True(index1 < index3); Assert.True(index1 < index4); Assert.True(index2 < index3); Assert.True(index2 < index4); Assert.True(index3 < index5); Assert.True(index4 < index5); }
public void RetrievesCorrectData() { EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = new EdgeListNode <int>(1); EdgeListNode <int> node2 = new EdgeListNode <int>(2); EdgeListNode <int> node3 = new EdgeListNode <int>(3); EdgeListNode <int> node4 = new EdgeListNode <int>(4); node1.AddEdgeTo(node1); node2.AddEdgeTo(node3); graph.AddNode(node1); graph.AddNode(node2); graph.AddNode(node4); List <EdgeListNode <int> > nodes = graph.NodesWithoutIncomingEdges.ToList(); int count = nodes.Count; Assert.Equal(2, count); bool has2 = nodes.Contains(node2); Assert.True(has2); bool has4 = nodes.Contains(node4); Assert.True(has4); }
public void ThrowsOnCyclicalGraphs() { EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = graph.AddNode(1); EdgeListNode <int> node2 = graph.AddNode(2); node1.AddEdgeTo(node1); node2.AddEdgeTo(3); Assert.Throws <Exception>(() => { graph.TopologicalSort(); }); }
public void AddsANewEdges() { //by value EdgeListNode <int> node = new EdgeListNode <int>(1); node.AddEdgeTo(1); bool added = node.HasEdgeTo(1); Assert.True(added); //by node node = new EdgeListNode <int>(1); node.AddEdgeTo(node); added = node.HasEdgeTo(1); Assert.True(added); }
public void DetectsNegativesCases() { //by value EdgeListNode <int> node = new EdgeListNode <int>(1); node.AddEdgeTo(2); bool hasEdgeToValue = node.HasEdgeTo(5); Assert.False(hasEdgeToValue); //by node node = new EdgeListNode <int>(1); EdgeListNode <int> neighbour = new EdgeListNode <int>(5); bool hasEdgeToNode = node.HasEdgeTo(neighbour); Assert.False(hasEdgeToNode); }
public void DoesNotRetrieveNonExistingEdges() { //by value EdgeListNode <int> node = new EdgeListNode <int>(1); node.AddEdgeTo(2); bool hasEdgeToValue = node.HasEdgeTo(5); Assert.False(hasEdgeToValue); //by node node = new EdgeListNode <int>(1); EdgeListNode <int> neighbour = new EdgeListNode <int>(5); bool hasEdgeToNode = node.HasEdgeTo(neighbour); Assert.False(hasEdgeToNode); }
public void DoesNotAddOldEdges() { //by value EdgeListNode <int> node = new EdgeListNode <int>(1); node.AddEdgeTo(1); node.AddEdgeTo(1); int count = node.Edges.Count(); Assert.Equal(1, count); //by node node = new EdgeListNode <int>(1); node.AddEdgeTo(node); node.AddEdgeTo(node); count = node.Edges.Count(); Assert.Equal(1, count); }
public void RetreivesExistingEdges() { //by value EdgeListNode <int> node = new EdgeListNode <int>(1); node.AddEdgeTo(2); Edge <EdgeListNode <int> > valueResult = node.FindEdgeTo(2); Assert.NotNull(valueResult); //by node node = new EdgeListNode <int>(1); EdgeListNode <int> neighbour = new EdgeListNode <int>(2); node.AddEdgeTo(neighbour); Edge <EdgeListNode <int> > nodeResult = node.FindEdgeTo(2); Assert.NotNull(nodeResult); }
public void DetectsPositiveCases() { //by value EdgeListNode <int> node = new EdgeListNode <int>(1); node.AddEdgeTo(2); bool hasEdgeToValue = node.HasEdgeTo(2); Assert.True(hasEdgeToValue); //by node node = new EdgeListNode <int>(1); EdgeListNode <int> neighbour = new EdgeListNode <int>(2); node.AddEdgeTo(neighbour); bool hasEdgeToNode = node.HasEdgeTo(neighbour); Assert.True(hasEdgeToNode); }
public void DoesNotAddOldNodes() { //by node EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = new EdgeListNode <int>(1); EdgeListNode <int> node2 = new EdgeListNode <int>(2); EdgeListNode <int> node3 = new EdgeListNode <int>(3); node2.AddEdgeTo(node3); graph.AddNode(node1); graph.AddNode(node1); graph.AddNode(node2); graph.AddNode(node2); List <EdgeListNode <int> > nodes = graph.Nodes.ToList(); int count = nodes.Count; Assert.Equal(2, count); bool has1 = nodes.Contains(node1); Assert.True(has1); bool has2 = nodes.Contains(node2); Assert.True(has2); //by value graph = new EdgeListGraph <int>(); node1 = graph.AddNode(1); node2 = graph.AddNode(2); node2.AddEdgeTo(node3); graph.AddNode(1); graph.AddNode(1); nodes = graph.Nodes.ToList(); count = nodes.Count; Assert.Equal(2, count); has1 = nodes.Contains(node1); Assert.True(has1); has2 = nodes.Contains(node2); Assert.True(has2); }
public void AddsNewEdges() { //by value EdgeListGraph <int> graph = new EdgeListGraph <int>(); graph.AddNode(1); graph.AddNode(2); graph.AddDirectedEdge(1, 2); int count = graph.AllEdges.Count(); Assert.Equal(1, count); //by node graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = new EdgeListNode <int>(1); EdgeListNode <int> node2 = new EdgeListNode <int>(2); graph.AddNode(node1); graph.AddNode(node2); graph.AddDirectedEdge(node1, node2); count = graph.AllEdges.Count(); Assert.Equal(1, count); }
public void FindsExistingNodes() { EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = new EdgeListNode <int>(1); EdgeListNode <int> node2 = new EdgeListNode <int>(2); EdgeListNode <int> node3 = new EdgeListNode <int>(3); node2.AddEdgeTo(node3); graph.AddNode(node1); graph.AddNode(node2); EdgeListNode <int> result1, result2, result3; //by value result1 = graph.FindNode(1); result2 = graph.FindNode(2); result3 = graph.FindNode(3); Assert.NotNull(result1); Assert.NotNull(result2); Assert.NotNull(result3); //by node result1 = graph.FindNode(node1); result2 = graph.FindNode(node2); result3 = graph.FindNode(node3); Assert.NotNull(result1); Assert.NotNull(result2); Assert.NotNull(result3); //by predicate result1 = graph.FindNode(node => node.Value < 2); result2 = graph.FindNode(node => Math.Pow(node.Value, 2) == 4); result3 = graph.FindNode(node => node.Value > 2); Assert.NotNull(result1); Assert.NotNull(result2); Assert.NotNull(result3); }
public void DetectsNonExistingNodes() { EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = new EdgeListNode <int>(1); EdgeListNode <int> node2 = new EdgeListNode <int>(2); EdgeListNode <int> node3 = new EdgeListNode <int>(3); node2.AddEdgeTo(node3); graph.AddNode(node1); graph.AddNode(node2); bool result; //by value result = graph.ContainsNode(12); Assert.False(result); //by node result = graph.ContainsNode(new EdgeListNode <int>(12)); Assert.False(result); //by predicate result = graph.ContainsNode(node => node.Value >= 12); Assert.False(result); }
public void DetectsExistingNodes() { EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = new EdgeListNode <int>(1); EdgeListNode <int> node2 = new EdgeListNode <int>(2); EdgeListNode <int> node3 = new EdgeListNode <int>(3); node2.AddEdgeTo(node3); graph.AddNode(node1); graph.AddNode(node2); bool result1, result2, result3; //by value result1 = graph.ContainsNode(1); result2 = graph.ContainsNode(2); result3 = graph.ContainsNode(3); Assert.True(result1); Assert.True(result2); Assert.True(result3); //by node result1 = graph.ContainsNode(node1); result2 = graph.ContainsNode(node2); result3 = graph.ContainsNode(node3); Assert.True(result1); Assert.True(result2); Assert.True(result3); //by predicate result1 = graph.ContainsNode(node => node.Value < 2); result2 = graph.ContainsNode(node => Math.Pow(node.Value, 2) == 4); result3 = graph.ContainsNode(node => node.Value > 2); Assert.True(result1); Assert.True(result2); Assert.True(result3); }
public void DoesNotFindNonExistingNodes() { EdgeListGraph <int> graph = new EdgeListGraph <int>(); EdgeListNode <int> node1 = new EdgeListNode <int>(1); EdgeListNode <int> node2 = new EdgeListNode <int>(2); EdgeListNode <int> node3 = new EdgeListNode <int>(3); node2.AddEdgeTo(node3); graph.AddNode(node1); graph.AddNode(node2); EdgeListNode <int> result; //by value result = graph.FindNode(12); Assert.Null(result); //by node result = graph.FindNode(new EdgeListNode <int>(12)); Assert.Null(result); //by predicate result = graph.FindNode(node => node.Value >= 12); Assert.Null(result); }