|
| 1 | +import typing |
| 2 | + |
| 3 | +import pytest |
| 4 | +from algopy import Account, Application, Asset, Bytes, String, UInt64, arc4, size_of |
| 5 | + |
| 6 | + |
| 7 | +class Swapped(arc4.Struct): |
| 8 | + a: arc4.UInt64 |
| 9 | + b: arc4.Bool |
| 10 | + c: arc4.Tuple[arc4.UInt64, arc4.Bool, arc4.Bool] |
| 11 | + d: arc4.StaticArray[arc4.Bool, typing.Literal[10]] |
| 12 | + e: arc4.Tuple[arc4.UInt64, arc4.StaticArray[arc4.UInt64, typing.Literal[3]]] |
| 13 | + |
| 14 | + |
| 15 | +class WhatsMySize(typing.NamedTuple): |
| 16 | + foo: UInt64 |
| 17 | + bar: bool |
| 18 | + baz: Swapped |
| 19 | + |
| 20 | + |
| 21 | +class MyTuple(typing.NamedTuple): |
| 22 | + foo: UInt64 |
| 23 | + bar: bool |
| 24 | + baz: bool |
| 25 | + |
| 26 | + |
| 27 | +class MyDynamicSizedTuple(typing.NamedTuple): |
| 28 | + foo: UInt64 |
| 29 | + bar: String |
| 30 | + |
| 31 | + |
| 32 | +def test_size_of() -> None: |
| 33 | + x = arc4.UInt64(0) |
| 34 | + assert size_of(x) == 8 |
| 35 | + assert size_of(arc4.UInt64) == 8 |
| 36 | + assert size_of(UInt64) == 8 |
| 37 | + assert size_of(arc4.Address) == 32 |
| 38 | + assert size_of(Account) == 32 |
| 39 | + assert size_of(Application) == 8 |
| 40 | + assert size_of(Asset) == 8 |
| 41 | + assert size_of(bool) == 8 |
| 42 | + assert size_of(tuple[bool]) == 1 |
| 43 | + assert size_of(tuple[bool, bool, bool, bool, bool, bool, bool, bool]) == 1 |
| 44 | + assert size_of(tuple[bool, bool, bool, bool, bool, bool, bool, bool, bool]) == 2 |
| 45 | + assert size_of(tuple[arc4.UInt64, UInt64, bool, arc4.Bool]) == 17 |
| 46 | + assert size_of(arc4.Tuple[arc4.UInt64, arc4.Bool, arc4.Bool] == 9) |
| 47 | + assert size_of(MyTuple) == 9 |
| 48 | + assert size_of(WhatsMySize) == 61 |
| 49 | + assert size_of(arc4.StaticArray[arc4.Byte, typing.Literal[7]]) == 7 |
| 50 | + assert size_of(arc4.StaticArray(arc4.Byte(), arc4.Byte())) == 2 |
| 51 | + assert size_of(Swapped) == 52 |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize( |
| 55 | + "typ", |
| 56 | + [ |
| 57 | + arc4.StaticArray[arc4.DynamicBytes, typing.Literal[7]], |
| 58 | + tuple[arc4.DynamicBytes, Bytes], |
| 59 | + arc4.Tuple[arc4.UInt64, arc4.String], |
| 60 | + MyDynamicSizedTuple, |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_size_of_dynamic(typ: type) -> None: |
| 64 | + with pytest.raises(ValueError, match="is dynamically sized"): |
| 65 | + size_of(typ) |
0 commit comments