def test_flattenBlocks_branch(): blockTrue = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.byte, '"true"'), pt.TealOp(None, pt.Op.return_) ]) blockFalse = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.byte, '"false"'), pt.TealOp(None, pt.Op.return_) ]) block = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)]) block.setTrueBlock(blockTrue) block.setFalseBlock(blockFalse) block.addIncoming() block.validateTree() blocks = [block, blockFalse, blockTrue] expected = [ pt.TealOp(None, pt.Op.int, 1), pt.TealOp(None, pt.Op.bnz, pt.LabelReference("l2")), pt.TealOp(None, pt.Op.byte, '"false"'), pt.TealOp(None, pt.Op.return_), pt.TealLabel(None, pt.LabelReference("l2")), pt.TealOp(None, pt.Op.byte, '"true"'), pt.TealOp(None, pt.Op.return_), ] actual = flattenBlocks(blocks) assert actual == expected
def test_flattenBlocks_single_one(): blocks = [pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 1)])] expected = [pt.TealOp(None, pt.Op.int, 1)] actual = flattenBlocks(blocks) assert actual == expected
def test_flattenBlocks_none(): blocks = [] expected = [] actual = flattenBlocks(blocks) assert actual == expected
def test_flattenBlocks_single_empty(): blocks = [pt.TealSimpleBlock([])] expected = [] actual = flattenBlocks(blocks) assert actual == expected
def sort_subroutine_blocks( subroutine_start_blocks: Dict[Optional[SubroutineDefinition], TealBlock], subroutine_end_blocks: Dict[Optional[SubroutineDefinition], TealBlock], ) -> Dict[Optional[SubroutineDefinition], List[TealComponent]]: subroutine_mapping: Dict[Optional[SubroutineDefinition], List[TealComponent]] = dict() for subroutine, start in subroutine_start_blocks.items(): order = sortBlocks(start, subroutine_end_blocks[subroutine]) subroutine_mapping[subroutine] = flattenBlocks(order) return subroutine_mapping
def test_flattenBlocks_multiple_branch_converge(): blockEnd = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.return_)]) blockTrueTrue = pt.TealSimpleBlock( [pt.TealOp(None, pt.Op.byte, '"true true"')]) blockTrueTrue.setNextBlock(blockEnd) blockTrueFalse = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.byte, '"true false"'), pt.TealOp(None, pt.Op.err) ]) blockTrueBranch = pt.TealConditionalBlock([]) blockTrueBranch.setTrueBlock(blockTrueTrue) blockTrueBranch.setFalseBlock(blockTrueFalse) blockTrue = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')]) blockTrue.setNextBlock(blockTrueBranch) blockFalse = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')]) blockFalse.setNextBlock(blockEnd) block = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)]) block.setTrueBlock(blockTrue) block.setFalseBlock(blockFalse) block.addIncoming() block.validateTree() blocks = [ block, blockFalse, blockTrue, blockTrueBranch, blockTrueFalse, blockTrueTrue, blockEnd, ] expected = [ pt.TealOp(None, pt.Op.int, 1), pt.TealOp(None, pt.Op.bnz, pt.LabelReference("l2")), pt.TealOp(None, pt.Op.byte, '"false"'), pt.TealOp(None, pt.Op.b, pt.LabelReference("l6")), pt.TealLabel(None, pt.LabelReference("l2")), pt.TealOp(None, pt.Op.byte, '"true"'), pt.TealOp(None, pt.Op.bnz, pt.LabelReference("l5")), pt.TealOp(None, pt.Op.byte, '"true false"'), pt.TealOp(None, pt.Op.err), pt.TealLabel(None, pt.LabelReference("l5")), pt.TealOp(None, pt.Op.byte, '"true true"'), pt.TealLabel(None, pt.LabelReference("l6")), pt.TealOp(None, pt.Op.return_), ] actual = flattenBlocks(blocks) assert actual == expected
def test_flattenBlocks_single_many(): blocks = [ pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.int, 1), pt.TealOp(None, pt.Op.int, 2), pt.TealOp(None, pt.Op.int, 3), pt.TealOp(None, pt.Op.add), pt.TealOp(None, pt.Op.add), ]) ] expected = [ pt.TealOp(None, pt.Op.int, 1), pt.TealOp(None, pt.Op.int, 2), pt.TealOp(None, pt.Op.int, 3), pt.TealOp(None, pt.Op.add), pt.TealOp(None, pt.Op.add), ] actual = flattenBlocks(blocks) assert actual == expected
def test_flattenBlocks_sequence(): block5 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 5)]) block4 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 4)]) block4.setNextBlock(block5) block3 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 3)]) block3.setNextBlock(block4) block2 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 2)]) block2.setNextBlock(block3) block1 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 1)]) block1.setNextBlock(block2) block1.addIncoming() block1.validateTree() blocks = [block1, block2, block3, block4, block5] expected = [ pt.TealOp(None, pt.Op.int, 1), pt.TealOp(None, pt.Op.int, 2), pt.TealOp(None, pt.Op.int, 3), pt.TealOp(None, pt.Op.int, 4), pt.TealOp(None, pt.Op.int, 5), ] actual = flattenBlocks(blocks) assert actual == expected