templates.test_for

  1import smartpy as sp
  2
  3
  4@sp.module
  5def main():
  6    def matrix(vms):
  7        mm = {}
  8        km = 0
  9        for vvs in vms:
 10            mv = {}
 11            kv = 0
 12            for vv in vvs:
 13                mv[kv] = vv
 14                kv += 1
 15            mm[km] = mv
 16            km += 1
 17        return mm
 18
 19    def cube(vcs):
 20        mc = {}
 21        kc = 0
 22        for vms in vcs:
 23            mm = {}
 24            km = 0
 25            for vvs in vms:
 26                mv = {}
 27                kv = 0
 28                for vv in vvs:
 29                    mv[kv] = vv
 30                    kv += 1
 31                mm[kv] = mv
 32                km += 1
 33            mc[kc] = mm
 34        return mc
 35
 36    class C(sp.Contract):
 37        def __init__(self):
 38            self.data.x = 0
 39            self.data.xxs = matrix([[0]])
 40            self.data.xxxs = cube([[[0]]])
 41            self.data.rs = [sp.record(a=5, b=6)]
 42            self.data.ms = [{"a": 97, "b": 98}, {"a": 0, "b": 1}]
 43
 44        @sp.entrypoint
 45        def ep1(self):
 46            self.data.x = 0
 47            r = sp.record(a=2, b=3)
 48            s = sp.record(a=5, b=6)
 49            for i in [r, s]:
 50                self.data.x += i.a  # read record attribute
 51            assert self.data.x == 7
 52
 53        @sp.entrypoint
 54        def ep2(self):
 55            self.data.x = 2
 56            for i in [self.data.x, self.data.x, self.data.x]:
 57                self.data.x += i  # write to something mentioned in the list (no effect)
 58            assert self.data.x == 8
 59
 60        @sp.entrypoint
 61        def ep3(self):
 62            r = sp.record(a=2, b=3)
 63            self.data.rs = [r, r, r]
 64            for i in self.data.rs:
 65                i.a = 0  # write to record attribute
 66            # r = sp.record(a=0,b=3)
 67            # sp.verify(self.data.rs == [r,r,r])
 68
 69        @sp.entrypoint
 70        def ep4(self):
 71            xs = [2, 3]
 72            self.data.xxs = matrix([xs, xs, xs])
 73            for xs_ in self.data.xxs.values():
 74                xs_[0] = 0  # write to sub-list
 75                sp.send(
 76                    sp.address("tz1PiDHTNJXhqpkbRUYNZEzmePNd21WcB8yB"),
 77                    utils.nat_to_mutez(xs_[1]),
 78                )
 79            xs = [0, 3]
 80            # sp.verify(self.data.xxs == [xs,xs,xs])
 81
 82        @sp.entrypoint
 83        def ep5(self):
 84            self.data.x = 0
 85            self.data.xxxs = cube([[[1]], [[2]]])
 86            for xs in self.data.xxxs[self.data.x].values():
 87                self.data.x = 1
 88                xs[0] = 0
 89            # sp.verify(self.data.xxxs == [[[0]],[[2]]])
 90
 91        @sp.entrypoint
 92        def ep6(self):
 93            self.data.xxxs = cube([[[0], [10]], [[20], [30]]])
 94            for xs in self.data.xxxs[self.data.xxxs[0][0][0]].values():
 95                xs[0] = 1
 96            # sp.verify(self.data.xxxs == [[[1],[1]],[[20],[30]]])
 97
 98        @sp.entrypoint
 99        def ep7(self):
100            self.data.ms = [{"a": 97, "b": 98}, {"a": 0, "b": 1}]
101            for m in self.data.ms:
102                del m["a"]
103            # assert self.data.ms == [{'b': 98}, {'b': 1}]
104
105
106@sp.add_test()
107def test():
108    s = sp.test_scenario("For", [sp.utils, main])
109    c = main.C()
110    s += c
111    c.ep1()
112    c.ep2()
113    c.ep3()
114    # c.ep4()
115    # c.ep5()
116    # c.ep6()
117    c.ep7()