Beispiel #1
0
        public void EdgeListGraphTestAddStuff()
        {
            var g = new EdgeListGraph <int, IEdge <int> >();

            g.AddEdge(new Edge <int>(0, 1));
            Assert.AreEqual(2, g.VertexCount);
            Assert.AreEqual(1, g.EdgeCount);
            g.AddEdge(new Edge <int>(0, 1));
            Assert.AreEqual(2, g.VertexCount);
            Assert.AreEqual(2, g.EdgeCount);
            g.AddEdge(new EquatableEdge <int>(0, 2));
            Assert.AreEqual(3, g.VertexCount);
            Assert.AreEqual(3, g.EdgeCount);
            g.AddVerticesAndEdge(new Edge <int>(3, 4));
            Assert.AreEqual(5, g.VertexCount);
            Assert.AreEqual(4, g.EdgeCount);
            g.AddEdgeRange(new List <IEdge <int> > {
                new Edge <int>(4, 5)
            });
            Assert.AreEqual(6, g.VertexCount);
            Assert.AreEqual(5, g.EdgeCount);
            g.AddVerticesAndEdgeRange(new List <IEdge <int> > {
                new Edge <int>(4, 5)
            });
            Assert.AreEqual(6, g.VertexCount);
            Assert.AreEqual(6, g.EdgeCount);
        }
Beispiel #2
0
        protected static void RemoveEdgeIf_EdgesOnly_Test(
            [NotNull] EdgeListGraph <int, Edge <int> > graph)
        {
            int edgesRemoved = 0;

            // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
            graph.EdgeRemoved += e =>
            {
                Assert.IsNotNull(e);
                // ReSharper disable once AccessToModifiedClosure
                ++edgesRemoved;
            };

            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.AddEdgeRange(new[] { edge12, edge13, edge13Bis, edge14, edge24, edge31, edge33 });

            Assert.AreEqual(0, graph.RemoveEdgeIf(edge => edge.Target == 5));
            CheckCounter(0);

            Assert.AreEqual(2, graph.RemoveEdgeIf(edge => edge.Source == 3));
            CheckCounter(2);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4 });
            AssertHasEdges(graph, new[] { edge12, edge13, edge13Bis, edge14, edge24 });

            Assert.AreEqual(5, graph.RemoveEdgeIf(_ => true));
            CheckCounter(5);
            AssertEmptyGraph(graph);    // Vertices removed in the same time as edges

            #region Local function

            void CheckCounter(int expectedRemovedEdges)
            {
                Assert.AreEqual(expectedRemovedEdges, edgesRemoved);
                edgesRemoved = 0;
            }

            #endregion
        }