open("graph2.dot", "w").write(blocks.dot()) # Test helper methods ## Label2block should always be updated assert blocks.label2block(first_block.label) == first_block my_block = AsmBlock(AsmLabel("testlabel")) blocks.add_node(my_block) assert len(blocks) == 3 assert blocks.label2block(first_block.label) == first_block assert blocks.label2block(my_block.label) == my_block ## Bad blocks assert len(list(blocks.get_bad_blocks())) == 0 assert len(list(blocks.get_bad_blocks_predecessors())) == 0 ### Add a bad block, not linked my_bad_block = AsmBlockBad(AsmLabel("testlabel_bad")) blocks.add_node(my_bad_block) assert list(blocks.get_bad_blocks()) == [my_bad_block] assert len(list(blocks.get_bad_blocks_predecessors())) == 0 ### Link the bad block and update edges ### Indeed, a sub-element has been modified (bto from a block from blocks) my_block.bto.add(AsmConstraintTo(my_bad_block.label)) blocks.rebuild_edges() assert list(blocks.get_bad_blocks_predecessors()) == [my_block] ### Test strict option my_block.bto.add(AsmConstraintTo(my_block.label)) blocks.rebuild_edges() assert list(blocks.get_bad_blocks_predecessors(strict=False)) == [my_block] assert len(list(blocks.get_bad_blocks_predecessors(strict=True))) == 0 ## Sanity check
# Test helper methods ## loc_key_to_block should always be updated assert asmcfg.loc_key_to_block(first_block.loc_key) == first_block testlabel = mdis.loc_db.get_or_create_name_location("testlabel") my_block = AsmBlock(testlabel) asmcfg.add_block(my_block) assert len(asmcfg) == 3 assert asmcfg.loc_key_to_block(first_block.loc_key) == first_block assert asmcfg.loc_key_to_block(my_block.loc_key) == my_block ## Bad asmcfg assert len(list(asmcfg.get_bad_blocks())) == 0 assert len(list(asmcfg.get_bad_blocks_predecessors())) == 0 ### Add a bad block, not linked testlabel_bad = mdis.loc_db.get_or_create_name_location("testlabel_bad") my_bad_block = AsmBlockBad(testlabel_bad) asmcfg.add_block(my_bad_block) assert list(asmcfg.get_bad_blocks()) == [my_bad_block] assert len(list(asmcfg.get_bad_blocks_predecessors())) == 0 ### Link the bad block and update edges ### Indeed, a sub-element has been modified (bto from a block from asmcfg) my_block.bto.add(AsmConstraintTo(my_bad_block.loc_key)) asmcfg.rebuild_edges() assert list(asmcfg.get_bad_blocks_predecessors()) == [my_block.loc_key] ### Test strict option my_block.bto.add(AsmConstraintTo(my_block.loc_key)) asmcfg.rebuild_edges() assert list( asmcfg.get_bad_blocks_predecessors(strict=False)) == [my_block.loc_key] assert len(list(asmcfg.get_bad_blocks_predecessors(strict=True))) == 0