-
Notifications
You must be signed in to change notification settings - Fork 9
/
value_encoder_test.go
102 lines (82 loc) · 2.63 KB
/
value_encoder_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
package collections
import (
"math/big"
"testing"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/stretchr/testify/suite"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/gogo/protobuf/types"
)
type SuiteValueEncoder struct {
suite.Suite
}
func TestSuiteValueEncoder_RunAll(t *testing.T) {
suite.Run(t, new(SuiteValueEncoder))
}
func (s *SuiteValueEncoder) TestProtoValueEncoder() {
s.T().Run("bijectivity", func(t *testing.T) {
protoType := types.BytesValue{Value: []byte("testing")}
registry := testdata.NewTestInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
assertValueBijective[types.BytesValue](t, ProtoValueEncoder[types.BytesValue](cdc), protoType)
})
}
func (s *SuiteValueEncoder) TestDecValueEncoder() {
s.Run("bijectivity", func() {
assertValueBijective(s.T(), DecValueEncoder, math.LegacyMustNewDecFromStr("-1000.5858"))
})
}
func (s *SuiteValueEncoder) TestAccAddressValueEncoder() {
s.Run("bijectivity", func() {
assertValueBijective(s.T(), AccAddressValueEncoder, sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()))
})
}
func (s *SuiteValueEncoder) TestUint64ValueEncoder() {
s.Run("bijectivity", func() {
assertValueBijective(s.T(), Uint64ValueEncoder, 1000)
})
}
func (s *SuiteValueEncoder) TestIntEncoder() {
// we test our assumptions around int are correct.
outOfBounds := new(big.Int).Lsh(big.NewInt(1), 256) // 2^256
maxBigInt := new(big.Int).Sub(outOfBounds, big.NewInt(1)) // 2^256 - 1
s.Equal(maxBigInt.BitLen(), math.MaxBitLen)
s.Greater(outOfBounds.BitLen(), math.MaxBitLen)
s.NotPanics(func() {
math.NewIntFromBigInt(maxBigInt)
})
s.Panics(func() {
math.NewIntFromBigInt(outOfBounds)
})
s.Require().Equal(maxIntKeyLen, len(maxBigInt.Bytes()))
// test encoding ordering
enc1 := IntKeyEncoder.Encode(math.NewInt(50_000))
enc2 := IntKeyEncoder.Encode(math.NewInt(100_000))
s.Less(enc1, enc2)
// test decoding
size, got1 := IntKeyEncoder.Decode(enc1)
s.Equal(maxIntKeyLen, size)
_, got2 := IntKeyEncoder.Decode(enc2)
s.Equal(math.NewInt(50_000), got1)
s.Equal(math.NewInt(100_000), got2)
// require panics on negative values
s.Panics(func() {
IntKeyEncoder.Encode(math.NewInt(-1))
})
// require panics on invalid int
s.Panics(func() {
IntKeyEncoder.Encode(math.Int{})
})
// test value encoder
value := math.NewInt(50_000)
valueBytes := IntValueEncoder.Encode(value)
gotValue := IntValueEncoder.Decode(valueBytes)
s.Equal(value, gotValue)
// panics on invalid math.Int
s.Panics(func() {
IntValueEncoder.Encode(math.Int{})
})
}