protected static void ContainsEdge_EquatableEdge_EdgesOnly_Test(
            [NotNull] EdgeListGraph <int, EquatableEdge <int> > graph)
        {
            var edge1      = new EquatableEdge <int>(1, 2);
            var edge2      = new EquatableEdge <int>(1, 3);
            var edge3      = new EquatableEdge <int>(2, 1);
            var edge4      = new EquatableEdge <int>(2, 2);
            var otherEdge1 = new EquatableEdge <int>(1, 2);

            Assert.IsFalse(graph.ContainsEdge(edge1));
            Assert.IsFalse(graph.ContainsEdge(edge2));
            Assert.IsFalse(graph.ContainsEdge(edge3));
            Assert.IsFalse(graph.ContainsEdge(edge4));
            Assert.IsFalse(graph.ContainsEdge(otherEdge1));

            graph.AddVerticesAndEdge(edge1);
            Assert.IsTrue(graph.ContainsEdge(edge1));
            Assert.IsFalse(graph.ContainsEdge(edge2));
            Assert.IsFalse(graph.ContainsEdge(edge3));
            Assert.IsFalse(graph.ContainsEdge(edge4));
            Assert.IsTrue(graph.ContainsEdge(otherEdge1));

            graph.AddVerticesAndEdge(edge2);
            Assert.IsTrue(graph.ContainsEdge(edge1));
            Assert.IsTrue(graph.ContainsEdge(edge2));
            Assert.IsFalse(graph.ContainsEdge(edge3));
            Assert.IsFalse(graph.ContainsEdge(edge4));
            Assert.IsTrue(graph.ContainsEdge(otherEdge1));

            graph.AddVerticesAndEdge(edge3);
            Assert.IsTrue(graph.ContainsEdge(edge1));
            Assert.IsTrue(graph.ContainsEdge(edge2));
            Assert.IsTrue(graph.ContainsEdge(edge3));
            Assert.IsFalse(graph.ContainsEdge(edge4));
            Assert.IsTrue(graph.ContainsEdge(otherEdge1));

            graph.AddVerticesAndEdge(edge4);
            Assert.IsTrue(graph.ContainsEdge(edge1));
            Assert.IsTrue(graph.ContainsEdge(edge2));
            Assert.IsTrue(graph.ContainsEdge(edge3));
            Assert.IsTrue(graph.ContainsEdge(edge4));
            Assert.IsTrue(graph.ContainsEdge(otherEdge1));

            graph.AddVerticesAndEdge(otherEdge1);
            Assert.IsTrue(graph.ContainsEdge(edge1));
            Assert.IsTrue(graph.ContainsEdge(edge2));
            Assert.IsTrue(graph.ContainsEdge(edge3));
            Assert.IsTrue(graph.ContainsEdge(edge4));
            Assert.IsTrue(graph.ContainsEdge(otherEdge1));

            // Both vertices not in graph
            Assert.IsFalse(graph.ContainsEdge(new EquatableEdge <int>(0, 10)));
            // Source not in graph
            Assert.IsFalse(graph.ContainsEdge(new EquatableEdge <int>(0, 1)));
            // Target not in graph
            Assert.IsFalse(graph.ContainsEdge(new EquatableEdge <int>(1, 0)));
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public void EdgeListGraphTestProperty()
        {
            var g = new EdgeListGraph <int, IEdge <int> >();

            Assert.IsTrue(g.IsDirected);
            Assert.IsTrue(g.AllowParallelEdges);
            Assert.IsTrue(g.IsEdgesEmpty);
            Assert.IsTrue(g.IsVerticesEmpty);
            Assert.AreEqual(g.VertexCount, 0);
            Assert.AreEqual(g.EdgeCount, 0);
            var h = new EdgeListGraph <int, IEdge <int> >(false, false);

            Assert.IsFalse(h.IsDirected);
            Assert.IsFalse(h.AllowParallelEdges);
            h.AddVerticesAndEdge(new EquatableEdge <int>(1, 2));
            Assert.IsFalse(h.IsEdgesEmpty);
            Assert.IsFalse(h.IsVerticesEmpty);
            Assert.AreEqual(h.VertexCount, 2);
            Assert.AreEqual(h.EdgeCount, 1);
            var hv = h.Vertices.ToList <int>();

            Assert.AreEqual(hv[0], 1);
            Assert.AreEqual(hv[1], 2);
            var he = h.Edges.ToList <IEdge <int> >();

            Assert.AreEqual(he[0], new EquatableEdge <int>(1, 2));
        }
Esempio n. 4
0
 protected static void AddVerticesAndEdge_Throws_EdgesOnly_Test <TVertex, TEdge>(
     [NotNull] EdgeListGraph <TVertex, TEdge> graph)
     where TEdge : class, IEdge <TVertex>
 {
     // ReSharper disable once AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(() => graph.AddVerticesAndEdge(null));
     AssertEmptyGraph(graph);
 }
Esempio n. 5
0
        public void Clear()
        {
            int edgesRemoved = 0;

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

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

            AssertEmptyGraph(graph);

            graph.Clear();
            AssertEmptyGraph(graph);
            CheckCounter(0);

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

            graph.Clear();
            AssertEmptyGraph(graph);
            CheckCounter(3);

            #region Local function

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

            #endregion
        }