def test_StaticArray_set_computed(): value = abi.StaticArray(abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10)) computed = ContainerType(value.type_spec(), pt.Bytes("indeed this is hard to simulate")) expr = value.set(computed) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.byte, '"indeed this is hard to simulate"'), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = actual.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected with pytest.raises(pt.TealInputError): value.set( ContainerType( abi.StaticArrayTypeSpec(abi.Uint16TypeSpec(), 40), pt.Bytes("well i am trolling"), ))
def test_StaticArray_set_copy(): value = abi.StaticArray(abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10)) otherArray = abi.StaticArray( abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10)) with pytest.raises(pt.TealInputError): value.set( abi.StaticArray(abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 11))) with pytest.raises(pt.TealInputError): value.set( abi.StaticArray(abi.StaticArrayTypeSpec(abi.Uint8TypeSpec(), 10))) with pytest.raises(pt.TealInputError): value.set(abi.Uint64()) expr = value.set(otherArray) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.load, otherArray.stored_value.slot), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_StaticArrayTypeSpec_init(): for elementType in STATIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) assert staticArrayType.value_type_spec() is elementType assert not staticArrayType.is_length_dynamic() assert staticArrayType._stride() == elementType.byte_length_static( ) assert staticArrayType.length_static() == length with pytest.raises(TypeError): abi.StaticArrayTypeSpec(elementType, -1) for length in range(256): staticBytesType = abi.StaticBytesTypeSpec(length) assert isinstance(staticBytesType.value_type_spec(), abi.ByteTypeSpec) assert not staticBytesType.is_length_dynamic() assert staticBytesType._stride() == 1 assert staticBytesType.length_static() == length for elementType in DYNAMIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) assert staticArrayType.value_type_spec() is elementType assert not staticArrayType.is_length_dynamic() assert staticArrayType._stride() == 2 assert staticArrayType.length_static() == length with pytest.raises(TypeError): abi.StaticArrayTypeSpec(elementType, -1)
def test_StaticArrayTypeSpec_byte_length_static(): for elementType in STATIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) actual = staticArrayType.byte_length_static() if elementType == abi.BoolTypeSpec(): expected = _bool_sequence_length(length) else: expected = elementType.byte_length_static() * length assert (actual == expected ), "failed with element type {} and length {}".format( elementType, length) for length in range(256): staticBytesType = abi.StaticBytesTypeSpec(length) actual = staticBytesType.byte_length_static() assert ( actual == length ), f"failed with element type {staticBytesType.value_type_spec()} and length {length}" for elementType in DYNAMIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) with pytest.raises(ValueError): staticArrayType.byte_length_static()
def test_StaticArrayTypeSpec_is_dynamic(): for elementType in STATIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) assert not staticArrayType.is_dynamic() for length in range(256): assert not abi.StaticBytesTypeSpec(length).is_dynamic() for elementType in DYNAMIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) assert staticArrayType.is_dynamic()
def test_StaticArray_getitem(): for length in (0, 1, 2, 3, 1000): value = abi.StaticArray( abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), length)) for index in range(length): # dynamic indexes indexExpr = pt.Int(index) element = value[indexExpr] assert type(element) is abi.ArrayElement assert element.array is value assert element.index is indexExpr for index in range(length): # static indexes element = value[index] assert type(element) is abi.ArrayElement assert element.array is value assert type(element.index) is pt.Int assert element.index.value == index with pytest.raises(pt.TealInputError): value[-1] with pytest.raises(pt.TealInputError): value[length]
def test_StaticArrayTypeSpec_eq(): for elementType in STATIC_TYPES + DYNAMIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) assert staticArrayType == staticArrayType assert staticArrayType != abi.StaticArrayTypeSpec( elementType, length + 1) assert staticArrayType != abi.StaticArrayTypeSpec( abi.TupleTypeSpec(elementType), length) for length in range(256): staticBytesType = abi.StaticBytesTypeSpec(length) assert staticBytesType == staticBytesType assert staticBytesType != abi.StaticBytesTypeSpec(length + 1) assert staticBytesType != abi.StaticArrayTypeSpec( abi.TupleTypeSpec(abi.ByteTypeSpec()), length)
def test_StaticArray_set_values(): value = abi.StaticArray(abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10)) with pytest.raises(pt.TealInputError): value.set([]) with pytest.raises(pt.TealInputError): value.set([abi.Uint64()] * 9) with pytest.raises(pt.TealInputError): value.set([abi.Uint64()] * 11) with pytest.raises(pt.TealInputError): value.set([abi.Uint16()] * 10) with pytest.raises(pt.TealInputError): value.set([abi.Uint64()] * 9 + [abi.Uint16()]) values = [abi.Uint64() for _ in range(10)] expr = value.set(values) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expectedExpr = value.stored_value.store(_encode_tuple(values)) expected, _ = expectedExpr.__teal__(options) 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
def test_StaticArrayTypeSpec_str(): for elementType in STATIC_TYPES + DYNAMIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) assert str(staticArrayType) == "{}[{}]".format(elementType, length) for length in range(256): assert str(abi.StaticBytesTypeSpec(length)) == f"byte[{length}]"
def test_AddressTypeSpec_eq(): assert abi.AddressTypeSpec() == abi.AddressTypeSpec() for otherType in ( abi.ByteTypeSpec(), abi.StaticArrayTypeSpec(abi.ByteTypeSpec(), 32), abi.DynamicArrayTypeSpec(abi.ByteTypeSpec()), ): assert abi.AddressTypeSpec() != otherType
def test_BoolTypeSpec_eq(): assert abi.BoolTypeSpec() == abi.BoolTypeSpec() for otherType in ( abi.ByteTypeSpec, abi.Uint64TypeSpec, abi.StaticArrayTypeSpec(abi.BoolTypeSpec(), 1), abi.DynamicArrayTypeSpec(abi.BoolTypeSpec()), ): assert abi.BoolTypeSpec() != otherType
def test_StringTypeSpec_eq(): assert abi.StringTypeSpec() == abi.StringTypeSpec() for otherType in ( abi.ByteTypeSpec(), abi.StaticArrayTypeSpec(abi.ByteTypeSpec(), 1), abi.DynamicArrayTypeSpec(abi.Uint8TypeSpec()), abi.DynamicArrayTypeSpec(abi.ByteTypeSpec()), ): assert abi.StringTypeSpec() != otherType
def test_Address_set_StaticArray(): value_to_set = abi.StaticArray( abi.StaticArrayTypeSpec(abi.ByteTypeSpec(), abi.AddressLength.Bytes)) value = abi.Address() expr = value.set(value_to_set) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.load, value_to_set.stored_value.slot), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected with pytest.raises(pt.TealInputError): bogus = abi.StaticArray(abi.StaticArrayTypeSpec( abi.ByteTypeSpec(), 10)) value.set(bogus)
def test_StaticArray_encode(): value = abi.StaticArray(abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10)) expr = value.encode() assert expr.type_of() == pt.TealType.bytes assert not expr.has_return() expected = pt.TealSimpleBlock( [pt.TealOp(None, pt.Op.load, value.stored_value.slot)]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_StaticArrayTypeSpec_new_instance(): for elementType in STATIC_TYPES + DYNAMIC_TYPES: for length in range(256): staticArrayType = abi.StaticArrayTypeSpec(elementType, length) instance = staticArrayType.new_instance() assert isinstance( instance, abi.StaticArray, ) assert instance.type_spec() == staticArrayType for length in range(256): staticBytesType = abi.StaticBytesTypeSpec(length) instance = staticBytesType.new_instance() assert isinstance(instance, abi.StaticBytes) assert instance.type_spec() == staticBytesType
def test_StaticArray_length(): for length in (0, 1, 2, 3, 1000): value = abi.StaticArray( abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), length)) expr = value.length() assert expr.type_of() == pt.TealType.uint64 assert not expr.has_return() expected = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, length)]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_UintTypeSpec_eq(): for i, test in enumerate(testData): assert test.uintType == test.uintType for j, otherTest in enumerate(testData): if i == j: continue assert test.uintType != otherTest.uintType for otherType in ( abi.BoolTypeSpec(), abi.StaticArrayTypeSpec(test.uintType, 1), abi.DynamicArrayTypeSpec(test.uintType), ): assert test.uintType != otherType assert abi.ByteTypeSpec() != abi.Uint8TypeSpec() assert abi.Uint8TypeSpec() != abi.ByteTypeSpec()
def test_StaticArray_decode(): encoded = pt.Bytes("encoded") for start_index in (None, pt.Int(1)): for end_index in (None, pt.Int(2)): for length in (None, pt.Int(3)): value = abi.StaticArray( abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10)) if end_index is not None and length is not None: with pytest.raises(pt.TealInputError): value.decode( encoded, start_index=start_index, end_index=end_index, length=length, ) continue expr = value.decode(encoded, start_index=start_index, end_index=end_index, length=length) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expectedExpr = value.stored_value.store( substring_for_decoding( encoded, start_index=start_index, end_index=end_index, length=length, )) expected, _ = expectedExpr.__teal__(options) 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
def test_ArrayElement_store_into(): for elementType in STATIC_TYPES + DYNAMIC_TYPES: staticArrayType = abi.StaticArrayTypeSpec(elementType, 100) staticArray = staticArrayType.new_instance() index = pt.Int(9) element = abi.ArrayElement(staticArray, index) output = elementType.new_instance() expr = element.store_into(output) encoded = staticArray.encode() stride = pt.Int(staticArray.type_spec()._stride()) expectedLength = staticArray.length() if elementType == abi.BoolTypeSpec(): expectedExpr = cast(abi.Bool, output).decode_bit(encoded, index) elif not elementType.is_dynamic(): expectedExpr = output.decode(encoded, start_index=stride * index, length=stride) else: expectedExpr = output.decode( encoded, start_index=pt.ExtractUint16(encoded, stride * index), end_index=pt.If(index + pt.Int(1) == expectedLength).Then( pt.Len(encoded)).Else( pt.ExtractUint16(encoded, stride * index + pt.Int(2))), ) expected, _ = expectedExpr.__teal__(options) 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 with pytest.raises(pt.TealInputError): element.store_into(abi.Tuple(abi.TupleTypeSpec(elementType))) for elementType in STATIC_TYPES + DYNAMIC_TYPES: dynamicArrayType = abi.DynamicArrayTypeSpec(elementType) dynamicArray = dynamicArrayType.new_instance() index = pt.Int(9) element = abi.ArrayElement(dynamicArray, index) output = elementType.new_instance() expr = element.store_into(output) encoded = dynamicArray.encode() stride = pt.Int(dynamicArray.type_spec()._stride()) expectedLength = dynamicArray.length() if elementType == abi.BoolTypeSpec(): expectedExpr = cast(abi.Bool, output).decode_bit(encoded, index + pt.Int(16)) elif not elementType.is_dynamic(): expectedExpr = output.decode(encoded, start_index=stride * index + pt.Int(2), length=stride) else: expectedExpr = output.decode( encoded, start_index=pt.ExtractUint16( encoded, stride * index + pt.Int(2)) + pt.Int(2), end_index=pt.If(index + pt.Int(1) == expectedLength).Then( pt.Len(encoded)).Else( pt.ExtractUint16( encoded, stride * index + pt.Int(2) + pt.Int(2)) + pt.Int(2)), ) expected, _ = expectedExpr.__teal__(options) expected.addIncoming() expected = pt.TealBlock.NormalizeBlocks(expected) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): with pt.TealComponent.Context.ignoreScratchSlotEquality(): assert actual == expected assert pt.TealBlock.MatchScratchSlotReferences( pt.TealBlock.GetReferencedScratchSlots(actual), pt.TealBlock.GetReferencedScratchSlots(expected), ) with pytest.raises(pt.TealInputError): element.store_into(abi.Tuple(abi.TupleTypeSpec(elementType)))
import pyteal as pt from pyteal import abi options = pt.CompileOptions(version=5) STATIC_TYPES: List[abi.TypeSpec] = [ abi.BoolTypeSpec(), abi.Uint8TypeSpec(), abi.Uint16TypeSpec(), abi.Uint32TypeSpec(), abi.Uint64TypeSpec(), abi.TupleTypeSpec(), abi.TupleTypeSpec(abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.Uint64TypeSpec()), abi.StaticArrayTypeSpec(abi.BoolTypeSpec(), 10), abi.StaticArrayTypeSpec(abi.Uint8TypeSpec(), 10), abi.StaticArrayTypeSpec(abi.Uint16TypeSpec(), 10), abi.StaticArrayTypeSpec(abi.Uint32TypeSpec(), 10), abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10), abi.StaticArrayTypeSpec( abi.TupleTypeSpec(abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.Uint64TypeSpec()), 10, ), ] DYNAMIC_TYPES: List[abi.TypeSpec] = [ abi.DynamicArrayTypeSpec(abi.BoolTypeSpec()), abi.DynamicArrayTypeSpec(abi.Uint8TypeSpec()), abi.DynamicArrayTypeSpec(abi.Uint16TypeSpec()),