-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathint_internal_test.go
137 lines (109 loc) · 3.89 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
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("91888242871839275229946405745257275988696311157297823662689937894645226298583", 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) 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)
}
}