protected static void AddVerticesAndEdge_Throws_Clusters_Test <TVertex, TEdge>( [NotNull] ClusteredAdjacencyGraph <TVertex, TEdge> graph) where TEdge : class, IEdge <TVertex> { // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.AddVerticesAndEdge(null)); AssertEmptyGraph(graph); }
public void ContainsEdge_EquatableEdge() { var wrappedGraph = new AdjacencyGraph <int, EquatableEdge <int> >(); var graph = new ClusteredAdjacencyGraph <int, EquatableEdge <int> >(wrappedGraph); ContainsEdge_EquatableEdge_Test( graph, edge => graph.AddVerticesAndEdge(edge)); }
public void GenerateSameDot() { var graph = new AdjacencyGraph <int, Edge <int> >(); // Empty graph TestGenerate(graph); // Only vertices graph.AddVertexRange(new[] { 1, 2 }); TestGenerate(graph); // With edges graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(2, 3), new Edge <int>(3, 1) }); TestGenerate(graph); // With no cluster var clusteredGraph = new ClusteredAdjacencyGraph <int, Edge <int> >(graph); TestGenerate(clusteredGraph); // With clusters ClusteredAdjacencyGraph <int, Edge <int> > subGraph1 = clusteredGraph.AddCluster(); subGraph1.AddVertexRange(new[] { 4, 5 }); ClusteredAdjacencyGraph <int, Edge <int> > subGraph2 = clusteredGraph.AddCluster(); subGraph2.AddVerticesAndEdge(new Edge <int>(1, 6)); TestGenerate(clusteredGraph); #region Local function void TestGenerate <TVertex, TEdge>(IEdgeListGraph <TVertex, TEdge> g) where TEdge : IEdge <TVertex> { var algorithm = new GraphvizAlgorithm <TVertex, TEdge>(g); string generatedDot = algorithm.Generate(); Assert.IsNotEmpty(generatedDot); var dotEngine = new TestDotEngine { ExpectedDot = generatedDot }; // ReSharper disable once ReturnValueOfPureMethodIsNotUsed algorithm.Generate(dotEngine, "NotSaved.dot"); } #endregion }
public void ToGraphvizWithInit() { var wrappedGraph = new AdjacencyGraph <int, Edge <int> >(); wrappedGraph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 4) }); wrappedGraph.AddVertex(5); var clusteredGraph = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph); ClusteredAdjacencyGraph <int, Edge <int> > subGraph1 = clusteredGraph.AddCluster(); subGraph1.AddVerticesAndEdgeRange(new[] { new Edge <int>(6, 7), new Edge <int>(7, 8) }); ClusteredAdjacencyGraph <int, Edge <int> > subGraph2 = clusteredGraph.AddCluster(); subGraph2.AddVerticesAndEdge(new Edge <int>(9, 10)); subGraph2.AddVertex(11); string expectedDot = "digraph G {" + Environment.NewLine + "node [shape=diamond];" + Environment.NewLine + "edge [tooltip=\"Test Edge\"];" + Environment.NewLine + "subgraph cluster1 {" + Environment.NewLine + "5 [label=\"Test Vertex 6\"];" + Environment.NewLine + "6 [label=\"Test Vertex 7\"];" + Environment.NewLine + "7 [label=\"Test Vertex 8\"];" + Environment.NewLine + "5 -> 6;" + Environment.NewLine + "6 -> 7;" + Environment.NewLine + "}" + Environment.NewLine + "subgraph cluster2 {" + Environment.NewLine + "8 [label=\"Test Vertex 9\"];" + Environment.NewLine + "9 [label=\"Test Vertex 10\"];" + Environment.NewLine + "10 [label=\"Test Vertex 11\"];" + Environment.NewLine + "8 -> 9;" + Environment.NewLine + "}" + Environment.NewLine + "0 [label=\"Test Vertex 1\"];" + Environment.NewLine + "1 [label=\"Test Vertex 2\"];" + Environment.NewLine + "2 [label=\"Test Vertex 3\"];" + Environment.NewLine + "3 [label=\"Test Vertex 4\"];" + Environment.NewLine + "4 [label=\"Test Vertex 5\"];" + Environment.NewLine + "0 -> 1;" + Environment.NewLine + "0 -> 2;" + Environment.NewLine + "1 -> 3;" + Environment.NewLine + "}"; string dotGraph = clusteredGraph.ToGraphviz(algorithm => { algorithm.CommonVertexFormat.Shape = GraphvizVertexShape.Diamond; algorithm.CommonEdgeFormat.ToolTip = "Test Edge"; algorithm.FormatVertex += (sender, args) => { args.VertexFormat.Label = $"Test Vertex {args.Vertex}"; }; }); Assert.AreEqual(expectedDot, dotGraph); }
protected static void AddVerticesAndEdge_Clusters_Test( [NotNull] ClusteredAdjacencyGraph <int, Edge <int> > graph1, [NotNull] ClusteredAdjacencyGraph <int, Edge <int> > parent2, [NotNull] ClusteredAdjacencyGraph <int, Edge <int> > graph2) { // Graph without parent AssertEmptyGraph(graph1); // Edge 1 var edge1 = new Edge <int>(1, 2); Assert.IsTrue(graph1.AddVerticesAndEdge(edge1)); AssertHasVertices(graph1, new[] { 1, 2 }); AssertHasEdges(graph1, new[] { edge1 }); // Edge 2 var edge2 = new Edge <int>(1, 3); Assert.IsTrue(graph1.AddVerticesAndEdge(edge2)); AssertHasVertices(graph1, new[] { 1, 2, 3 }); AssertHasEdges(graph1, new[] { edge1, edge2 }); // Edge 3 var edge3 = new Edge <int>(2, 3); Assert.IsTrue(graph1.AddVerticesAndEdge(edge3)); AssertHasVertices(graph1, new[] { 1, 2, 3 }); AssertHasEdges(graph1, new[] { edge1, edge2, edge3 }); // Graph with parent AssertEmptyGraph(parent2); AssertEmptyGraph(graph2); // Edge 1 Assert.IsTrue(graph2.AddVerticesAndEdge(edge1)); AssertHasVertices(parent2, new[] { 1, 2 }); AssertHasVertices(graph2, new[] { 1, 2 }); AssertHasEdges(parent2, new[] { edge1 }); AssertHasEdges(graph2, new[] { edge1 }); // Edge 2 Assert.IsTrue(parent2.AddVerticesAndEdge(edge2)); AssertHasVertices(parent2, new[] { 1, 2, 3 }); AssertHasVertices(graph2, new[] { 1, 2 }); AssertHasEdges(parent2, new[] { edge1, edge2 }); AssertHasEdges(graph2, new[] { edge1 }); Assert.IsTrue(graph2.AddVerticesAndEdge(edge2)); AssertHasVertices(parent2, new[] { 1, 2, 3 }); AssertHasVertices(graph2, new[] { 1, 2, 3 }); AssertHasEdges(parent2, new[] { edge1, edge2 }); AssertHasEdges(graph2, new[] { edge1, edge2 }); // Edge 3 Assert.IsTrue(graph2.AddVerticesAndEdge(edge3)); AssertHasVertices(parent2, new[] { 1, 2, 3 }); AssertHasVertices(graph2, new[] { 1, 2, 3 }); AssertHasEdges(parent2, new[] { edge1, edge2, edge3 }); AssertHasEdges(graph2, new[] { edge1, edge2, edge3 }); }
protected static void RemoveEdge_Clusters_Test( [NotNull] ClusteredAdjacencyGraph <int, Edge <int> > graph) { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge13Bis = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge24 = new Edge <int>(2, 4); var edge31 = new Edge <int>(3, 1); var edge33 = new Edge <int>(3, 3); var edgeNotInGraph = new Edge <int>(3, 4); var edgeWithVertexNotInGraph1 = new Edge <int>(2, 10); var edgeWithVertexNotInGraph2 = new Edge <int>(10, 2); var edgeWithVerticesNotInGraph = new Edge <int>(10, 11); var edgeNotEquatable = new Edge <int>(1, 2); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge13Bis, edge14, edge24, edge31, edge33 }); Assert.IsFalse(graph.RemoveEdge(edgeNotInGraph)); Assert.IsFalse(graph.RemoveEdge(edgeWithVertexNotInGraph1)); Assert.IsFalse(graph.RemoveEdge(edgeWithVertexNotInGraph2)); Assert.IsFalse(graph.RemoveEdge(edgeWithVerticesNotInGraph)); Assert.IsFalse(graph.RemoveEdge(edgeNotEquatable)); Assert.IsTrue(graph.RemoveEdge(edge13Bis)); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge12, edge13, edge14, edge24, edge31, edge33 }); Assert.IsTrue(graph.RemoveEdge(edge31)); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge12, edge13, edge14, edge24, edge33 }); Assert.IsTrue(graph.RemoveEdge(edge12)); Assert.IsTrue(graph.RemoveEdge(edge13)); Assert.IsTrue(graph.RemoveEdge(edge14)); Assert.IsTrue(graph.RemoveEdge(edge24)); Assert.IsTrue(graph.RemoveEdge(edge33)); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertNoEdge(graph); // With cluster graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31 }); AssertHasEdges(graph, new[] { edge12, edge13, edge14, edge24, edge31 }); ClusteredAdjacencyGraph <int, Edge <int> > cluster1 = graph.AddCluster(); ClusteredAdjacencyGraph <int, Edge <int> > cluster2 = graph.AddCluster(); ClusteredAdjacencyGraph <int, Edge <int> > cluster3 = graph.AddCluster(); cluster1.AddVerticesAndEdgeRange(new[] { edge12, edge13 }); AssertHasEdges(cluster1, new[] { edge12, edge13 }); cluster2.AddVerticesAndEdgeRange(new[] { edge12, edge14, edge24 }); AssertHasEdges(cluster2, new[] { edge12, edge14, edge24 }); cluster3.AddVerticesAndEdge(edge12); AssertHasEdges(cluster3, new[] { edge12 }); graph.RemoveEdge(edge12); AssertHasEdges(graph, new[] { edge13, edge14, edge24, edge31 }); AssertHasEdges(cluster1, new[] { edge13 }); AssertHasEdges(cluster2, new[] { edge14, edge24 }); AssertNoEdge(cluster3); graph.RemoveEdge(edge13); AssertHasEdges(graph, new[] { edge14, edge24, edge31 }); AssertNoEdge(cluster1); AssertHasEdges(cluster2, new[] { edge14, edge24 }); AssertNoEdge(cluster3); graph.RemoveEdge(edge24); AssertHasEdges(graph, new[] { edge14, edge31 }); AssertNoEdge(cluster1); AssertHasEdges(cluster2, new[] { edge14 }); AssertNoEdge(cluster3); graph.RemoveEdge(edge14); AssertHasEdges(graph, new[] { edge31 }); AssertNoEdge(cluster1); AssertNoEdge(cluster2); AssertNoEdge(cluster3); }
public void FormatHandlers() { var graph = new AdjacencyGraph <int, Edge <int> >(); var algorithm = new GraphvizAlgorithm <int, Edge <int> >(graph); algorithm.FormatVertex += NoVertexOnFormatVertex; algorithm.FormatEdge += NoEdgeOnFormatEdge; algorithm.FormatCluster += NoClusterOnFormatCluster; // Empty graph algorithm.Generate(); // Only vertices graph.AddVertexRange(new[] { 1, 2 }); algorithm = new GraphvizAlgorithm <int, Edge <int> >(graph); List <int> notFormattedVertices = RegisterOnFormatVertex(algorithm, graph.Vertices); algorithm.FormatEdge += NoEdgeOnFormatEdge; algorithm.FormatCluster += NoClusterOnFormatCluster; algorithm.Generate(); CollectionAssert.IsEmpty(notFormattedVertices); // With edges graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(2, 3), new Edge <int>(3, 1) }); algorithm = new GraphvizAlgorithm <int, Edge <int> >(graph); notFormattedVertices = RegisterOnFormatVertex(algorithm, graph.Vertices); List <Edge <int> > notFormattedEdges = RegisterOnFormatEdge(algorithm, graph.Edges); algorithm.FormatCluster += NoClusterOnFormatCluster; algorithm.Generate(); CollectionAssert.IsEmpty(notFormattedVertices); CollectionAssert.IsEmpty(notFormattedEdges); // With no cluster var clusteredGraph = new ClusteredAdjacencyGraph <int, Edge <int> >(graph); algorithm = new GraphvizAlgorithm <int, Edge <int> >(clusteredGraph); notFormattedVertices = RegisterOnFormatVertex(algorithm, clusteredGraph.Vertices); notFormattedEdges = RegisterOnFormatEdge(algorithm, clusteredGraph.Edges); algorithm.FormatCluster += NoClusterOnFormatCluster; algorithm.Generate(); CollectionAssert.IsEmpty(notFormattedVertices); CollectionAssert.IsEmpty(notFormattedEdges); // With clusters ClusteredAdjacencyGraph <int, Edge <int> > subGraph1 = clusteredGraph.AddCluster(); subGraph1.AddVertexRange(new[] { 4, 5 }); ClusteredAdjacencyGraph <int, Edge <int> > subGraph2 = clusteredGraph.AddCluster(); subGraph2.AddVerticesAndEdge(new Edge <int>(1, 6)); algorithm = new GraphvizAlgorithm <int, Edge <int> >(clusteredGraph); notFormattedVertices = RegisterOnFormatVertex(algorithm, clusteredGraph.Vertices); notFormattedEdges = RegisterOnFormatEdge(algorithm, clusteredGraph.Edges); List <IVertexAndEdgeListGraph <int, Edge <int> > > notFormattedClusters = RegisterOnFormatCluster( algorithm, new[] { subGraph1, subGraph2 }); algorithm.Generate(); CollectionAssert.IsEmpty(notFormattedVertices); CollectionAssert.IsEmpty(notFormattedEdges); CollectionAssert.IsEmpty(notFormattedClusters); #region Local functions void NoVertexOnFormatVertex(object sender, FormatVertexEventArgs <int> args) { Assert.Fail($"{nameof(GraphvizAlgorithm<object, Edge<object>>.FormatVertex)} called while no vertex in graph."); } List <TVertex> RegisterOnFormatVertex <TVertex, TEdge>(GraphvizAlgorithm <TVertex, TEdge> algo, IEnumerable <TVertex> vertices) where TEdge : IEdge <TVertex> { var verticesList = new List <TVertex>(vertices); algo.FormatVertex += (sender, args) => { Assert.IsTrue(verticesList.Remove(args.Vertex)); }; return(verticesList); } void NoEdgeOnFormatEdge(object sender, FormatEdgeEventArgs <int, Edge <int> > args) { Assert.Fail($"{nameof(GraphvizAlgorithm<object, Edge<object>>.FormatEdge)} called while no edge in graph."); } List <TEdge> RegisterOnFormatEdge <TVertex, TEdge>(GraphvizAlgorithm <TVertex, TEdge> algo, IEnumerable <TEdge> edges) where TEdge : IEdge <TVertex> { var edgeList = new List <TEdge>(edges); algo.FormatEdge += (sender, args) => { Assert.IsTrue(edgeList.Remove(args.Edge)); }; return(edgeList); } void NoClusterOnFormatCluster(object sender, FormatClusterEventArgs <int, Edge <int> > args) { Assert.Fail($"{nameof(GraphvizAlgorithm<object, Edge<object>>.FormatCluster)} called while no cluster in graph."); } List <IVertexAndEdgeListGraph <TVertex, TEdge> > RegisterOnFormatCluster <TVertex, TEdge>(GraphvizAlgorithm <TVertex, TEdge> algo, IEnumerable <IVertexAndEdgeListGraph <TVertex, TEdge> > clusters) where TEdge : IEdge <TVertex> { var clusterList = new List <IVertexAndEdgeListGraph <TVertex, TEdge> >(clusters); algo.FormatCluster += (sender, args) => { Assert.IsTrue(clusterList.Remove(args.Cluster)); }; return(clusterList); } #endregion }