forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_test.go
111 lines (90 loc) · 3.46 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
package types
import (
"math/big"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFromInt64(t *testing.T) {
for n := 0; n < 20; n++ {
r := rand.Int63()
assert.Equal(t, r, NewInt(r).Int64())
}
}
func TestInt(t *testing.T) {
// Max Int = 2^255-1 = 5.789e+76
// Min Int = -(2^255-1) = -5.789e+76
assert.NotPanics(t, func() { NewIntWithDecimal(1, 76) })
i1 := NewIntWithDecimal(1, 76)
assert.NotPanics(t, func() { NewIntWithDecimal(2, 76) })
i2 := NewIntWithDecimal(2, 76)
assert.NotPanics(t, func() { NewIntWithDecimal(3, 76) })
i3 := NewIntWithDecimal(3, 76)
assert.Panics(t, func() { NewIntWithDecimal(6, 76) })
assert.Panics(t, func() { NewIntWithDecimal(9, 80) })
// Overflow check
assert.NotPanics(t, func() { i1.Add(i1) })
assert.NotPanics(t, func() { i2.Add(i2) })
assert.Panics(t, func() { i3.Add(i3) })
assert.NotPanics(t, func() { i1.Sub(i1.Neg()) })
assert.NotPanics(t, func() { i2.Sub(i2.Neg()) })
assert.Panics(t, func() { i3.Sub(i3.Neg()) })
assert.Panics(t, func() { i1.Mul(i1) })
assert.Panics(t, func() { i2.Mul(i2) })
assert.Panics(t, func() { i3.Mul(i3) })
assert.Panics(t, func() { i1.Neg().Mul(i1.Neg()) })
assert.Panics(t, func() { i2.Neg().Mul(i2.Neg()) })
assert.Panics(t, func() { i3.Neg().Mul(i3.Neg()) })
// Underflow check
i3n := i3.Neg()
assert.NotPanics(t, func() { i3n.Sub(i1) })
assert.NotPanics(t, func() { i3n.Sub(i2) })
assert.Panics(t, func() { i3n.Sub(i3) })
assert.NotPanics(t, func() { i3n.Add(i1.Neg()) })
assert.NotPanics(t, func() { i3n.Add(i2.Neg()) })
assert.Panics(t, func() { i3n.Add(i3.Neg()) })
assert.Panics(t, func() { i1.Mul(i1.Neg()) })
assert.Panics(t, func() { i2.Mul(i2.Neg()) })
assert.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()
assert.NotPanics(t, func() { intmax.Add(ZeroInt()) })
assert.NotPanics(t, func() { intmin.Sub(ZeroInt()) })
assert.Panics(t, func() { intmax.Add(OneInt()) })
assert.Panics(t, func() { intmin.Sub(OneInt()) })
// Division-by-zero check
assert.Panics(t, func() { i1.Div(NewInt(0)) })
}
func TestUint(t *testing.T) {
// Max Uint = 1.15e+77
// Min Uint = 0
assert.NotPanics(t, func() { NewUintWithDecimal(5, 76) })
i1 := NewUintWithDecimal(5, 76)
assert.NotPanics(t, func() { NewUintWithDecimal(10, 76) })
i2 := NewUintWithDecimal(10, 76)
assert.NotPanics(t, func() { NewUintWithDecimal(11, 76) })
i3 := NewUintWithDecimal(11, 76)
assert.Panics(t, func() { NewUintWithDecimal(12, 76) })
assert.Panics(t, func() { NewUintWithDecimal(1, 80) })
// Overflow check
assert.NotPanics(t, func() { i1.Add(i1) })
assert.Panics(t, func() { i2.Add(i2) })
assert.Panics(t, func() { i3.Add(i3) })
assert.Panics(t, func() { i1.Mul(i1) })
assert.Panics(t, func() { i2.Mul(i2) })
assert.Panics(t, func() { i3.Mul(i3) })
// Underflow check
assert.NotPanics(t, func() { i2.Sub(i1) })
assert.NotPanics(t, func() { i2.Sub(i2) })
assert.Panics(t, func() { i2.Sub(i3) })
// Bound check
uintmax := NewUintFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1)))
uintmin := NewUint(0)
assert.NotPanics(t, func() { uintmax.Add(ZeroUint()) })
assert.NotPanics(t, func() { uintmin.Sub(ZeroUint()) })
assert.Panics(t, func() { uintmax.Add(OneUint()) })
assert.Panics(t, func() { uintmin.Sub(OneUint()) })
// Division-by-zero check
assert.Panics(t, func() { i1.Div(uintmin) })
}