def test_fill_df_given_while(self): as_tree = ast.parse( ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # Eth block c = 3 # Fth block """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() dom_tree.fill_df() self.assertDfEqual( dom_tree, [], [2, 2], [2, 2], [5, 5], [2, 2], [], )
def test_build_dominator_tree_given_2_lvl(self): as_tree = ast.parse( ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # Eth block c = 3 # Fth block """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_block_list = build_blocks([5, 5, None], [4, 4, None], [3, 3, None], [6, 6, None], [2, 2, None], [1, 1, None], block_links={ '5': [4], '4': [2, 3], '2': [1, 0], '1': [], '0': [], '3': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_block_list) pass
def test_build_dominator_tree_given_1_lvl(self): as_tree = ast.parse( ms("""\ a = 3 # 1st if a > 3: # | a = E # 2nd else: # 3rd z = F # | y = F # Eth """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_block_list = build_blocks([6, 6, None], [3, 3, None], [5, 5, None], [1, 2, None], block_links={ '3': [1, 2, 0], '2': [], '1': [], '0': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_block_list)
def test_dominator_tree_given_13_blocks(self): """ |---------> R | / | \ | A <-- B C | | / \ | \ | D <-- E <- | ---- G | | | | F / \ | L | | | | J | \ | | \ | | | ----> H --| I <----| | \ / ----------------------> K ---/ """ blocks = self.build_blocks_arb( block_links={ 'A': ['B', 'C', 'H'], 'B': ['D'], 'C': ['B', 'D', 'F'], 'D': ['E'], 'E': ['G'], 'F': ['G'], 'G': ['F', 'M'], 'H': ['I', 'J'], 'I': ['L'], 'J': ['K', 'L'], 'K': ['L'], 'L': ['M'], 'M': ['A', 'L'] }) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.root = blocks[0] dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_blocks = self.build_blocks_arb( block_links={ 'A': ['B', 'C', 'D', 'F', 'G', 'H', 'L', 'M'], 'B': [], 'C': [], 'D': ['E'], 'E': [], 'F': [], 'G': [], 'H': ['I', 'J'], 'I': [], 'J': ['K'], 'K': [], 'L': [], 'M': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_blocks)
def test_dominator_tree_given_complex_block(self): """ A | B <------| / \ | C F | | / \ | | G I | | \ / | | H | \ / | D-----------| | E """ blocks = self.build_blocks_arb( block_links={ 'A': ['B'], 'B': ['C', 'F'], 'C': ['D'], 'D': ['E', 'B'], 'E': [], 'F': ['G', 'I'], 'G': ['H'], 'H': ['D'], 'I': ['H'] }) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.root = blocks[0] dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_blocks = self.build_blocks_arb( block_links={ 'A': ['B'], 'B': ['C', 'D', 'F'], 'C': [], 'D': ['E'], 'E': [], 'F': ['G', 'H', 'I'], 'G': [], 'H': [], 'I': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_blocks)
def test_fill_df_given_if_else(self): as_tree = ast.parse( ms("""\ a = 3 # 1st if a > 3: # | a = E # 2nd else: # 3rd z = F # | y = F # Eth """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() dom_tree.fill_df() self.assertDfEqual(dom_tree, [], [6, 6], [6, 6], [])