protected static void ContainsEdge_SourceTarget_ImmutableGraph_UndirectedGraph_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph)
        {
            var edge1 = new Edge <int>(1, 2);
            var edge2 = new Edge <int>(1, 3);
            var edge3 = new Edge <int>(2, 2);

            IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph();

            Assert.IsFalse(graph.ContainsEdge(1, 2));
            Assert.IsFalse(graph.ContainsEdge(2, 1));

            wrappedGraph.AddVerticesAndEdge(edge1);
            graph = createGraph();
            Assert.IsTrue(graph.ContainsEdge(1, 2));
            Assert.IsTrue(graph.ContainsEdge(2, 1));

            wrappedGraph.AddVerticesAndEdge(edge2);
            graph = createGraph();
            Assert.IsTrue(graph.ContainsEdge(1, 3));
            Assert.IsTrue(graph.ContainsEdge(3, 1));

            wrappedGraph.AddVerticesAndEdge(edge3);
            graph = createGraph();
            Assert.IsTrue(graph.ContainsEdge(2, 2));

            // Vertices is not present in the graph
            Assert.IsFalse(graph.ContainsEdge(0, 4));
            Assert.IsFalse(graph.ContainsEdge(1, 4));
            Assert.IsFalse(graph.ContainsEdge(4, 1));
        }
예제 #2
0
 public static void AssertNoAdjacentEdge <TVertex, TEdge>(IImplicitUndirectedGraph <TVertex, TEdge> graph, TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     Assert.IsTrue(graph.IsAdjacentEdgesEmpty(vertex));
     Assert.AreEqual(0, graph.AdjacentDegree(vertex));
     CollectionAssert.IsEmpty(graph.AdjacentEdges(vertex));
 }
        protected static void AdjacentEdges_ImmutableGraph_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph)
        {
            var edge12 = new Edge <int>(1, 2);
            var edge13 = 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);

            wrappedGraph.AddVertex(1);
            IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph();

            AssertNoAdjacentEdge(graph, 1);

            wrappedGraph.AddVertex(5);
            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31, edge33 });
            graph = createGraph();

            AssertHasAdjacentEdges(graph, 1, new[] { edge12, edge13, edge14, edge31 });
            AssertHasAdjacentEdges(graph, 2, new[] { edge12, edge24 });
            AssertHasAdjacentEdges(graph, 3, new[] { edge13, edge31, edge33 }, 4);  // Has self edge counting twice
            AssertHasAdjacentEdges(graph, 4, new[] { edge14, edge24 });
            AssertNoAdjacentEdge(graph, 5);
        }
예제 #4
0
 protected static void TryGetEdge_UndirectedGraph_Test(
     [NotNull] GraphData <int, Edge <int> > data,
     [NotNull] IImplicitUndirectedGraph <int, Edge <int> > graph)
 {
     ContainsEdge_SourceTarget_GenericTest(
         data,
         (source, target) => graph.TryGetEdge(source, target, out _),
         false);
 }
 protected static void AdjacentEdge_NullThrows_Test <TVertex, TEdge>(
     [NotNull] IImplicitUndirectedGraph <TVertex, TEdge> graph)
     where TVertex : class
     where TEdge : IEdge <TVertex>
 {
     // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
     // ReSharper disable once AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(() => graph.AdjacentEdge(null, 0));
 }
예제 #6
0
 protected static void ContainsEdge_SourceTarget_UndirectedGraph_Test(
     [NotNull] GraphData <int, Edge <int> > data,
     [NotNull] IImplicitUndirectedGraph <int, Edge <int> > graph)
 {
     ContainsEdge_SourceTarget_GenericTest(
         data,
         graph.ContainsEdge,
         false);
 }
        bool IImplicitUndirectedGraph <TVertex, TEdge> .ContainsEdge(TVertex source, TVertex target)
        {
            IImplicitUndirectedGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(source != null);
            Contract.Requires(target != null);
            Contract.Ensures(Contract.Result <bool>() == Enumerable.Any(ithis.AdjacentEdges(source), e => e.Target.Equals(target) || e.Source.Equals(target)));

            return(default(bool));
        }
        bool IImplicitUndirectedGraph <TVertex, TEdge> .TryGetEdge(TVertex source, TVertex target, out TEdge edge)
        {
            IImplicitUndirectedGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(source != null);
            Contract.Requires(target != null);

            edge = default(TEdge);
            return(default(bool));
        }
        bool IImplicitUndirectedGraph <TVertex, TEdge> .IsAdjacentEdgesEmpty(TVertex v)
        {
            IImplicitUndirectedGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(Contract.Result <bool>() == (ithis.AdjacentDegree(v) == 0));

            return(default(bool));
        }
        int IImplicitUndirectedGraph <TVertex, TEdge> .AdjacentDegree(TVertex v)
        {
            IImplicitUndirectedGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(Contract.Result <int>() == Enumerable.Count(ithis.AdjacentEdges(v)));

            return(default(int));
        }
예제 #11
0
 protected static void TryGetEdge_Throws_UndirectedGraph_Test <TVertex, TEdge>(
     [NotNull] IImplicitUndirectedGraph <TVertex, TEdge> graph)
     where TVertex : class, new()
     where TEdge : IEdge <TVertex>
 {
     // ReSharper disable AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(() => graph.TryGetEdge(null, new TVertex(), out _));
     Assert.Throws <ArgumentNullException>(() => graph.TryGetEdge(new TVertex(), null, out _));
     Assert.Throws <ArgumentNullException>(() => graph.TryGetEdge(null, null, out _));
     // ReSharper restore AssignNullToNotNullAttribute
 }
 protected static void AdjacentEdges_NullThrows_Test <TVertex, TEdge>(
     [NotNull] IImplicitUndirectedGraph <TVertex, TEdge> graph)
     where TVertex : class
     where TEdge : IEdge <TVertex>
 {
     // ReSharper disable ReturnValueOfPureMethodIsNotUsed
     // ReSharper disable AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(() => graph.IsAdjacentEdgesEmpty(null));
     Assert.Throws <ArgumentNullException>(() => graph.AdjacentDegree(null));
     Assert.Throws <ArgumentNullException>(() => graph.AdjacentEdges(null));
     // ReSharper restore AssignNullToNotNullAttribute
     // ReSharper restore ReturnValueOfPureMethodIsNotUsed
 }
        TEdge IImplicitUndirectedGraph <TVertex, TEdge> .AdjacentEdge(TVertex v, int index)
        {
            IImplicitUndirectedGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(Contract.Result <TEdge>() != null);
            Contract.Ensures(
                Contract.Result <TEdge>().Source.Equals(v) ||
                Contract.Result <TEdge>().Target.Equals(v));

            return(default(TEdge));
        }
        protected static void AdjacentEdges_Throws_Test <TVertex>(
            [NotNull] IImplicitUndirectedGraph <TVertex, Edge <TVertex> > graph)
            where TVertex : class, IEquatable <TVertex>, new()
        {
            AdjacentEdges_NullThrows_Test(graph);

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            var vertex = new TVertex();

            Assert.Throws <VertexNotFoundException>(() => graph.IsAdjacentEdgesEmpty(vertex));
            Assert.Throws <VertexNotFoundException>(() => graph.AdjacentDegree(vertex));
            Assert.Throws <VertexNotFoundException>(() => graph.AdjacentEdges(vertex));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
예제 #15
0
        public static void AssertHasAdjacentEdges <TVertex, TEdge>(
            IImplicitUndirectedGraph <TVertex, TEdge> graph,
            TVertex vertex,
            IEnumerable <TEdge> edges,
            int degree = -1)    // If not set => equals the count of edges
            where TEdge : IEdge <TVertex>
        {
            TEdge[] edgeArray = edges.ToArray();
            CollectionAssert.IsNotEmpty(edgeArray);

            Assert.IsFalse(graph.IsAdjacentEdgesEmpty(vertex));
            Assert.AreEqual(
                degree < 0 ? edgeArray.Length : degree,
                graph.AdjacentDegree(vertex));
            CollectionAssert.AreEquivalent(edgeArray, graph.AdjacentEdges(vertex));
        }
예제 #16
0
        protected static void AdjacentEdges_Throws_Test(
            [NotNull] GraphData <int, Edge <int> > data,
            [NotNull] IImplicitUndirectedGraph <int, Edge <int> > graph)
        {
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            data.CheckCalls(0);

            data.ShouldReturnValue = false;
            Assert.Throws <VertexNotFoundException>(() => graph.IsAdjacentEdgesEmpty(1));
            data.CheckCalls(1);

            Assert.Throws <VertexNotFoundException>(() => graph.AdjacentDegree(1));
            data.CheckCalls(1);

            Assert.Throws <VertexNotFoundException>(() => graph.AdjacentEdges(1));
            data.CheckCalls(1);
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
예제 #17
0
        protected static void AdjacentEdge_Test(
            [NotNull] GraphData <int, Edge <int> > data,
            [NotNull] IImplicitUndirectedGraph <int, Edge <int> > graph)
        {
            var edge11 = new Edge <int>(1, 1);
            var edge12 = new Edge <int>(1, 2);
            var edge13 = new Edge <int>(1, 3);

            data.CheckCalls(0);

            data.ShouldReturnValue = true;
            data.ShouldReturnEdges = new[] { edge11, edge12, edge13 };
            Assert.AreSame(edge11, graph.AdjacentEdge(1, 0));
            data.CheckCalls(1);

            Assert.AreSame(edge13, graph.AdjacentEdge(1, 2));
            data.CheckCalls(1);
        }
예제 #18
0
        protected static void AdjacentEdges_Test(
            [NotNull] GraphData <int, Edge <int> > data,
            [NotNull] IImplicitUndirectedGraph <int, Edge <int> > graph)
        {
            data.CheckCalls(0);

            data.ShouldReturnValue = true;
            AssertNoAdjacentEdge(graph, 1);
            data.CheckCalls(3);

            Edge <int>[] edges =
            {
                new Edge <int>(1, 2),
                new Edge <int>(1, 3)
            };
            data.ShouldReturnEdges = edges;
            AssertHasAdjacentEdges(graph, 1, edges);
            data.CheckCalls(3);
        }
        IEnumerable <TEdge> IImplicitUndirectedGraph <TVertex, TEdge> .AdjacentEdges(TVertex v)
        {
            IImplicitUndirectedGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(Contract.Result <IEnumerable <TEdge> >() != null);
            Contract.Ensures(
                Enumerable.All(
                    Contract.Result <IEnumerable <TEdge> >(),
                    edge =>
                    edge != null &&
                    ithis.ContainsEdge(edge.Source, edge.Target) &&
                    (edge.Source.Equals(v) || edge.Target.Equals(v))
                    )
                );

            return(default(IEnumerable <TEdge>));
        }
예제 #20
0
        protected static void AdjacentEdge_Throws_Test(
            [NotNull] GraphData <int, Edge <int> > data,
            [NotNull] IImplicitUndirectedGraph <int, Edge <int> > graph)
        {
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            data.CheckCalls(0);

            data.ShouldReturnValue = false;
            Assert.Throws <VertexNotFoundException>(() => graph.AdjacentEdge(1, 0));
            data.CheckCalls(1);

            data.ShouldReturnValue = true;
            AssertIndexOutOfRange(() => graph.AdjacentEdge(1, 0));
            data.CheckCalls(1);

            data.ShouldReturnEdges = new[] { new Edge <int>(1, 2) };
            AssertIndexOutOfRange(() => graph.AdjacentEdge(1, 1));
            data.CheckCalls(1);
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
        protected static void AdjacentEdge_ImmutableGraph_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph)
        {
            var edge11 = new Edge <int>(1, 1);
            var edge12 = new Edge <int>(1, 2);
            var edge13 = new Edge <int>(1, 3);
            var edge24 = new Edge <int>(2, 4);
            var edge33 = new Edge <int>(3, 3);
            var edge41 = new Edge <int>(4, 1);

            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge11, edge12, edge13, edge24, edge33, edge41 });
            IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph();

            Assert.AreSame(edge11, graph.AdjacentEdge(1, 0));
            Assert.AreSame(edge13, graph.AdjacentEdge(1, 2));
            Assert.AreSame(edge41, graph.AdjacentEdge(1, 3));
            Assert.AreSame(edge13, graph.AdjacentEdge(3, 0));
            Assert.AreSame(edge33, graph.AdjacentEdge(3, 1));
            Assert.AreSame(edge24, graph.AdjacentEdge(4, 0));
        }
        protected static void AdjacentEdge_Throws_ImmutableGraph_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph)
        {
            const int vertex1 = 1;
            const int vertex2 = 2;

            IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph();

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <VertexNotFoundException>(() => graph.AdjacentEdge(vertex1, 0));

            wrappedGraph.AddVertex(vertex1);
            wrappedGraph.AddVertex(vertex2);
            graph = createGraph();
            AssertIndexOutOfRange(() => graph.AdjacentEdge(vertex1, 0));

            wrappedGraph.AddEdge(new Edge <int>(1, 2));
            graph = createGraph();
            AssertIndexOutOfRange(() => graph.AdjacentEdge(vertex1, 5));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
        protected static void TryGetEdge_ImmutableGraph_UndirectedGraph_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph)
        {
            var edge1 = new Edge <int>(1, 2);
            var edge2 = new Edge <int>(1, 2);
            var edge3 = new Edge <int>(1, 3);
            var edge4 = new Edge <int>(2, 2);
            var edge5 = new Edge <int>(2, 4);
            var edge6 = new Edge <int>(3, 1);
            var edge7 = new Edge <int>(5, 2);

            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6, edge7 });
            IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph();

            Assert.IsFalse(graph.TryGetEdge(0, 10, out _));
            Assert.IsFalse(graph.TryGetEdge(0, 1, out _));

            Assert.IsTrue(graph.TryGetEdge(2, 4, out Edge <int> gotEdge));
            Assert.AreSame(edge5, gotEdge);

            Assert.IsTrue(graph.TryGetEdge(1, 2, out gotEdge));
            Assert.AreSame(edge1, gotEdge);

            Assert.IsTrue(graph.TryGetEdge(2, 2, out gotEdge));
            Assert.AreSame(edge4, gotEdge);

            // 1 -> 2 is present in this undirected graph
            Assert.IsTrue(graph.TryGetEdge(2, 1, out gotEdge));
            Assert.AreSame(edge1, gotEdge);

            Assert.IsTrue(graph.TryGetEdge(5, 2, out gotEdge));
            Assert.AreSame(edge7, gotEdge);

            // 5 -> 2 is present in this undirected graph
            Assert.IsTrue(graph.TryGetEdge(2, 5, out gotEdge));
            Assert.AreSame(edge7, gotEdge);
        }