public void If() { // Artificially construct an if construct. var graph = TestGraphs.CreateIfElse(); var n1 = graph.GetNodeByOffset(0); var n2 = graph.GetNodeByOffset(2); var n3 = graph.GetNodeByOffset(3); 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.False(dominatorTree.Dominates(n3, n2)); Assert.True(dominatorTree.Dominates(n3, n3)); Assert.False(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)); }
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)); } }