templates.test_list_utils

  1import smartpy as sp
  2
  3
  4@sp.module
  5def M():
  6    class C(sp.Contract):
  7        def __init__(self):
  8            pass
  9
 10        @sp.private
 11        def test_replicate(self):
 12            assert sp.len(list_utils.replicate((0, 5))) == 5
 13            assert sp.pack(list_utils.replicate((0, 5))) == sp.pack([0, 0, 0, 0, 0])
 14
 15        @sp.private
 16        def test_element_at(self):
 17            assert list_utils.element_at(([1, 2, 3], 0)) == 1
 18            assert list_utils.element_at(([1, 2, 3], 1)) == 2
 19            assert list_utils.element_at(([1, 2, 3], 2)) == 3
 20
 21        @sp.private
 22        def test_insertion_sort(self):
 23            assert sp.len(list_utils.insertion_sort(([1, 3, 5, 7, 2, 4, 6, 8]))) == 8
 24            assert (
 25                list_utils.element_at(
 26                    (list_utils.insertion_sort([1, 3, 5, 7, 2, 4, 6, 8]), 0)
 27                )
 28                == 1
 29            )
 30            assert (
 31                list_utils.element_at(
 32                    (list_utils.insertion_sort([1, 3, 5, 7, 2, 4, 6, 8]), 1)
 33                )
 34                == 2
 35            )
 36            assert (
 37                list_utils.element_at(
 38                    (list_utils.insertion_sort([1, 3, 5, 7, 2, 4, 6, 8]), 2)
 39                )
 40                == 3
 41            )
 42            assert (
 43                list_utils.element_at(
 44                    (list_utils.insertion_sort([1, 3, 5, 7, 2, 4, 6, 8]), 3)
 45                )
 46                == 4
 47            )
 48            assert (
 49                list_utils.element_at(
 50                    (list_utils.insertion_sort([1, 3, 5, 7, 2, 4, 6, 8]), 4)
 51                )
 52                == 5
 53            )
 54            assert (
 55                list_utils.element_at(
 56                    (list_utils.insertion_sort([1, 3, 5, 7, 2, 4, 6, 8]), 5)
 57                )
 58                == 6
 59            )
 60            assert (
 61                list_utils.element_at(
 62                    (list_utils.insertion_sort([1, 3, 5, 7, 2, 4, 6, 8]), 6)
 63                )
 64                == 7
 65            )
 66            assert (
 67                list_utils.element_at(
 68                    (list_utils.insertion_sort([1, 3, 5, 7, 2, 4, 6, 8]), 7)
 69                )
 70                == 8
 71            )
 72
 73        @sp.private
 74        def test_sort(self):
 75            assert sp.len(list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8]))) == 8
 76            assert (
 77                list_utils.element_at((list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8])), 0))
 78                == 1
 79            )
 80            assert (
 81                list_utils.element_at((list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8])), 1))
 82                == 2
 83            )
 84            assert (
 85                list_utils.element_at((list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8])), 2))
 86                == 3
 87            )
 88            assert (
 89                list_utils.element_at((list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8])), 3))
 90                == 4
 91            )
 92            assert (
 93                list_utils.element_at((list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8])), 4))
 94                == 5
 95            )
 96            assert (
 97                list_utils.element_at((list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8])), 5))
 98                == 6
 99            )
100            assert (
101                list_utils.element_at((list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8])), 6))
102                == 7
103            )
104            assert (
105                list_utils.element_at((list_utils.sort(([1, 3, 5, 7, 2, 4, 6, 8])), 7))
106                == 8
107            )
108
109        @sp.private
110        def test_sub_list(self):
111            assert sp.pack(list_utils.sub_list(([1, 2, 3, 4, 5], 1, 2))) == sp.pack(
112                [2, 3]
113            )
114            assert sp.pack(list_utils.sub_list(([1, 2, 3, 4, 5], 1, 3))) == sp.pack(
115                [2, 3, 4]
116            )
117            assert sp.pack(list_utils.sub_list(([1, 2, 3, 4, 5], 1, 4))) == sp.pack(
118                [2, 3, 5]
119            )
120            assert sp.pack(list_utils.sub_list(([1, 2, 3, 4, 5], 1, 5))) == sp.pack(
121                [2, 3, 4, 5]
122            )
123
124        @sp.private
125        def test_update_list(self):
126            assert sp.pack(
127                list_utils.update_list(([1, 2, 3, 4, 5], 0, 100))
128            ) == sp.pack([100, 2, 3, 4, 5])
129            assert sp.pack(
130                list_utils.update_list(([1, 2, 3, 4, 5], 1, 100))
131            ) == sp.pack([1, 100, 3, 4, 5])
132            assert sp.pack(
133                list_utils.update_list(([1, 2, 3, 4, 5], 2, 100))
134            ) == sp.pack([1, 2, 100, 4, 5])
135            assert sp.pack(
136                list_utils.update_list(([1, 2, 3, 4, 5], 3, 100))
137            ) == sp.pack([1, 2, 3, 100, 5])
138            assert sp.pack(
139                list_utils.update_list(([1, 2, 3, 4, 5], 4, 100))
140            ) == sp.pack([1, 2, 3, 4, 100])
141
142        @sp.private
143        def test_merge_sort(self):
144            assert sp.len(list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8]))) == 8
145            assert (
146                list_utils.element_at(
147                    (list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8])), 0)
148                )
149                == 1
150            )
151            assert (
152                list_utils.element_at(
153                    (list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8])), 1)
154                )
155                == 2
156            )
157            assert (
158                list_utils.element_at(
159                    (list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8])), 2)
160                )
161                == 3
162            )
163            assert (
164                list_utils.element_at(
165                    (list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8])), 3)
166                )
167                == 4
168            )
169            assert (
170                list_utils.element_at(
171                    (list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8])), 4)
172                )
173                == 5
174            )
175            assert (
176                list_utils.element_at(
177                    (list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8])), 5)
178                )
179                == 6
180            )
181            assert (
182                list_utils.element_at(
183                    (list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8])), 6)
184                )
185                == 7
186            )
187            assert (
188                list_utils.element_at(
189                    (list_utils.merge_sort(([1, 3, 5, 7, 2, 4, 6, 8])), 7)
190                )
191                == 8
192            )
193
194        @sp.private
195        def test_quick_select(self):
196            assert list_utils.quick_select(([10, 4, 5, 8, 6, 11, 26], 0)) == 4
197            assert list_utils.quick_select(([10, 4, 5, 8, 6, 11, 26], 1)) == 5
198            assert list_utils.quick_select(([10, 4, 5, 8, 6, 11, 26], 2)) == 6
199            assert list_utils.quick_select(([10, 4, 5, 8, 6, 11, 26], 3)) == 8
200            assert list_utils.quick_select(([10, 4, 5, 8, 6, 11, 26], 4)) == 10
201            assert list_utils.quick_select(([10, 4, 5, 8, 6, 11, 26], 5)) == 11
202            assert list_utils.quick_select(([10, 4, 5, 8, 6, 11, 26], 6)) == 26
203
204        @sp.entrypoint
205        def ep(self):
206            self.test_replicate()
207            self.test_element_at()
208            self.test_update_list()
209            self.test_insertion_sort()
210            self.test_sort()
211            self.test_merge_sort()
212            self.test_quick_select()
213
214
215@sp.add_test()
216def test():
217    s = sp.test_scenario("Test", [sp.list_utils, M])
218    c = M.C()
219    s += c
220    c.ep()