templates.test_lists

  1# Lists - Example for illustrative purposes only.
  2
  3import smartpy as sp
  4
  5
  6@sp.module
  7def main():
  8    class TestLists(sp.Contract):
  9        def __init__(self):
 10            self.data.a = None
 11            self.data.b = 0
 12            self.data.c = ""
 13            self.data.d = 0
 14            self.data.e = ""
 15            self.data.f = []
 16            self.data.g = []
 17            self.data.head = "no head"
 18            self.data.tail = ["no tail"]
 19            self.data.total = 0
 20            self.data.xs2 = []
 21
 22        @sp.entrypoint
 23        def test(self, params):
 24            result = sp.record(
 25                l=params.l,
 26                lr=reversed(params.l),
 27                # from maps
 28                mi=params.m.items(),
 29                mir=reversed(params.m.items()),
 30                mk=params.m.keys(),
 31                mkr=reversed(params.m.keys()),
 32                mv=params.m.values(),
 33                mvr=reversed(params.m.values()),
 34                # from sets
 35                s=params.s.elements(),
 36                sr=reversed(params.s.elements()),
 37            )
 38            self.data.a = sp.Some(result)
 39            self.data.b = sum(result.l)
 40            self.data.c = sp.concat(result.mk)  # string list concatenation
 41            self.data.d = sum(result.sr)  # int    list sum
 42            self.data.e = ""
 43
 44            # iterations
 45            for x in result.mv:
 46                if sp.snd(x):
 47                    self.data.e += sp.fst(x)
 48
 49            # ranges
 50            for i in range(0, 5):
 51                self.data.f.push(i * i)
 52
 53            self.data.g = range(1, 12)
 54
 55        @sp.private(with_storage="read-write", with_operations=True)
 56        def f(self, param):
 57            (x, to_) = param
 58            self.data.total += x
 59            sp.send(to_, sp.mul(sp.tez(1), x))
 60            return sp.cast(2 * x, sp.nat)
 61
 62        @sp.entrypoint
 63        def test_comprehensions(self, to_):
 64            xs = [1, 2, 3]
 65            c = 1
 66            assert sp.pack([x + c for x in xs]) == sp.pack([2, 3, 4])
 67            c = 2
 68            assert sp.pack([x + c for x in xs]) == sp.pack([3, 4, 5])
 69            self.data.xs2 = [self.f((n, to_)) for n in xs]
 70
 71        # @sp.entrypoint
 72        # def test_match(self, params):
 73        #     with sp.match_cons(params) as x1:
 74        #         self.data.head = x1.head
 75        #         self.data.tail = x1.tail
 76        #     else:
 77        #         self.data.head = "abc"
 78
 79        # @sp.entrypoint
 80        # def test_match2(self, params):
 81        #     with sp.match_cons(params) as x1:
 82        #         with sp.match_cons(x1.tail) as x2:
 83        #             self.data.head = x1.head + x2.head
 84        #             self.data.tail = x2.tail
 85        #     sp.else:
 86        #         self.data.head = "abc"
 87
 88    class R(sp.Contract):
 89        @sp.entrypoint
 90        def default(self):
 91            pass
 92
 93
 94@sp.add_test()
 95def test():
 96    scenario = sp.test_scenario("Lists", main)
 97    r = main.R()
 98    scenario.h1("Helper contract")
 99    scenario += r
100
101    c1 = main.TestLists()
102    scenario.h1("Lists")
103    scenario += c1
104
105    c1.test(
106        l=[1, 2, 3],
107        m={"a": ("aa", True), "b": ("bb", False), "c": ("cc", True)},
108        s=sp.set([1, 12, 100]),
109    )
110
111    # c1.test_match(['1', '2', '3'])
112
113    # TODO: active this one when test_match is activated
114    # target = sp.record(a = sp.Some(
115    #     sp.record(l   = sp.list([1, 2, 3]),
116    #               lr  = sp.list([3, 2, 1]),
117    #               mi  =  sp.list([sp.record(key = 'a', value = ('aa', True)),
118    #                               sp.record(key = 'b', value = ('bb', False)),
119    #                               sp.record(key = 'c', value = ('cc', True))]),
120    #               mir = sp.list([sp.record(key = 'c', value = ('cc', True)),
121    #                              sp.record(key = 'b', value = ('bb', False)),
122    #                              sp.record(key = 'a', value = ('aa', True))]),
123    #               mk  = sp.list(['a', 'b', 'c']),
124    #               mkr = sp.list(['c', 'b', 'a']),
125    #               mv  = sp.list([('aa', True), ('bb', False), ('cc', True)]),
126    #               mvr = sp.list([('cc', True), ('bb', False), ('aa', True)]),
127    #               s   = sp.list([1, 12, 100]),
128    #               sr  = sp.list([100, 12, 1]))),
129    #                    b = 6,
130    #                    c = 'abc',
131    #                    d = 113,
132    #                    e = 'aacc',
133    #                    f = [16, 9, 4, 1, 0],
134    #                    g = range(1, 12),
135    #                    head = "1",
136    #                    tail = ["2", "3"],
137    #                    )
138    target = sp.record(
139        a=sp.Some(
140            sp.record(
141                l=sp.list([1, 2, 3]),
142                lr=sp.list([3, 2, 1]),
143                mi=sp.list(
144                    [
145                        sp.record(key="a", value=("aa", True)),
146                        sp.record(key="b", value=("bb", False)),
147                        sp.record(key="c", value=("cc", True)),
148                    ]
149                ),
150                mir=sp.list(
151                    [
152                        sp.record(key="c", value=("cc", True)),
153                        sp.record(key="b", value=("bb", False)),
154                        sp.record(key="a", value=("aa", True)),
155                    ]
156                ),
157                mk=sp.list(["a", "b", "c"]),
158                mkr=sp.list(["c", "b", "a"]),
159                mv=sp.list([("aa", True), ("bb", False), ("cc", True)]),
160                mvr=sp.list([("cc", True), ("bb", False), ("aa", True)]),
161                s=sp.list([1, 12, 100]),
162                sr=sp.list([100, 12, 1]),
163            )
164        ),
165        b=6,
166        c="abc",
167        d=113,
168        e="aacc",
169        f=[16, 9, 4, 1, 0],
170        g=range(1, 12),
171        head="no head",
172        tail=["no tail"],
173        total=0,
174        xs2=[],
175    )
176
177    scenario.verify_equal(c1.data, target)
178
179    # c1.test_match2(['1', '2', '3'])
180
181    # scenario.verify_equal(c1.data.tail, ['3'])
182    # scenario.verify_equal(c1.data.head, '12')
183
184    c1.test_comprehensions(r.address, _amount=sp.tez(6))
185    scenario.verify_equal(c1.data.xs2, [2, 4, 6])
186    scenario.verify(c1.data.total == 6)
187    scenario.verify(r.balance == sp.tez(6))