def __test_single(expr: pt.MultiValue): assert expr.output_slots[0] != expr.output_slots[1] with pt.TealComponent.Context.ignoreExprEquality(): assert expr.output_slots[0].load().__teal__(options) == pt.ScratchLoad( expr.output_slots[0]).__teal__(options) with pt.TealComponent.Context.ignoreExprEquality(): assert expr.output_slots[1].load().__teal__(options) == pt.ScratchLoad( expr.output_slots[1]).__teal__(options) assert expr.type_of() == pt.TealType.none
def test_scratch_load_default(): slot = pt.ScratchSlot() expr = pt.ScratchLoad(slot) assert expr.type_of() == pt.TealType.anytype expected = pt.TealSimpleBlock([pt.TealOp(expr, pt.Op.load, slot)]) actual, _ = expr.__teal__(options) assert actual == expected
def test_scratch_load_type(): for type in (pt.TealType.uint64, pt.TealType.bytes, pt.TealType.anytype): slot = pt.ScratchSlot() expr = pt.ScratchLoad(slot, type) assert expr.type_of() == type expected = pt.TealSimpleBlock([pt.TealOp(expr, pt.Op.load, slot)]) actual, _ = expr.__teal__(options) assert actual == expected
def test_scratch_load_index_expression(): expr = pt.ScratchLoad(slot=None, index_expression=pt.Int(1337)) assert expr.type_of() == pt.TealType.anytype expected = pt.TealSimpleBlock([pt.TealOp(pt.Int(1337), pt.Op.int, 1337)]) expected.setNextBlock(pt.TealSimpleBlock([pt.TealOp(None, pt.Op.loads)])) actual, _ = expr.__teal__(options) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_dynamic_scratchvar_load(): myvar = pt.DynamicScratchVar() expr = myvar.load() expected = pt.TealSimpleBlock( [ pt.TealOp(pt.ScratchLoad(myvar.slot), pt.Op.load, myvar.slot), pt.TealOp(expr, pt.Op.loads), ] ) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_dynamic_scratchvar_store(): myvar = pt.DynamicScratchVar(pt.TealType.bytes) arg = pt.Bytes("value") expr = myvar.store(arg) expected = pt.TealSimpleBlock( [ pt.TealOp(pt.ScratchLoad(myvar.slot), pt.Op.load, myvar.slot), pt.TealOp(arg, pt.Op.byte, '"value"'), pt.TealOp(expr, pt.Op.stores), ] ) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_scratch_slot(): slot = pt.ScratchSlot() assert slot == slot assert slot.__hash__() == slot.__hash__() assert slot != pt.ScratchSlot() with pt.TealComponent.Context.ignoreExprEquality(): assert ( slot.store().__teal__(options)[0] == pt.ScratchStackStore(slot).__teal__(options)[0] ) assert ( slot.store(pt.Int(1)).__teal__(options)[0] == pt.ScratchStore(slot, pt.Int(1)).__teal__(options)[0] ) assert slot.load().type_of() == pt.TealType.anytype assert slot.load(pt.TealType.uint64).type_of() == pt.TealType.uint64 assert ( slot.load().__teal__(options)[0] == pt.ScratchLoad(slot).__teal__(options)[0] )
def test_maybe_value(): ops = ( pt.Op.app_global_get_ex, pt.Op.app_local_get_ex, pt.Op.asset_holding_get, pt.Op.asset_params_get, ) types = (pt.TealType.uint64, pt.TealType.bytes, pt.TealType.anytype) immedate_argv = ([], ["AssetFrozen"]) argv = ([], [pt.Int(0)], [pt.Int(1), pt.Int(2)]) for op in ops: for type in types: for iargs in immedate_argv: for args in argv: expr = pt.MaybeValue(op, type, immediate_args=iargs, args=args) assert expr.slotOk != expr.slotValue assert expr.output_slots == [expr.slotValue, expr.slotOk] assert expr.hasValue().type_of() == pt.TealType.uint64 with pt.TealComponent.Context.ignoreExprEquality(): assert expr.hasValue().__teal__( options) == pt.ScratchLoad( expr.slotOk).__teal__(options) assert expr.value().type_of() == type with pt.TealComponent.Context.ignoreExprEquality(): assert expr.value().__teal__( options) == pt.ScratchLoad( expr.slotValue).__teal__(options) assert expr.type_of() == pt.TealType.none expected_call = pt.TealSimpleBlock([ pt.TealOp(expr, op, *iargs), pt.TealOp(None, pt.Op.store, expr.slotOk), pt.TealOp(None, pt.Op.store, expr.slotValue), ]) if len(args) == 0: expected = expected_call elif len(args) == 1: expected, after_arg = args[0].__teal__(options) after_arg.setNextBlock(expected_call) elif len(args) == 2: expected, after_arg_1 = args[0].__teal__(options) arg_2, after_arg_2 = args[1].__teal__(options) after_arg_1.setNextBlock(arg_2) after_arg_2.setNextBlock(expected_call) expected.addIncoming() expected = pt.TealBlock.NormalizeBlocks(expected) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected