forked from Finschia/finschia-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_internal_test.go
155 lines (122 loc) · 4.37 KB
/
int_internal_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
package types
import (
"math/big"
"math/rand"
"testing"
"github.com/stretchr/testify/suite"
)
type internalIntTestSuite struct {
suite.Suite
}
func TestInternalIntTestSuite(t *testing.T) {
suite.Run(t, new(internalIntTestSuite))
}
func (s *internalIntTestSuite) TestEncodingRandom() {
for i := 0; i < 1000; i++ {
n := rand.Int63()
ni := NewInt(n)
var ri Int
str, err := ni.Marshal()
s.Require().Nil(err)
err = (&ri).Unmarshal(str)
s.Require().Nil(err)
s.Require().Equal(ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String())
s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i)
bz, err := ni.MarshalJSON()
s.Require().Nil(err)
err = (&ri).UnmarshalJSON(bz)
s.Require().Nil(err)
s.Require().Equal(ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String())
s.Require().True(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.Marshal()
s.Require().Nil(err)
err = (&ri).Unmarshal(str)
s.Require().Nil(err)
s.Require().Equal(ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String())
s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i)
bz, err := ni.MarshalJSON()
s.Require().Nil(err)
err = (&ri).UnmarshalJSON(bz)
s.Require().Nil(err)
s.Require().Equal(ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String())
s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i)
}
}
func (s *internalIntTestSuite) TestSerializationOverflow() {
bx, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639936", 10)
x := Int{bx}
y := new(Int)
bz, err := x.Marshal()
s.Require().NoError(err)
// require deserialization to fail due to overflow
s.Require().Error(y.Unmarshal(bz))
// require JSON deserialization to fail due to overflow
bz, err = x.MarshalJSON()
s.Require().NoError(err)
s.Require().Error(y.UnmarshalJSON(bz))
}
func (s *internalIntTestSuite) TestDeserializeMaxERC20() {
bx, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639935", 10)
x := Int{bx}
y := new(Int)
bz, err := x.Marshal()
s.Require().NoError(err)
// require deserialization to be successful
s.Require().NoError(y.Unmarshal(bz))
// require JSON deserialization to succeed
bz, err = x.MarshalJSON()
s.Require().NoError(err)
s.Require().NoError(y.UnmarshalJSON(bz))
}
func (s *internalIntTestSuite) TestImmutabilityArithInt() {
size := 500
ops := []intOp{
applyWithRand(Int.Add, (*big.Int).Add),
applyWithRand(Int.Sub, (*big.Int).Sub),
applyWithRand(Int.Mul, (*big.Int).Mul),
applyWithRand(Int.Quo, (*big.Int).Quo),
applyRawWithRand(Int.AddRaw, (*big.Int).Add),
applyRawWithRand(Int.SubRaw, (*big.Int).Sub),
applyRawWithRand(Int.MulRaw, (*big.Int).Mul),
applyRawWithRand(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++ {
s.Require().Equal(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())
s.Require().Equal(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())
s.Require().True(uis[j].i != bis[j], "Pointer addresses are equal. tc #%d, Int %s, *big.Int %s", j, uis[j].String(), bis[j].String())
}
}
}
type (
intOp func(Int, *big.Int) (Int, *big.Int)
bigIntFunc func(*big.Int, *big.Int, *big.Int) *big.Int
)
func applyWithRand(intFn func(Int, Int) Int, bigIntFn bigIntFunc) intOp {
return func(integer Int, bigInteger *big.Int) (Int, *big.Int) {
r := rand.Int63()
br := new(big.Int).SetInt64(r)
return intFn(integer, NewInt(r)), bigIntFn(new(big.Int), bigInteger, br)
}
}
func applyRawWithRand(intFn func(Int, int64) Int, bigIntFn bigIntFunc) intOp {
return func(integer Int, bigInteger *big.Int) (Int, *big.Int) {
r := rand.Int63()
br := new(big.Int).SetInt64(r)
return intFn(integer, r), bigIntFn(new(big.Int), bigInteger, br)
}
}