public void TryGetOutEdges_Throws()
        {
            var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var graph        = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph);

            TryGetOutEdges_Throws_Test(graph);
        }
Пример #2
0
 public bool ContainsVertexParent([NotNull] ClusteredAdjacencyGraph <int, IEdge <int> > cluster, int vertex)
 {
     return(cluster.ContainsVertex(vertex) &&
            cluster.Parent != null &&
            ContainsVertexParent(cluster.Parent, vertex) ||
            cluster.Parent is null);
 }
        public void ClearOutEdges()
        {
            var wrappedGraph1 = new AdjacencyGraph <int, Edge <int> >();
            var graph1        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph1);

            ClearOutEdgesTest(graph1);

            var wrappedGraph2 = new AdjacencyGraph <int, Edge <int> >();
            var graph2        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph2);
            var subGraph2     = new ClusteredAdjacencyGraph <int, Edge <int> >(graph2);

            ClearOutEdgesTest(subGraph2);

            #region Local function

            void ClearOutEdgesTest(ClusteredAdjacencyGraph <int, Edge <int> > g)
            {
                AssertEmptyGraph(g);

                // Clear 1 => not in graph
                g.ClearOutEdges(1);
                AssertEmptyGraph(g);

                // Clear 1 => In graph but no out edges
                g.AddVertex(1);
                g.ClearOutEdges(1);
                AssertHasVertices(g, new[] { 1 });
                AssertNoEdge(g);

                var edge12 = new Edge <int>(1, 2);
                var edge23 = new Edge <int>(2, 3);

                g.AddVerticesAndEdgeRange(new[] { edge12, edge23 });

                // Clear 1
                g.ClearOutEdges(1);
                AssertHasEdges(g, new[] { edge23 });

                var edge13 = new Edge <int>(1, 3);
                var edge31 = new Edge <int>(3, 1);
                var edge32 = new Edge <int>(3, 2);

                g.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge31, edge32 });

                // Clear 3
                g.ClearOutEdges(3);
                AssertHasEdges(g, new[] { edge12, edge13, edge23 });

                // Clear 1
                g.ClearOutEdges(1);
                AssertHasEdges(g, new[] { edge23 });

                // Clear 2 = Clear
                g.ClearOutEdges(2);

                AssertNoEdge(g);
            }

            #endregion
        }
        public void RemoveOutEdgeIf_Throws()
        {
            var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var graph        = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph);

            RemoveOutEdgeIf_Throws_Test(graph);
        }
        public void Clear()
        {
            var wrappedGraph1 = new AdjacencyGraph <int, Edge <int> >();
            var graph1        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph1);

            ClearGraphTest(graph1);

            var wrappedGraph2 = new AdjacencyGraph <int, Edge <int> >();
            var graph2        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph2);
            var subGraph2     = new ClusteredAdjacencyGraph <int, Edge <int> >(graph2);

            ClearGraphTest(subGraph2);

            #region Local function

            void ClearGraphTest(ClusteredAdjacencyGraph <int, Edge <int> > g)
            {
                AssertEmptyGraph(g);

                g.Clear();
                AssertEmptyGraph(g);

                g.AddVerticesAndEdge(new Edge <int>(1, 2));
                g.AddVerticesAndEdge(new Edge <int>(2, 3));
                g.AddVerticesAndEdge(new Edge <int>(3, 1));

                g.Clear();
                AssertEmptyGraph(g);
            }

            #endregion
        }
 protected static void RemoveEdgeIf_Throws_Clusters_Test <TVertex, TEdge>(
     [NotNull] ClusteredAdjacencyGraph <TVertex, TEdge> graph)
     where TEdge : IEdge <TVertex>
 {
     // ReSharper disable once AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(() => graph.RemoveEdgeIf(null));
 }
        protected static void RemoveOutEdgeIf_Clusters_Test(
            [NotNull] ClusteredAdjacencyGraph <int, Edge <int> > graph)
        {
            Assert.AreEqual(0, graph.RemoveOutEdgeIf(1, edge => true));
            AssertEmptyGraph(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);

            graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge13Bis, edge14, edge24, edge31, edge33 });

            Assert.AreEqual(3, graph.RemoveOutEdgeIf(1, edge => edge.Target >= 3));
            AssertHasVertices(graph, new[] { 1, 2, 3, 4 });
            AssertHasEdges(graph, new[] { edge12, edge24, edge31, edge33 });

            Assert.AreEqual(0, graph.RemoveOutEdgeIf(3, edge => edge.Target > 5));
            AssertHasVertices(graph, new[] { 1, 2, 3, 4 });
            AssertHasEdges(graph, new[] { edge12, edge24, edge31, edge33 });

            Assert.AreEqual(2, graph.RemoveOutEdgeIf(3, edge => true));
            AssertHasVertices(graph, new[] { 1, 2, 3, 4 });
            AssertHasEdges(graph, new[] { edge12, edge24 });
        }
        public void RemoveCluster()
        {
            var wrappedGraph = new AdjacencyGraph <int, Edge <int> >();
            var graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph);

            AssertNoCluster(graph);

            IClusteredGraph cluster            = graph.AddCluster();
            IClusteredGraph cluster2           = graph.AddCluster();
            IClusteredGraph cluster3           = graph.AddCluster();
            var             wrappedGraph2      = new AdjacencyGraph <int, Edge <int> >();
            var             graphNotInClusters = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph2);

            graph.RemoveCluster(graphNotInClusters);
            AssertHasClusters(graph, new[] { cluster, cluster2, cluster3 });

            graph.RemoveCluster(cluster2);
            AssertHasClusters(graph, new[] { cluster, cluster3 });

            graph.RemoveCluster(cluster);
            AssertHasClusters(graph, new[] { cluster3 });

            graph.RemoveCluster(cluster3);
            AssertNoCluster(graph);
        }
Пример #9
0
        public void BinarySerialization_ClusteredGraph([NotNull] ClusteredAdjacencyGraph <int, EquatableEdge <int> > graph)
        {
            ClusteredAdjacencyGraph <int, EquatableEdge <int> > deserializedGraph =
                SerializeDeserialize <int, EquatableEdge <int>, ClusteredAdjacencyGraph <int, EquatableEdge <int> > >(graph);

            Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph));
        }
Пример #10
0
 public bool ContainsEdgeParent([NotNull] ClusteredAdjacencyGraph <int, IEdge <int> > cluster, IEdge <int> edge)
 {
     return(cluster.ContainsEdge(edge) &&
            cluster.Parent != null &&
            ContainsEdgeParent(cluster.Parent, edge) ||
            cluster.Parent is null);
 }
        public void ContainsVertex_Throws()
        {
            var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var graph        = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph);

            ContainsVertex_Throws_Test(graph);
        }
Пример #12
0
        public void BinarySerialization_ClusteredGraph_Complex([NotNull] ClusteredAdjacencyGraph <EquatableTestVertex, EquatableTestEdge> graph)
        {
            ClusteredAdjacencyGraph <EquatableTestVertex, EquatableTestEdge> deserializedGraph =
                SerializeDeserialize <EquatableTestVertex, EquatableTestEdge, ClusteredAdjacencyGraph <EquatableTestVertex, EquatableTestEdge> >(graph);

            Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph));
        }
Пример #13
0
 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_Throws()
        {
            var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var graph        = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph);

            ContainsEdge_NullThrows_Test(graph);
            ContainsEdge_SourceTarget_Throws_Test(graph);
        }
        public void ClearOutEdges_Throws()
        {
            var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var graph        = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph);

            // ReSharper disable once AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => graph.ClearOutEdges(null));
        }
        public void RemoveCluster_Throws()
        {
            var wrappedGraph = new AdjacencyGraph <int, Edge <int> >();
            var graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph);

            // ReSharper disable once AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => graph.RemoveCluster(null));
        }
        protected static void AddVertex_Clusters_Test <TEdge>(
            ClusteredAdjacencyGraph <TestVertex, TEdge> graph1,
            ClusteredAdjacencyGraph <TestVertex, TEdge> parent2,
            ClusteredAdjacencyGraph <TestVertex, TEdge> graph2)
            where TEdge : IEdge <TestVertex>
        {
            AssertNoVertex(graph1);

            // Graph without parent
            // Vertex 1
            var vertex1 = new TestVertex("1");

            Assert.IsTrue(graph1.AddVertex(vertex1));
            AssertHasVertices(graph1, new[] { vertex1 });

            // Vertex 2
            var vertex2 = new TestVertex("2");

            Assert.IsTrue(graph1.AddVertex(vertex2));
            AssertHasVertices(graph1, new[] { vertex1, vertex2 });

            // Vertex 1 bis
            Assert.IsFalse(graph1.AddVertex(vertex1));
            AssertHasVertices(graph1, new[] { vertex1, vertex2 });

            // Other "Vertex 1"
            var otherVertex1 = new TestVertex("1");

            Assert.IsTrue(graph1.AddVertex(otherVertex1));
            AssertHasVertices(graph1, new[] { vertex1, vertex2, otherVertex1 });

            // Graph with parent
            AssertNoVertex(parent2);
            AssertNoVertex(graph2);

            Assert.IsTrue(graph2.AddVertex(vertex1));
            AssertHasVertices(parent2, new[] { vertex1 });
            AssertHasVertices(graph2, new[] { vertex1 });

            // Vertex 2
            Assert.IsTrue(parent2.AddVertex(vertex2));
            AssertHasVertices(parent2, new[] { vertex1, vertex2 });
            AssertHasVertices(graph2, new[] { vertex1 });

            Assert.IsTrue(graph2.AddVertex(vertex2));
            AssertHasVertices(parent2, new[] { vertex1, vertex2 });
            AssertHasVertices(graph2, new[] { vertex1, vertex2 });

            // Vertex 1 bis
            Assert.IsFalse(graph2.AddVertex(vertex1));
            AssertHasVertices(parent2, new[] { vertex1, vertex2 });
            AssertHasVertices(graph2, new[] { vertex1, vertex2 });

            // Other "Vertex 1"
            Assert.IsTrue(graph2.AddVertex(otherVertex1));
            AssertHasVertices(parent2, new[] { vertex1, vertex2, otherVertex1 });
            AssertHasVertices(graph2, new[] { vertex1, vertex2, otherVertex1 });
        }
 protected static void AddVertex_Throws_Clusters_Test <TVertex, TEdge>(
     ClusteredAdjacencyGraph <TVertex, TEdge> graph)
     where TVertex : class
     where TEdge : IEdge <TVertex>
 {
     // ReSharper disable once AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(() => graph.AddVertex(null));
     AssertNoVertex(graph);
 }
        public void Construction()
        {
            var wrappedGraph = new AdjacencyGraph <int, Edge <int> >(true);
            var graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph);

            AssertGraphProperties(graph);

            wrappedGraph = new AdjacencyGraph <int, Edge <int> >(false);
            graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph);
            AssertGraphProperties(graph, false);

            wrappedGraph = new AdjacencyGraph <int, Edge <int> >(true, 12, 25);
            graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph);
            AssertGraphProperties(graph, edgeCapacity: 25);

            wrappedGraph = new AdjacencyGraph <int, Edge <int> >(false, 12, 52);
            graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph);
            AssertGraphProperties(graph, false, 52);

            wrappedGraph = new AdjacencyGraph <int, Edge <int> >(false, 12);
            graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph)
            {
                EdgeCapacity = 25
            };
            AssertGraphProperties(graph, false, 25);

            var subGraph = new ClusteredAdjacencyGraph <int, Edge <int> >(graph);

            AssertGraphProperties(subGraph, false, parent: graph);

            #region Local function

            void AssertGraphProperties <TVertex, TEdge>(
                ClusteredAdjacencyGraph <TVertex, TEdge> g,
                bool parallelEdges = true,
                int edgeCapacity   = 0,
                ClusteredAdjacencyGraph <int, Edge <int> > parent = null)
                where TEdge : IEdge <TVertex>
            {
                Assert.IsTrue(g.IsDirected);
                Assert.AreEqual(parallelEdges, g.AllowParallelEdges);
                AssertEmptyGraph(g);
                Assert.AreEqual(edgeCapacity, g.EdgeCapacity);
                Assert.AreSame(typeof(int), g.VertexType);
                Assert.AreSame(typeof(Edge <int>), g.EdgeType);
                if (parent is null)
                {
                    Assert.IsNull(g.Parent);
                }
                else
                {
                    Assert.AreSame(parent, g.Parent);
                }
            }

            #endregion
        }
        public void TryGetOutEdges()
        {
            var wrappedGraph = new AdjacencyGraph <int, Edge <int> >();
            var graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph);

            TryGetOutEdges_Test(
                graph,
                edges => graph.AddVerticesAndEdgeRange(edges));
        }
        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));
        }
Пример #22
0
        public void AddingClusterVertex()
        {
            var graph          = new AdjacencyGraph <int, IEdge <int> >();
            var clusteredGraph = new ClusteredAdjacencyGraph <int, IEdge <int> >(graph);
            ClusteredAdjacencyGraph <int, IEdge <int> > cluster = clusteredGraph.AddCluster();

            cluster.AddVertex(5);

            Assert.IsTrue(ContainsVertexParent(clusteredGraph, 5));
        }
Пример #23
0
        public void AddingClustVertexTest1()
        {
            var graph          = new AdjacencyGraph <int, IEdge <int> >();
            var clusteredGraph = new ClusteredAdjacencyGraph <int, IEdge <int> >(graph);
            var cluster1       = clusteredGraph.AddCluster();

            cluster1.AddVertex(5);
            var a = ContainsVertexParent(clusteredGraph, 5);

            Assert.IsTrue(a);
        }
Пример #24
0
        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 AddVertexRange_Throws()
        {
            var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var graph        = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph);

            AddVertexRange_Throws_Clusters_Test(graph);

            var subGraph = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(graph);

            AddVertexRange_Throws_Clusters_Test(subGraph);
        }
        public void AddVerticesAndEdgeRange_Throws()
        {
            var wrappedGraph = new AdjacencyGraph <int, Edge <int> >();
            var graph        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph);

            AddVerticesAndEdgeRange_Throws_Clusters_Test(graph);

            var subGraph = new ClusteredAdjacencyGraph <int, Edge <int> >(graph);

            AddVerticesAndEdgeRange_Throws_Clusters_Test(subGraph);
        }
Пример #27
0
        public void RemovingClustVertexTest1()
        {
            var graph          = new AdjacencyGraph <int, IEdge <int> >();
            var clusteredGraph = new ClusteredAdjacencyGraph <int, IEdge <int> >(graph);
            var cluster1       = clusteredGraph.AddCluster();
            var cluster2       = cluster1.AddCluster();
            var cluster3       = cluster2.AddCluster();

            cluster3.AddVertex(5);
            cluster2.RemoveVertex(5);
            Assert.IsFalse(ContainsVertexParent(cluster3, 5));
        }
        public void OutEdges_Throws()
        {
            var wrappedGraph1 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var graph1        = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph1);

            OutEdges_NullThrows_Test(graph1);

            var wrappedGraph2 = new AdjacencyGraph <EquatableTestVertex, Edge <EquatableTestVertex> >();
            var graph2        = new ClusteredAdjacencyGraph <EquatableTestVertex, Edge <EquatableTestVertex> >(wrappedGraph2);

            OutEdges_Throws_Test(graph2);
        }
        public void RemoveOutEdgeIf()
        {
            var wrappedGraph1 = new AdjacencyGraph <int, Edge <int> >();
            var graph1        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph1);

            RemoveOutEdgeIf_Clusters_Test(graph1);

            var wrappedGraph2 = new AdjacencyGraph <int, Edge <int> >();
            var graph2        = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph2);
            var subGraph2     = new ClusteredAdjacencyGraph <int, Edge <int> >(graph2);

            RemoveOutEdgeIf_Clusters_Test(subGraph2);
        }
        public void RemoveEdge_EquatableEdge()
        {
            var wrappedGraph1 = new AdjacencyGraph <int, EquatableEdge <int> >();
            var graph1        = new ClusteredAdjacencyGraph <int, EquatableEdge <int> >(wrappedGraph1);

            RemoveEdge_EquatableEdge_Clusters_Test(graph1);

            var wrappedGraph2 = new AdjacencyGraph <int, EquatableEdge <int> >();
            var graph2        = new ClusteredAdjacencyGraph <int, EquatableEdge <int> >(wrappedGraph2);
            var subGraph2     = new ClusteredAdjacencyGraph <int, EquatableEdge <int> >(graph2);

            RemoveEdge_EquatableEdge_Clusters_Test(subGraph2);
        }