def test_func(): m = parser.parse_string(fixtures.load('func.code')) assert m.function_name == 'multiple word name' assert len(m.variables) == 2 assert m.argument_count == 2 assert m.labels == {'a': 0, 'L1': 1} assert m.variables == [ValueInt(), ValueFloat()]
def test_instructions(): m = parser.parse_string(fixtures.load('instructions.code')) assert len(m.code) == 6 assert m.code == [ins.InsIPush(ValueInt(10)), ins.InsIPush(ValueInt(11)), ins.InsIAdd(), ins.InsIStore(ValueInt(0)), ins.InsILoad(ValueInt(1)), ins.InsIReturn()]
def test_basic_block_complex(): m = parse_string(fixtures.load('sum.code')) anz = controlflow.ControlFlowAnalyzer() anz.analyze(m) assert len(anz.basic_blocks) == 4 assert [bb.instruction_indexes for bb in anz.basic_blocks] == [[0, 1, 2, 3], [4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15], [16, 17]] m = parse_string(fixtures.load('bubblesort.code')) anz = controlflow.ControlFlowAnalyzer() anz.analyze(m) assert len(anz.basic_blocks) == 7 assert [bb.instruction_indexes for bb in anz.basic_blocks ] == [[0, 1, 2], [3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20, 21, 22], [ 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43 ], [44, 45, 46], [47, 48]]
def test_basic_block_complex(): m = parse_string(fixtures.load('sum.code')) anz = controlflow.ControlFlowAnalyzer() anz.analyze(m) assert len(anz.basic_blocks) == 4 assert [bb.instruction_indexes for bb in anz.basic_blocks] == [ [0, 1, 2, 3], [4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15], [16, 17]] m = parse_string(fixtures.load('bubblesort.code')) anz = controlflow.ControlFlowAnalyzer() anz.analyze(m) assert len(anz.basic_blocks) == 7 assert [bb.instruction_indexes for bb in anz.basic_blocks] == [[0, 1, 2], [3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43], [44, 45, 46], [47, 48]]
def test_whole(): m = parser.parse_string(fixtures.load('parse_ok.code')) assert m
def test_variables(): m = parser.parse_string(fixtures.load('variables.code')) assert len(m.variables) == 4 assert m.argument_count == 1 assert m.labels == {'a': 0, 'x': 1, 'b': 2, 'y': 3} assert m.variables == [ValueInt(), ValueInt(), ValueInt(), ValueFloat()]