templates.test_bitwise

  1import smartpy as sp
  2
  3
  4@sp.module
  5def main():
  6    class Test(sp.Contract):
  7        def __init__(self):
  8            self.data.n = sp.nat(1234567)
  9            self.data.i = sp.int(3)
 10            self.data.b = sp.bytes("0xf6")
 11
 12        @sp.entrypoint
 13        def test_and(self, x):
 14            self.data.n = self.data.n & x
 15
 16        @sp.entrypoint
 17        def test_or(self, x):
 18            self.data.n = self.data.n | x
 19
 20        @sp.entrypoint
 21        def test_exclusive_or(self, x):
 22            self.data.n = self.data.n ^ x
 23
 24        @sp.entrypoint
 25        def test_shift_right(self, x):
 26            self.data.n = self.data.n >> x
 27
 28        @sp.entrypoint
 29        def test_shift_left(self, x):
 30            self.data.n = self.data.n << x
 31
 32        @sp.entrypoint
 33        def test_invert(self, params):
 34            sp.cast(params.b, sp.bytes)
 35            sp.cast(params.i, sp.int)
 36            self.data.b = sp.invert_bytes(params.b)
 37            self.data.i = ~params.i
 38
 39        @sp.entrypoint
 40        def test_nat_to_bytes(self, x):
 41            sp.cast(x, sp.nat)
 42            self.data.b = sp.to_bytes(x)
 43
 44        @sp.entrypoint
 45        def test_bytes_to_nat(self, x):
 46            sp.cast(x, sp.bytes)
 47            self.data.n = sp.to_nat(x)
 48
 49        @sp.entrypoint
 50        def test_int_to_bytes(self, x):
 51            sp.cast(x, sp.int)
 52            self.data.b = sp.to_bytes(x)
 53
 54        @sp.entrypoint
 55        def test_bytes_to_int(self, x):
 56            sp.cast(x, sp.bytes)
 57            self.data.i = sp.to_int(x)
 58
 59        @sp.entrypoint
 60        def test_shift_left_bytes(self, x):
 61            self.data.b = sp.lshift_bytes(self.data.b, x)
 62
 63        @sp.entrypoint
 64        def test_shift_right_bytes(self, x):
 65            self.data.b = sp.rshift_bytes(self.data.b, x)
 66
 67        @sp.entrypoint
 68        def test_and_bytes(self, x):
 69            self.data.b = sp.and_bytes(self.data.b, x)
 70
 71        @sp.entrypoint
 72        def test_or_bytes(self, x):
 73            self.data.b = sp.or_bytes(self.data.b, x)
 74
 75        @sp.entrypoint
 76        def test_xor_bytes(self, x):
 77            self.data.b = sp.xor_bytes(self.data.b, x)
 78
 79
 80@sp.add_test()
 81def test():
 82    scenario = sp.test_scenario("test", main)
 83    c1 = main.Test()
 84    scenario += c1
 85    c1.test_and(10)
 86    scenario.verify(c1.data.n == 2)
 87    c1.test_or(10)
 88    scenario.verify(c1.data.n == 10)
 89    c1.test_exclusive_or(9)
 90    scenario.verify(c1.data.n == 3)
 91    c1.test_shift_left(10)
 92    scenario.verify(c1.data.n == 3072)
 93    c1.test_shift_right(10)
 94    scenario.verify(c1.data.n == 3)
 95
 96    c1.test_invert(sp.record(b=sp.bytes("0xf6"), i=100))
 97    scenario.verify(c1.data.b == sp.bytes("0x09"))
 98    scenario.verify(c1.data.i == -101)
 99
100    c1.test_invert(sp.record(b=sp.bytes("0x00"), i=0))
101    scenario.verify(c1.data.b == sp.bytes("0xff"))
102    scenario.verify(c1.data.i == -1)
103
104    c1.test_invert(sp.record(b=sp.bytes("0x80"), i=-100))
105    scenario.verify(c1.data.b == sp.bytes("0x7f"))
106    scenario.verify(c1.data.i == 99)
107
108    c1.test_nat_to_bytes(0)
109    scenario.verify(c1.data.b == sp.bytes("0x"))
110
111    c1.test_nat_to_bytes(255)
112    scenario.verify(c1.data.b == sp.bytes("0xff"))
113
114    c1.test_nat_to_bytes(256)
115    scenario.verify(c1.data.b == sp.bytes("0x0100"))
116
117    c1.test_bytes_to_nat(sp.bytes("0x7f"))
118    scenario.verify(c1.data.n == 127)
119
120    c1.test_bytes_to_nat(sp.bytes("0xff"))
121    scenario.verify(c1.data.n == 255)
122
123    c1.test_bytes_to_nat(sp.bytes("0x00ff"))
124    scenario.verify(c1.data.n == 255)
125
126    c1.test_bytes_to_nat(sp.bytes("0x000000ff"))
127    scenario.verify(c1.data.n == 255)
128
129    c1.test_int_to_bytes(0)
130    scenario.verify(c1.data.b == sp.bytes("0x"))
131
132    c1.test_int_to_bytes(255)
133    scenario.verify(c1.data.b == sp.bytes("0x00ff"))
134
135    c1.test_int_to_bytes(256)
136    scenario.verify(c1.data.b == sp.bytes("0x0100"))
137
138    c1.test_int_to_bytes(-5)
139    scenario.verify(c1.data.b == sp.bytes("0xfb"))
140
141    c1.test_int_to_bytes(-260)
142    scenario.verify(c1.data.b == sp.bytes("0xfefc"))
143
144    c1.test_int_to_bytes(127)
145    scenario.verify(c1.data.b == sp.bytes("0x7f"))
146
147    c1.test_int_to_bytes(128)
148    scenario.verify(c1.data.b == sp.bytes("0x0080"))
149
150    c1.test_int_to_bytes(-127)
151    scenario.verify(c1.data.b == sp.bytes("0x81"))
152
153    c1.test_int_to_bytes(-128)
154    scenario.verify(c1.data.b == sp.bytes("0x80"))
155
156    c1.test_bytes_to_int(sp.bytes("0xff"))
157    scenario.verify(c1.data.i == -1)
158
159    c1.test_bytes_to_int(sp.bytes("0x7f"))
160    scenario.verify(c1.data.i == 127)
161
162    c1.test_bytes_to_int(sp.bytes("0x007f"))
163    scenario.verify(c1.data.i == 127)
164
165    c1.test_bytes_to_int(sp.bytes("0x0000007f"))
166    scenario.verify(c1.data.i == 127)
167
168    c1.test_shift_left_bytes(10)
169    scenario.verify(c1.data.b == sp.bytes("0x020000"))
170
171    c1.test_shift_right_bytes(10)
172    scenario.verify(c1.data.b == sp.bytes("0x80"))
173
174    c1.test_and_bytes(sp.bytes("0x01"))
175    scenario.verify(c1.data.b == sp.bytes("0x00"))
176
177    c1.test_or_bytes(sp.bytes("0xf6"))
178    scenario.verify(c1.data.b == sp.bytes("0xf6"))
179
180    c1.test_xor_bytes(sp.bytes("0xf5"))  # 0b11110110 `xor` 0b11110101
181    scenario.verify(c1.data.b == sp.bytes("0x03"))