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
|
package ast
import (
"testing"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/suite"
)
type TypesTestSuite struct{ suite.Suite }
func (s *TypesTestSuite) requireEncodeAndDecodeDecimalNoError(
d DataType, t decimal.Decimal, bs int) {
encode, err := DecimalEncode(d, t)
s.Require().NoError(err)
s.Require().Len(encode, bs)
decode, err := DecimalDecode(d, encode)
s.Require().NoError(err)
s.Require().Equal(t.String(), decode.String())
}
func (s *TypesTestSuite) requireEncodeAndDecodeNoError(
d DataType, t interface{}) {
encode, err := DataTypeEncode(t)
s.Require().NoError(err)
s.Require().Equal(d, encode)
decode, err := DataTypeDecode(d)
s.Require().NoError(err)
s.Require().Equal(t, decode)
}
func (s *TypesTestSuite) requireEncodeError(input interface{}) {
_, err := DataTypeEncode(input)
s.Require().Error(err)
}
func (s *TypesTestSuite) requireDecodeError(input DataType) {
_, err := DataTypeDecode(input)
s.Require().Error(err)
}
func (s *TypesTestSuite) TestEncodeAndDecode() {
s.requireEncodeAndDecodeNoError(
ComposeDataType(DataTypeMajorBool, 0),
BoolTypeNode{})
s.requireEncodeAndDecodeNoError(
ComposeDataType(DataTypeMajorAddress, 0),
AddressTypeNode{})
s.requireEncodeAndDecodeNoError(
ComposeDataType(DataTypeMajorInt, 1),
IntTypeNode{Size: 16})
s.requireEncodeAndDecodeNoError(
ComposeDataType(DataTypeMajorUint, 2),
IntTypeNode{Unsigned: true, Size: 24})
s.requireEncodeAndDecodeNoError(
ComposeDataType(DataTypeMajorFixedBytes, 3),
FixedBytesTypeNode{Size: 32})
s.requireEncodeAndDecodeNoError(
ComposeDataType(DataTypeMajorDynamicBytes, 0),
DynamicBytesTypeNode{})
s.requireEncodeAndDecodeNoError(
ComposeDataType(DataTypeMajorFixed, 1),
FixedTypeNode{Size: 8, FractionalDigits: 1})
s.requireEncodeAndDecodeNoError(
ComposeDataType(DataTypeMajorUfixed+1, 2),
FixedTypeNode{Unsigned: true, Size: 16, FractionalDigits: 2})
}
func (s *TypesTestSuite) TestEncodeError() {
s.requireEncodeError(struct{}{})
s.requireEncodeError(IntTypeNode{Size: 1})
s.requireEncodeError(IntTypeNode{Size: 257})
s.requireEncodeError(FixedBytesTypeNode{Size: 1})
s.requireEncodeError(FixedBytesTypeNode{Size: 257})
s.requireEncodeError(FixedTypeNode{Size: 1, FractionalDigits: 0})
s.requireEncodeError(FixedTypeNode{Size: 257, FractionalDigits: 0})
s.requireEncodeError(FixedTypeNode{Size: 8, FractionalDigits: 81})
}
func (s *TypesTestSuite) TestDecodeError() {
s.requireDecodeError(DataTypeUnknown)
s.requireDecodeError(ComposeDataType(DataTypeMajorBool, 1))
s.requireDecodeError(ComposeDataType(DataTypeMajorAddress, 1))
s.requireDecodeError(ComposeDataType(DataTypeMajorInt, 0x20))
s.requireDecodeError(ComposeDataType(DataTypeMajorUint, 0x20))
s.requireDecodeError(ComposeDataType(DataTypeMajorFixedBytes, 0x20))
s.requireDecodeError(ComposeDataType(DataTypeMajorDynamicBytes, 1))
s.requireDecodeError(ComposeDataType(DataTypeMajorFixed, 81))
s.requireDecodeError(ComposeDataType(DataTypeMajorUfixed, 81))
s.requireDecodeError(ComposeDataType(DataTypeMajorUfixed+0x20, 80))
}
func (s *TypesTestSuite) TestEncodeAndDecodeDecimal() {
pos := decimal.New(15, 0)
zero := decimal.Zero
neg := decimal.New(-15, 0)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorInt, 2),
pos,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorInt, 2),
zero,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorInt, 2),
neg,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorUint, 2),
pos,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorUint, 2),
zero,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorAddress, 0),
pos,
20)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorAddress, 0),
zero,
20)
pos = decimal.New(15, -2)
neg = decimal.New(-15, -2)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorFixed+2, 2),
pos,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorFixed+2, 2),
zero,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorFixed+2, 2),
neg,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorUfixed+2, 2),
pos,
3)
s.requireEncodeAndDecodeDecimalNoError(
ComposeDataType(DataTypeMajorUfixed+2, 2),
zero,
3)
}
func TestTypes(t *testing.T) {
suite.Run(t, new(TypesTestSuite))
}
|