internal void Graph_OutEdgesShouldHaveSameTail(GraphDefinitionParameter p)
        {
            // Arrange
            using MutableUndirectedSimpleIncidenceGraph builder = new(p.VertexCount);
            foreach (Endpoints endpoints in p.Edges)
            {
                builder.Add(endpoints.Tail, endpoints.Head);
            }

            Graph graph = builder.ToGraph();

            // Act
            for (int vertex = 0; vertex < graph.VertexCount; ++vertex)
            {
                EdgeEnumerator outEdges = graph.EnumerateOutEdges(vertex);
                while (outEdges.MoveNext())
                {
                    Endpoints edge    = outEdges.Current;
                    bool      hasTail = graph.TryGetTail(edge, out int tail);
                    if (!hasTail)
                    {
                        Assert.True(hasTail);
                    }

                    // Assert
                    Assert.Equal(vertex, tail);
                }
            }
        }
Пример #2
0
        internal void Graph_ShouldContainSameSetOfEdges(GraphDefinitionParameter p)
        {
            // Arrange
            using MutableSimpleIncidenceGraph builder = new(p.VertexCount);
            foreach (Endpoints endpoints in p.Edges)
            {
                builder.Add(endpoints.Tail, endpoints.Head);
            }

            Graph graph = builder.ToGraph();
            HashSet <Endpoints> expectedEdgeSet = p.Edges.ToHashSet();

            // Act
            HashSet <Endpoints> actualEdgeSet = new();

            for (int vertex = 0; vertex < graph.VertexCount; ++vertex)
            {
                EdgeEnumerator outEdges = graph.EnumerateOutEdges(vertex);
                while (outEdges.MoveNext())
                {
                    Endpoints edge         = outEdges.Current;
                    bool      hasEndpoints = TryGetEndpoints(graph, edge, out Endpoints endpoints);
                    if (!hasEndpoints)
                    {
                        Assert.True(hasEndpoints);
                    }

                    actualEdgeSet.Add(endpoints);
                }
            }

            // Assert
            Assert.Equal(expectedEdgeSet, actualEdgeSet, HashSetEqualityComparer);
        }
        private static bool TryGetEndpoints(Graph graph, Endpoints edge, out Endpoints endpoints)
        {
            bool hasTail = graph.TryGetTail(edge, out int tail);
            bool hasHead = graph.TryGetHead(edge, out int head);

            endpoints = new Endpoints(tail, head);
            return(hasTail && hasHead);
        }
        internal void Graph_SizeShouldMatch(GraphDefinitionParameter p)
        {
            // Arrange
            using MutableUndirectedSimpleIncidenceGraph builder = new(p.VertexCount);
            foreach (Endpoints endpoints in p.Edges)
            {
                builder.Add(endpoints.Tail, endpoints.Head);
            }

            // Act
            Graph graph = builder.ToGraph();

            // Assert
            Assert.Equal(p.VertexCount, graph.VertexCount);
            Assert.Equal(p.Edges.Count, graph.EdgeCount);
        }
Пример #5
0
        internal void EnumerateEdges(GraphParameter <Graph> p)
        {
            Graph graph = p.Graph;

            // Arrange

            int source = graph.VertexCount >> 1;

            byte[] setBackingStore = ArrayPool <byte> .Shared.Rent(Math.Max(graph.VertexCount, source + 1));

            Array.Clear(setBackingStore, 0, setBackingStore.Length);
            IndexedSet exploredSet = new(setBackingStore);

            // Act

            IEnumerator <Endpoints> basicSteps      = Dfs.EnumerateEdges(graph, source, graph.VertexCount) !;
            IEnumerator <Endpoints> enumerableSteps = EnumerableDfs.EnumerateEdges(graph, source, exploredSet) !;

            // Assert

            while (true)
            {
                bool expectedHasCurrent = enumerableSteps.MoveNext();
                bool actualHasCurrent   = basicSteps.MoveNext();

                Assert.Equal(expectedHasCurrent, actualHasCurrent);

                if (!expectedHasCurrent || !actualHasCurrent)
                {
                    break;
                }

                Endpoints expected = enumerableSteps.Current;
                Endpoints actual   = basicSteps.Current;

                if (expected != actual)
                {
                    Assert.Equal(expected, actual);
                    break;
                }
            }
        }