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_rational(self):
12 assert rational.mk((3, 5)) == sp.record(
13 numerator=sp.int(3), denominator=sp.nat(5)
14 )
15 assert rational.mk((-3, -5)) == sp.record(
16 numerator=sp.int(3), denominator=sp.nat(5)
17 )
18 assert rational.mk((3, -5)) == sp.record(
19 numerator=sp.int(-3), denominator=sp.nat(5)
20 )
21
22 @sp.private
23 def test_ceil(self):
24 r1 = rational.mk((4, 3))
25 r2 = rational.mk((4, 2))
26 r3 = rational.mk((4, 1))
27 assert rational.ceil(r1) == sp.int(2)
28 assert rational.ceil(r2) == sp.int(2)
29 assert rational.ceil(r3) == sp.int(4)
30
31 @sp.private
32 def test_floor(self):
33 r1 = rational.mk((3, 1))
34 r2 = rational.mk((2, 3))
35 r3 = rational.mk((7, 2))
36 assert rational.floor(r1) == sp.int(3)
37 assert rational.floor(r2) == sp.int(0)
38 assert rational.floor(r3) == sp.int(3)
39
40 @sp.private
41 def test_round(self):
42 r1 = rational.mk((1, 3))
43 r2 = rational.mk((2, 3))
44 r3 = rational.mk((3, 3))
45 r4 = rational.mk((4, 3))
46 r5 = rational.mk((5, 3))
47 r6 = rational.mk((6, 3))
48 r7 = rational.mk((7, 3))
49 r8 = rational.mk((8, 3))
50 r9 = rational.mk((9, 3))
51 r10 = rational.mk((10, 3))
52 r11 = rational.mk((11, 3))
53 r12 = rational.mk((12, 3))
54 assert rational.round(r1) == sp.int(0)
55 assert rational.round(r2) == sp.int(1)
56 assert rational.round(r3) == sp.int(1)
57 assert rational.round(r4) == sp.int(1)
58 assert rational.round(r5) == sp.int(2)
59 assert rational.round(r6) == sp.int(2)
60 assert rational.round(r7) == sp.int(2)
61 assert rational.round(r8) == sp.int(3)
62 assert rational.round(r9) == sp.int(3)
63 assert rational.round(r10) == sp.int(3)
64 assert rational.round(r11) == sp.int(4)
65 assert rational.round(r12) == sp.int(4)
66
67 @sp.private
68 def test_rational_add(self):
69 r1 = rational.mk((3, 5))
70 r2 = rational.mk((4, 3))
71 res = rational.add((r1, r2))
72 assert res.numerator == sp.int(29)
73 assert res.denominator == sp.nat(15)
74
75 @sp.private
76 def test_rational_sub(self):
77 r1 = rational.mk((1, 5))
78 r2 = rational.mk((5, 3))
79 res = rational.sub((r1, r2))
80 assert res.numerator == sp.int(-22)
81 assert res.denominator == sp.nat(15)
82 r1 = rational.mk((2, 8))
83 r2 = rational.mk((1, 4))
84 res = rational.sub((r1, r2))
85 assert res.numerator == sp.int(0)
86
87 @sp.private
88 def test_rational_mul(self):
89 r1 = rational.mk((2, 5))
90 r2 = rational.mk((3, 4))
91 res = rational.mul((r1, r2))
92 assert res.numerator == sp.int(3)
93 assert res.denominator == sp.nat(10)
94 r1 = rational.mk((-4, 6))
95 r2 = rational.mk((1, 4))
96 res = rational.mul((r1, r2))
97 assert res.numerator == sp.int(-1)
98 assert res.denominator == sp.nat(6)
99
100 @sp.private
101 def test_rational_div(self):
102 r1 = rational.mk((2, 5))
103 r2 = rational.mk((3, 4))
104 res = rational.div((r1, r2))
105 assert res.numerator == sp.int(8)
106 assert res.denominator == sp.nat(15)
107 r1 = rational.mk((7, 4))
108 r2 = rational.mk((2, 7))
109 res = rational.div((r1, r2))
110 assert res.numerator == sp.int(49)
111 assert res.denominator == sp.nat(8)
112
113 @sp.entrypoint
114 def ep(self):
115 self.test_rational()
116 self.test_ceil()
117 self.test_floor()
118 self.test_rational_add()
119 self.test_rational_sub()
120 self.test_rational_mul()
121 self.test_round()
122
123
124@sp.add_test()
125def test():
126 s = sp.test_scenario("Test", [sp.math, sp.rational, M])
127 c = M.C()
128 s += c
129 c.ep()