Beispiel #1
0
        protected static void AddVerticesAndEdgeRange_EdgesOnly_Test(
            [NotNull] EdgeListGraph <int, Edge <int> > graph)
        {
            int edgeAdded = 0;

            AssertEmptyGraph(graph);
            // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
            graph.EdgeAdded += e =>
            {
                Assert.IsNotNull(e);
                ++edgeAdded;
            };

            // Edge 1, 2
            var edge1 = new Edge <int>(1, 2);
            var edge2 = new Edge <int>(1, 3);

            Assert.AreEqual(2, graph.AddVerticesAndEdgeRange(new[] { edge1, edge2 }));
            Assert.AreEqual(2, edgeAdded);
            AssertHasVertices(graph, new[] { 1, 2, 3 });
            AssertHasEdges(graph, new[] { edge1, edge2 });

            // Edge 1, 3
            var edge3 = new Edge <int>(2, 3);

            Assert.AreEqual(1, graph.AddVerticesAndEdgeRange(new[] { edge1, edge3 })); // Showcase the add of only one edge
            Assert.AreEqual(3, edgeAdded);
            AssertHasVertices(graph, new[] { 1, 2, 3 });
            AssertHasEdges(graph, new[] { edge1, edge2, edge3 });
        }
Beispiel #2
0
        protected static void AddVerticesAndEdgeRange_EdgesOnly_Test(
            EdgeListGraph <int, Edge <int> > graph)
        {
            int edgeAdded = 0;

            AssertEmptyGraph(graph);
            graph.EdgeAdded += e =>
            {
                Assert.IsNotNull(e);
                ++edgeAdded;
            };

            // Edge 1, 2
            var edge1 = new Edge <int>(1, 2);
            var edge2 = new Edge <int>(1, 3);

            Assert.AreEqual(2, graph.AddVerticesAndEdgeRange(new[] { edge1, edge2 }));
            Assert.AreEqual(2, edgeAdded);
            AssertHasVertices(graph, new[] { 1, 2, 3 });
            AssertHasEdges(graph, new[] { edge1, edge2 });

            // Edge 1, 3
            var edge3 = new Edge <int>(2, 3);

            Assert.AreEqual(1, graph.AddVerticesAndEdgeRange(new[] { edge1, edge3 })); // Showcase the add of only one edge
            Assert.AreEqual(3, edgeAdded);
            AssertHasVertices(graph, new[] { 1, 2, 3 });
            AssertHasEdges(graph, new[] { edge1, edge2, edge3 });
        }
Beispiel #3
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 #4
0
        public void Clone()
        {
            var graph = new EdgeListGraph <int, Edge <int> >();

            AssertEmptyGraph(graph);

            var clonedGraph = graph.Clone();

            Assert.IsNotNull(clonedGraph);
            AssertEmptyGraph(clonedGraph);

            clonedGraph = (EdgeListGraph <int, Edge <int> >)((ICloneable)graph).Clone();
            Assert.IsNotNull(clonedGraph);
            AssertEmptyGraph(clonedGraph);

            var edge1 = new Edge <int>(1, 2);
            var edge2 = new Edge <int>(1, 3);
            var edge3 = new Edge <int>(2, 3);

            graph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3 });
            AssertHasVertices(graph, new[] { 1, 2, 3 });
            AssertHasEdges(graph, new[] { edge1, edge2, edge3 });

            clonedGraph = graph.Clone();
            Assert.IsNotNull(clonedGraph);
            AssertHasVertices(clonedGraph, new[] { 1, 2, 3 });
            AssertHasEdges(clonedGraph, new[] { edge1, edge2, edge3 });

            clonedGraph = (EdgeListGraph <int, Edge <int> >)((ICloneable)graph).Clone();
            Assert.IsNotNull(clonedGraph);
            AssertHasVertices(clonedGraph, new[] { 1, 2, 3 });
            AssertHasEdges(clonedGraph, new[] { edge1, edge2, edge3 });
        }
Beispiel #5
0
        protected static void RemoveEdge_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);
            var edgeNotInGraph   = new Edge <int>(3, 4);
            var edgeNotEquatable = new Edge <int>(1, 2);

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

            Assert.IsFalse(graph.RemoveEdge(edgeNotInGraph));
            CheckCounter(0);

            Assert.IsFalse(graph.RemoveEdge(edgeNotEquatable));
            CheckCounter(0);

            Assert.IsTrue(graph.RemoveEdge(edge13Bis));
            CheckCounter(1);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4 });
            AssertHasEdges(graph, new[] { edge12, edge13, edge14, edge24, edge31, edge33 });

            Assert.IsTrue(graph.RemoveEdge(edge31));
            CheckCounter(1);
            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));
            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
        }
Beispiel #6
0
        protected static void AddVerticesAndEdgeRange_Throws_EdgesOnly_Test(
            EdgeListGraph <int, Edge <int> > graph)
        {
            int edgeAdded = 0;

            AssertEmptyGraph(graph);
            graph.EdgeAdded += e =>
            {
                Assert.IsNotNull(e);
                ++edgeAdded;
            };

            // ReSharper disable once AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => graph.AddVerticesAndEdgeRange(null));

            // Edge 1, 2, 3
            var edge1 = new Edge <int>(1, 2);
            var edge3 = new Edge <int>(1, 3);

            Assert.Throws <ArgumentNullException>(() => graph.AddVerticesAndEdgeRange(new[] { edge1, null, edge3 }));
            Assert.AreEqual(0, edgeAdded);
            AssertEmptyGraph(graph);
        }