def test_break_fail(): with pytest.raises(pt.TealCompileError): pt.Break().__teal__(options) with pytest.raises(pt.TealCompileError): pt.If(pt.Int(1), pt.Break()).__teal__(options) with pytest.raises(pt.TealCompileError): pt.Seq([pt.Break()]).__teal__(options) with pytest.raises(TypeError): pt.Break(pt.Int(1))
def test_while_break(): i = pt.ScratchVar() i.store(pt.Int(0)) items = [ i.load() < pt.Int(2), i.store(i.load() + pt.Int(1)), pt.If(i.load() == pt.Int(1), pt.Break()), ] expr = pt.While(items[0]).Do(pt.Seq(items[1], items[2])) assert expr.type_of() == pt.TealType.none assert not expr.has_return() options.enterLoop() expected, condEnd = items[0].__teal__(options) do, doEnd = pt.Seq([items[1], items[2]]).__teal__(options) expectedBranch = pt.TealConditionalBlock([]) end = pt.TealSimpleBlock([]) expectedBranch.setTrueBlock(do) expectedBranch.setFalseBlock(end) condEnd.setNextBlock(expectedBranch) doEnd.setNextBlock(expected) breakBlocks, _ = options.exitLoop() for block in breakBlocks: block.setNextBlock(end) actual, _ = expr.__teal__(options) assert actual == expected
def test_continue_break(): i = pt.ScratchVar() expr = pt.For(i.store(pt.Int(0)), pt.Int(1), i.store(i.load() + pt.Int(1))).Do( pt.Seq([pt.If(pt.Int(1), pt.Break(), pt.Continue())]) ) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expr.__teal__(options)
def test_break(): expr = pt.Break() assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([]) options.enterLoop() actual, _ = expr.__teal__(options) breakBlocks, continueBlocks = options.exitLoop() assert actual == expected assert breakBlocks == [actual] assert continueBlocks == []
def test_for_break(): i = pt.ScratchVar() items = [ (i.store(pt.Int(0))), i.load() < pt.Int(10), i.store(i.load() + pt.Int(1)), pt.If(i.load() == pt.Int(6), pt.Break()), pt.App.globalPut(pt.Itob(i.load()), i.load() * pt.Int(2)), ] expr = pt.For(items[0], items[1], items[2]).Do(pt.Seq([items[3], items[4]])) assert expr.type_of() == pt.TealType.none assert not expr.has_return() options.enterLoop() expected, varEnd = items[0].__teal__(options) condStart, condEnd = items[1].__teal__(options) stepStart, stepEnd = items[2].__teal__(options) do, doEnd = pt.Seq([items[3], items[4]]).__teal__(options) expectedBranch = pt.TealConditionalBlock([]) end = pt.TealSimpleBlock([]) doEnd.setNextBlock(stepStart) stepEnd.setNextBlock(condStart) expectedBranch.setTrueBlock(do) expectedBranch.setFalseBlock(end) condEnd.setNextBlock(expectedBranch) varEnd.setNextBlock(condStart) breakBlocks, _ = options.exitLoop() for block in breakBlocks: block.setNextBlock(end) actual, _ = expr.__teal__(options) assert actual == expected
def test_continue_break(): expr = pt.While(pt.Int(0)).Do( pt.Seq([pt.If(pt.Int(1), pt.Break(), pt.Continue())])) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expr.__teal__(options)