forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_test.go
399 lines (333 loc) · 11.3 KB
/
int_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package types
import (
"math/big"
"math/rand"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestFromInt64(t *testing.T) {
for n := 0; n < 20; n++ {
r := rand.Int63()
require.Equal(t, r, NewInt(r).Int64())
}
}
func TestIntPanic(t *testing.T) {
// Max Int = 2^255-1 = 5.789e+76
// Min Int = -(2^255-1) = -5.789e+76
require.NotPanics(t, func() { NewIntWithDecimal(1, 76) })
i1 := NewIntWithDecimal(1, 76)
require.NotPanics(t, func() { NewIntWithDecimal(2, 76) })
i2 := NewIntWithDecimal(2, 76)
require.NotPanics(t, func() { NewIntWithDecimal(3, 76) })
i3 := NewIntWithDecimal(3, 76)
require.Panics(t, func() { NewIntWithDecimal(6, 76) })
require.Panics(t, func() { NewIntWithDecimal(9, 80) })
// Overflow check
require.NotPanics(t, func() { i1.Add(i1) })
require.NotPanics(t, func() { i2.Add(i2) })
require.Panics(t, func() { i3.Add(i3) })
require.NotPanics(t, func() { i1.Sub(i1.Neg()) })
require.NotPanics(t, func() { i2.Sub(i2.Neg()) })
require.Panics(t, func() { i3.Sub(i3.Neg()) })
require.Panics(t, func() { i1.Mul(i1) })
require.Panics(t, func() { i2.Mul(i2) })
require.Panics(t, func() { i3.Mul(i3) })
require.Panics(t, func() { i1.Neg().Mul(i1.Neg()) })
require.Panics(t, func() { i2.Neg().Mul(i2.Neg()) })
require.Panics(t, func() { i3.Neg().Mul(i3.Neg()) })
// Underflow check
i3n := i3.Neg()
require.NotPanics(t, func() { i3n.Sub(i1) })
require.NotPanics(t, func() { i3n.Sub(i2) })
require.Panics(t, func() { i3n.Sub(i3) })
require.NotPanics(t, func() { i3n.Add(i1.Neg()) })
require.NotPanics(t, func() { i3n.Add(i2.Neg()) })
require.Panics(t, func() { i3n.Add(i3.Neg()) })
require.Panics(t, func() { i1.Mul(i1.Neg()) })
require.Panics(t, func() { i2.Mul(i2.Neg()) })
require.Panics(t, func() { i3.Mul(i3.Neg()) })
// Bound check
intmax := NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(1)))
intmin := intmax.Neg()
require.NotPanics(t, func() { intmax.Add(ZeroInt()) })
require.NotPanics(t, func() { intmin.Sub(ZeroInt()) })
require.Panics(t, func() { intmax.Add(OneInt()) })
require.Panics(t, func() { intmin.Sub(OneInt()) })
// Division-by-zero check
require.Panics(t, func() { i1.Quo(NewInt(0)) })
}
// Tests below uses randomness
// Since we are using *big.Int as underlying value
// and (U/)Int is immutable value(see TestImmutability(U/)Int)
// it is safe to use randomness in the tests
func TestIdentInt(t *testing.T) {
for d := 0; d < 1000; d++ {
n := rand.Int63()
i := NewInt(n)
ifromstr, ok := NewIntFromString(strconv.FormatInt(n, 10))
require.True(t, ok)
cases := []int64{
i.Int64(),
i.BigInt().Int64(),
ifromstr.Int64(),
NewIntFromBigInt(big.NewInt(n)).Int64(),
NewIntWithDecimal(n, 0).Int64(),
}
for tcnum, tc := range cases {
require.Equal(t, n, tc, "Int is modified during conversion. tc #%d", tcnum)
}
}
}
func minint(i1, i2 int64) int64 {
if i1 < i2 {
return i1
}
return i2
}
func maxint(i1, i2 int64) int64 {
if i1 > i2 {
return i1
}
return i2
}
func TestArithInt(t *testing.T) {
for d := 0; d < 1000; d++ {
n1 := int64(rand.Int31())
i1 := NewInt(n1)
n2 := int64(rand.Int31())
i2 := NewInt(n2)
cases := []struct {
ires Int
nres int64
}{
{i1.Add(i2), n1 + n2},
{i1.Sub(i2), n1 - n2},
{i1.Mul(i2), n1 * n2},
{i1.Quo(i2), n1 / n2},
{i1.AddRaw(n2), n1 + n2},
{i1.SubRaw(n2), n1 - n2},
{i1.MulRaw(n2), n1 * n2},
{i1.QuoRaw(n2), n1 / n2},
{MinInt(i1, i2), minint(n1, n2)},
{MaxInt(i1, i2), maxint(n1, n2)},
{i1.Neg(), -n1},
}
for tcnum, tc := range cases {
require.Equal(t, tc.nres, tc.ires.Int64(), "Int arithmetic operation does not match with int64 operation. tc #%d", tcnum)
}
}
}
func TestCompInt(t *testing.T) {
for d := 0; d < 1000; d++ {
n1 := int64(rand.Int31())
i1 := NewInt(n1)
n2 := int64(rand.Int31())
i2 := NewInt(n2)
cases := []struct {
ires bool
nres bool
}{
{i1.Equal(i2), n1 == n2},
{i1.GT(i2), n1 > n2},
{i1.LT(i2), n1 < n2},
}
for tcnum, tc := range cases {
require.Equal(t, tc.nres, tc.ires, "Int comparison operation does not match with int64 operation. tc #%d", tcnum)
}
}
}
func minuint(i1, i2 uint64) uint64 {
if i1 < i2 {
return i1
}
return i2
}
func maxuint(i1, i2 uint64) uint64 {
if i1 > i2 {
return i1
}
return i2
}
func randint() Int {
return NewInt(rand.Int63())
}
func TestImmutabilityAllInt(t *testing.T) {
ops := []func(*Int){
func(i *Int) { _ = i.Add(randint()) },
func(i *Int) { _ = i.Sub(randint()) },
func(i *Int) { _ = i.Mul(randint()) },
func(i *Int) { _ = i.Quo(randint()) },
func(i *Int) { _ = i.AddRaw(rand.Int63()) },
func(i *Int) { _ = i.SubRaw(rand.Int63()) },
func(i *Int) { _ = i.MulRaw(rand.Int63()) },
func(i *Int) { _ = i.QuoRaw(rand.Int63()) },
func(i *Int) { _ = i.Neg() },
func(i *Int) { _ = i.IsZero() },
func(i *Int) { _ = i.Sign() },
func(i *Int) { _ = i.Equal(randint()) },
func(i *Int) { _ = i.GT(randint()) },
func(i *Int) { _ = i.LT(randint()) },
func(i *Int) { _ = i.String() },
}
for i := 0; i < 1000; i++ {
n := rand.Int63()
ni := NewInt(n)
for opnum, op := range ops {
op(&ni)
require.Equal(t, n, ni.Int64(), "Int is modified by operation. tc #%d", opnum)
require.Equal(t, NewInt(n), ni, "Int is modified by operation. tc #%d", opnum)
}
}
}
type intop func(Int, *big.Int) (Int, *big.Int)
func intarith(uifn func(Int, Int) Int, bifn func(*big.Int, *big.Int, *big.Int) *big.Int) intop {
return func(ui Int, bi *big.Int) (Int, *big.Int) {
r := rand.Int63()
br := new(big.Int).SetInt64(r)
return uifn(ui, NewInt(r)), bifn(new(big.Int), bi, br)
}
}
func intarithraw(uifn func(Int, int64) Int, bifn func(*big.Int, *big.Int, *big.Int) *big.Int) intop {
return func(ui Int, bi *big.Int) (Int, *big.Int) {
r := rand.Int63()
br := new(big.Int).SetInt64(r)
return uifn(ui, r), bifn(new(big.Int), bi, br)
}
}
func TestImmutabilityArithInt(t *testing.T) {
size := 500
ops := []intop{
intarith(Int.Add, (*big.Int).Add),
intarith(Int.Sub, (*big.Int).Sub),
intarith(Int.Mul, (*big.Int).Mul),
intarith(Int.Quo, (*big.Int).Quo),
intarithraw(Int.AddRaw, (*big.Int).Add),
intarithraw(Int.SubRaw, (*big.Int).Sub),
intarithraw(Int.MulRaw, (*big.Int).Mul),
intarithraw(Int.QuoRaw, (*big.Int).Quo),
}
for i := 0; i < 100; i++ {
uis := make([]Int, size)
bis := make([]*big.Int, size)
n := rand.Int63()
ui := NewInt(n)
bi := new(big.Int).SetInt64(n)
for j := 0; j < size; j++ {
op := ops[rand.Intn(len(ops))]
uis[j], bis[j] = op(ui, bi)
}
for j := 0; j < size; j++ {
require.Equal(t, 0, bis[j].Cmp(uis[j].BigInt()), "Int is different from *big.Int. tc #%d, Int %s, *big.Int %s", j, uis[j].String(), bis[j].String())
require.Equal(t, NewIntFromBigInt(bis[j]), uis[j], "Int is different from *big.Int. tc #%d, Int %s, *big.Int %s", j, uis[j].String(), bis[j].String())
require.True(t, uis[j].i != bis[j], "Pointer addresses are equal. tc #%d, Int %s, *big.Int %s", j, uis[j].String(), bis[j].String())
}
}
}
func TestEncodingRandom(t *testing.T) {
for i := 0; i < 1000; i++ {
n := rand.Int63()
ni := NewInt(n)
var ri Int
str, err := ni.MarshalAmino()
require.Nil(t, err)
err = (&ri).UnmarshalAmino(str)
require.Nil(t, err)
require.Equal(t, ni, ri, "MarshalAmino * UnmarshalAmino is not identity. tc #%d, Expected %s, Actual %s", i, ni.String(), ri.String())
require.True(t, ni.i != ri.i, "Pointer addresses are equal. tc #%d", i)
bz, err := ni.MarshalJSON()
require.Nil(t, err)
err = (&ri).UnmarshalJSON(bz)
require.Nil(t, err)
require.Equal(t, ni, ri, "MarshalJSON * UnmarshalJSON is not identity. tc #%d, Expected %s, Actual %s", i, ni.String(), ri.String())
require.True(t, ni.i != ri.i, "Pointer addresses are equal. tc #%d", i)
}
for i := 0; i < 1000; i++ {
n := rand.Uint64()
ni := NewUint(n)
var ri Uint
str, err := ni.MarshalAmino()
require.Nil(t, err)
err = (&ri).UnmarshalAmino(str)
require.Nil(t, err)
require.Equal(t, ni, ri, "MarshalAmino * UnmarshalAmino is not identity. tc #%d, Expected %s, Actual %s", i, ni.String(), ri.String())
require.True(t, ni.i != ri.i, "Pointer addresses are equal. tc #%d", i)
bz, err := ni.MarshalJSON()
require.Nil(t, err)
err = (&ri).UnmarshalJSON(bz)
require.Nil(t, err)
require.Equal(t, ni, ri, "MarshalJSON * UnmarshalJSON is not identity. tc #%d, Expected %s, Actual %s", i, ni.String(), ri.String())
require.True(t, ni.i != ri.i, "Pointer addresses are equal. tc #%d", i)
}
}
func TestEncodingTableInt(t *testing.T) {
var i Int
cases := []struct {
i Int
bz []byte
str string
}{
{NewInt(0), []byte("\"0\""), "0"},
{NewInt(100), []byte("\"100\""), "100"},
{NewInt(51842), []byte("\"51842\""), "51842"},
{NewInt(19513368), []byte("\"19513368\""), "19513368"},
{NewInt(999999999999), []byte("\"999999999999\""), "999999999999"},
}
for tcnum, tc := range cases {
bz, err := tc.i.MarshalJSON()
require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err)
require.Equal(t, tc.bz, bz, "Marshaled value is different from exported. tc #%d", tcnum)
err = (&i).UnmarshalJSON(bz)
require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err)
require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum)
str, err := tc.i.MarshalAmino()
require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err)
require.Equal(t, tc.str, str, "Marshaled value is different from exported. tc #%d", tcnum)
err = (&i).UnmarshalAmino(str)
require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err)
require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum)
}
}
func TestEncodingTableUint(t *testing.T) {
var i Uint
cases := []struct {
i Uint
bz []byte
str string
}{
{NewUint(0), []byte("\"0\""), "0"},
{NewUint(100), []byte("\"100\""), "100"},
{NewUint(51842), []byte("\"51842\""), "51842"},
{NewUint(19513368), []byte("\"19513368\""), "19513368"},
{NewUint(999999999999), []byte("\"999999999999\""), "999999999999"},
}
for tcnum, tc := range cases {
bz, err := tc.i.MarshalJSON()
require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err)
require.Equal(t, tc.bz, bz, "Marshaled value is different from exported. tc #%d", tcnum)
err = (&i).UnmarshalJSON(bz)
require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err)
require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum)
str, err := tc.i.MarshalAmino()
require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err)
require.Equal(t, tc.str, str, "Marshaled value is different from exported. tc #%d", tcnum)
err = (&i).UnmarshalAmino(str)
require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err)
require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum)
}
}
func TestSerializationOverflow(t *testing.T) {
bx, _ := new(big.Int).SetString("91888242871839275229946405745257275988696311157297823662689937894645226298583", 10)
x := Int{bx}
y := new(Int)
// require amino deserialization to fail due to overflow
xStr, err := x.MarshalAmino()
require.NoError(t, err)
err = y.UnmarshalAmino(xStr)
require.Error(t, err)
// require JSON deserialization to fail due to overflow
bz, err := x.MarshalJSON()
require.NoError(t, err)
err = y.UnmarshalJSON(bz)
require.Error(t, err)
}