コード例 #1
0
        protected static void RemoveEdge_EquatableEdge_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, EquatableEdge <int> > graph)
        {
            int verticesRemoved = 0;
            int edgesRemoved    = 0;

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

            var edge12                     = new EquatableEdge <int>(1, 2);
            var edge13                     = new EquatableEdge <int>(1, 3);
            var edge13Bis                  = new EquatableEdge <int>(1, 3);
            var edge14                     = new EquatableEdge <int>(1, 4);
            var edge24                     = new EquatableEdge <int>(2, 4);
            var edge31                     = new EquatableEdge <int>(3, 1);
            var edge33                     = new EquatableEdge <int>(3, 3);
            var edgeNotInGraph             = new EquatableEdge <int>(3, 4);
            var edgeWithVertexNotInGraph1  = new EquatableEdge <int>(2, 10);
            var edgeWithVertexNotInGraph2  = new EquatableEdge <int>(10, 2);
            var edgeWithVerticesNotInGraph = new EquatableEdge <int>(10, 11);
            var edgeEquatable              = new EquatableEdge <int>(1, 2);

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

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

            Assert.IsFalse(graph.RemoveEdge(edgeWithVertexNotInGraph1));
            CheckCounters(0);

            Assert.IsFalse(graph.RemoveEdge(edgeWithVertexNotInGraph2));
            CheckCounters(0);

            Assert.IsFalse(graph.RemoveEdge(edgeWithVerticesNotInGraph));
            CheckCounters(0);

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

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

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

            Assert.IsTrue(graph.RemoveEdge(edge13));
            Assert.IsTrue(graph.RemoveEdge(edge14));
            Assert.IsTrue(graph.RemoveEdge(edge24));
            Assert.IsTrue(graph.RemoveEdge(edge33));
            CheckCounters(4);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4 });
            AssertNoEdge(graph);

            #region Local function

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

            #endregion
        }