cmp_test.gno
4.25 Kb ยท 251 lines
1package uint256
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestSign(t *testing.T) {
9 tests := []struct {
10 input *Uint
11 expected int
12 }{
13 {
14 input: NewUint(0),
15 expected: 0,
16 },
17 {
18 input: NewUint(1),
19 expected: 1,
20 },
21 {
22 input: NewUint(0x7fffffffffffffff),
23 expected: 1,
24 },
25 {
26 input: NewUint(0x8000000000000000),
27 expected: 1,
28 },
29 }
30
31 for _, tt := range tests {
32 t.Run(tt.input.String(), func(t *testing.T) {
33 result := tt.input.Sign()
34 if result != tt.expected {
35 t.Errorf("Sign() = %d; want %d", result, tt.expected)
36 }
37 })
38 }
39}
40
41func TestCmp(t *testing.T) {
42 tests := []struct {
43 x, y string
44 want int
45 }{
46 {"0", "0", 0},
47 {"0", "1", -1},
48 {"1", "0", 1},
49 {"1", "1", 0},
50 {"10", "10", 0},
51 {"10", "11", -1},
52 {"11", "10", 1},
53 }
54
55 for _, tc := range tests {
56 x := MustFromDecimal(tc.x)
57 y := MustFromDecimal(tc.y)
58
59 got := x.Cmp(y)
60 if got != tc.want {
61 t.Errorf("Cmp(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want)
62 }
63 }
64}
65
66func TestIsZero(t *testing.T) {
67 tests := []struct {
68 x string
69 want bool
70 }{
71 {"0", true},
72 {"1", false},
73 {"10", false},
74 }
75
76 for _, tt := range tests {
77 x := MustFromDecimal(tt.x)
78
79 got := x.IsZero()
80 if got != tt.want {
81 t.Errorf("IsZero(%s) = %v, want %v", tt.x, got, tt.want)
82 }
83 }
84}
85
86func TestLtUint64(t *testing.T) {
87 tests := []struct {
88 x string
89 y uint64
90 want bool
91 }{
92 {"0", 1, true},
93 {"1", 0, false},
94 {"10", 10, false},
95 {"0xffffffffffffffff", 0, false},
96 {"0x10000000000000000", 10000000000000000, false},
97 }
98
99 for _, tc := range tests {
100 x := parseTestString(t, tc.x)
101
102 got := x.LtUint64(tc.y)
103 if got != tc.want {
104 t.Errorf("LtUint64(%s, %d) = %v, want %v", tc.x, tc.y, got, tc.want)
105 }
106 }
107}
108
109func TestUint_GtUint64(t *testing.T) {
110 tests := []struct {
111 name string
112 z string
113 n uint64
114 want bool
115 }{
116 {
117 name: "z > n",
118 z: "1",
119 n: 0,
120 want: true,
121 },
122 {
123 name: "z < n",
124 z: "18446744073709551615",
125 n: 0xFFFFFFFFFFFFFFFF,
126 want: false,
127 },
128 {
129 name: "z == n",
130 z: "18446744073709551615",
131 n: 0xFFFFFFFFFFFFFFFF,
132 want: false,
133 },
134 }
135
136 for _, tt := range tests {
137 t.Run(tt.name, func(t *testing.T) {
138 z := MustFromDecimal(tt.z)
139
140 if got := z.GtUint64(tt.n); got != tt.want {
141 t.Errorf("Uint.GtUint64() = %v, want %v", got, tt.want)
142 }
143 })
144 }
145}
146
147func TestSGT(t *testing.T) {
148 x := MustFromHex("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe")
149 y := MustFromHex("0x0")
150 actual := x.Sgt(y)
151 if actual {
152 t.Fatalf("Expected %v false", actual)
153 }
154
155 x = MustFromHex("0x0")
156 y = MustFromHex("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe")
157 actual = x.Sgt(y)
158 if !actual {
159 t.Fatalf("Expected %v true", actual)
160 }
161}
162
163func TestEq(t *testing.T) {
164 tests := []struct {
165 x string
166 y string
167 want bool
168 }{
169 {"0xffffffffffffffff", "18446744073709551615", true},
170 {"0x10000000000000000", "18446744073709551616", true},
171 {"0", "0", true},
172 {twoPow256Sub1, twoPow256Sub1, true},
173 }
174
175 for _, tt := range tests {
176 x := parseTestString(t, tt.x)
177
178 y, err := FromDecimal(tt.y)
179 if err != nil {
180 t.Error(err)
181 continue
182 }
183
184 got := x.Eq(y)
185
186 if got != tt.want {
187 t.Errorf("Eq(%s, %s) = %v, want %v", tt.x, tt.y, got, tt.want)
188 }
189 }
190}
191
192func TestUint_Lte(t *testing.T) {
193 tests := []struct {
194 z, x string
195 want bool
196 }{
197 {"10", "20", true},
198 {"20", "10", false},
199 {"10", "10", true},
200 {"0", "0", true},
201 }
202
203 for _, tt := range tests {
204 z, err := FromDecimal(tt.z)
205 if err != nil {
206 t.Error(err)
207 continue
208 }
209 x, err := FromDecimal(tt.x)
210 if err != nil {
211 t.Error(err)
212 continue
213 }
214 if got := z.Lte(x); got != tt.want {
215 t.Errorf("Uint.Lte(%v, %v) = %v, want %v", tt.z, tt.x, got, tt.want)
216 }
217 }
218}
219
220func TestUint_Gte(t *testing.T) {
221 tests := []struct {
222 z, x string
223 want bool
224 }{
225 {"20", "10", true},
226 {"10", "20", false},
227 {"10", "10", true},
228 {"0", "0", true},
229 }
230
231 for _, tt := range tests {
232 z := parseTestString(t, tt.z)
233 x := parseTestString(t, tt.x)
234
235 if got := z.Gte(x); got != tt.want {
236 t.Errorf("Uint.Gte(%v, %v) = %v, want %v", tt.z, tt.x, got, tt.want)
237 }
238 }
239}
240
241func parseTestString(_ *testing.T, s string) *Uint {
242 var x *Uint
243
244 if strings.HasPrefix(s, "0x") {
245 x = MustFromHex(s)
246 } else {
247 x = MustFromDecimal(s)
248 }
249
250 return x
251}