Пример #1
0
        public void Loop()
        {
            // Artificially construct a looping construct.
            var graph = TestGraphs.CreateLoop();
            var n1    = graph.GetNodeByOffset(0);
            var n2    = graph.GetNodeByOffset(1);
            var n3    = graph.GetNodeByOffset(2);
            var n4    = graph.GetNodeByOffset(4);

            var dominatorTree = DominatorTree <DummyInstruction> .FromGraph(graph);

            Assert.Equal(graph.Entrypoint, dominatorTree.Root.OriginalNode);

            Assert.True(dominatorTree.Dominates(n1, n1));
            Assert.True(dominatorTree.Dominates(n1, n2));
            Assert.True(dominatorTree.Dominates(n1, n3));
            Assert.True(dominatorTree.Dominates(n1, n4));

            Assert.False(dominatorTree.Dominates(n2, n1));
            Assert.True(dominatorTree.Dominates(n2, n2));
            Assert.False(dominatorTree.Dominates(n2, n3));
            Assert.False(dominatorTree.Dominates(n2, n4));

            Assert.False(dominatorTree.Dominates(n3, n1));
            Assert.True(dominatorTree.Dominates(n3, n2));
            Assert.True(dominatorTree.Dominates(n3, n3));
            Assert.True(dominatorTree.Dominates(n3, n4));

            Assert.False(dominatorTree.Dominates(n4, n1));
            Assert.False(dominatorTree.Dominates(n4, n2));
            Assert.False(dominatorTree.Dominates(n4, n3));
            Assert.True(dominatorTree.Dominates(n4, n4));
        }
Пример #2
0
        public void If()
        {
            // Artificially construct an if construct.
            var graph = TestGraphs.CreateIfElse();

            var n1 = graph.GetNodeById(1);
            var n2 = graph.GetNodeById(2);
            var n3 = graph.GetNodeById(3);
            var n4 = graph.GetNodeById(4);

            // Record a depth first traversal.
            var traversal = new DepthFirstTraversal();
            var recorder  = new TraversalOrderRecorder(traversal);

            traversal.Run(n1);

            // Check if n1 is before any node in the traversal.
            Assert.All(graph.GetNodes(),
                       n => Assert.True(n1 == n || recorder.GetIndex(n1) < recorder.GetIndex(n)));

            // DFS should either pick n2 or n3. If n2, then n4 is before n3, otherwise before n2.
            if (recorder.GetIndex(n2) < recorder.GetIndex(n3))
            {
                Assert.True(recorder.GetIndex(n4) < recorder.GetIndex(n3));
            }
            else
            {
                Assert.True(recorder.GetIndex(n4) < recorder.GetIndex(n2));
            }
        }
Пример #3
0
        public void Path()
        {
            // Artificially construct a path of four nodes in sequential order.
            var graph = TestGraphs.CreatePath();
            var n1    = graph.GetNodeByOffset(0);
            var n2    = graph.GetNodeByOffset(1);
            var n3    = graph.GetNodeByOffset(2);
            var n4    = graph.GetNodeByOffset(3);

            var dominatorTree = DominatorTree <DummyInstruction> .FromGraph(graph);

            Assert.Equal(graph.Entrypoint, dominatorTree.Root.OriginalNode);

            Assert.True(dominatorTree.Dominates(n1, n1));
            Assert.True(dominatorTree.Dominates(n1, n2));
            Assert.True(dominatorTree.Dominates(n1, n3));
            Assert.True(dominatorTree.Dominates(n1, n4));

            Assert.False(dominatorTree.Dominates(n2, n1));
            Assert.True(dominatorTree.Dominates(n2, n2));
            Assert.True(dominatorTree.Dominates(n2, n3));
            Assert.True(dominatorTree.Dominates(n2, n4));

            Assert.False(dominatorTree.Dominates(n3, n1));
            Assert.False(dominatorTree.Dominates(n3, n2));
            Assert.True(dominatorTree.Dominates(n3, n3));
            Assert.True(dominatorTree.Dominates(n3, n4));

            Assert.False(dominatorTree.Dominates(n4, n1));
            Assert.False(dominatorTree.Dominates(n4, n2));
            Assert.False(dominatorTree.Dominates(n4, n3));
            Assert.True(dominatorTree.Dominates(n4, n4));
        }
Пример #4
0
        public void SingleNode()
        {
            var graph = TestGraphs.CreateSingularGraph();

            var dominatorTree = DominatorTree <DummyInstruction> .FromGraph(graph);

            Assert.Equal(graph.Entrypoint, dominatorTree.Root.OriginalNode);
            Assert.True(dominatorTree.Dominates(graph.Entrypoint, graph.Entrypoint));
        }
Пример #5
0
 protected IFieldsGraph RandomGraph(int rowCount, int columnCount, double blackDencity)
 {
     options.RowCount           = rowCount;
     options.ColumnCount        = columnCount;
     options.DfsSearchDepth     = rowCount * columnCount;
     options.DfsMaxVistedNodes  = MAX_VISITED_NODES_COUNT;
     options.BfsMaxVisitedNodes = MAX_VISITED_NODES_COUNT;
     return(TestGraphs.Random(rowCount, columnCount, blackDencity));
 }
Пример #6
0
        public void SingleNode()
        {
            var graph     = TestGraphs.CreateSingularGraph();
            var startNode = graph.GetNodeById(1);

            // Record a depth first traversal.
            var traversal = new DepthFirstTraversal();
            var recorder  = new TraversalOrderRecorder(traversal);

            traversal.Run(startNode);

            Assert.Single(recorder.GetTraversal());
            Assert.Equal(0, recorder.GetIndex(startNode));
        }
Пример #7
0
        public void PathReversed()
        {
            // Artificially construct a path of four nodes in sequential order.
            var graph = TestGraphs.CreatePath();

            // Record a depth first traversal.
            var traversal = new DepthFirstTraversal(true);
            var recorder  = new TraversalOrderRecorder(traversal);

            traversal.Run(graph.GetNodeById(4));

            // Traversal should exactly be the path.
            Assert.Equal(new INode[]
            {
                graph.GetNodeById(4),
                graph.GetNodeById(3),
                graph.GetNodeById(2),
                graph.GetNodeById(1),
            }, recorder.GetTraversal());
        }
Пример #8
0
        public void LoopReversed()
        {
            // Artificially construct a looping construct.
            var graph = TestGraphs.CreateLoop();
            var n1    = graph.GetNodeById(1);
            var n2    = graph.GetNodeById(2);
            var n3    = graph.GetNodeById(3);
            var n4    = graph.GetNodeById(4);

            // Record a depth first traversal.
            var traversal = new DepthFirstTraversal(true);
            var recorder  = new TraversalOrderRecorder(traversal);

            traversal.Run(n4);

            // Check if n1 is before any node in the traversal.
            Assert.All(graph.GetNodes(),
                       n => Assert.True(n4 == n || recorder.GetIndex(n4) < recorder.GetIndex(n)));

            Assert.True(recorder.GetIndex(n1) > recorder.GetIndex(n3));
        }