-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvariant_test.go
238 lines (215 loc) · 5.59 KB
/
variant_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package bin
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Forest struct {
T Tree
}
type Tree struct {
Padding [5]byte
NodeCount uint32 `bin:"sizeof=Nodes"`
Random uint64
Nodes []*Node
}
var NodeVariantDef = NewVariantDefinition(Uint32TypeIDEncoding, []VariantType{
{"left_node", (*NodeLeft)(nil)},
{"right_node", (*NodeRight)(nil)},
{"inner_node", (*NodeInner)(nil)},
})
type Node struct {
BaseVariant
}
type NodeLeft struct {
Key uint32
Description string
}
type NodeRight struct {
Owner uint64
Padding [2]byte
Quantity Uint64
}
type NodeInner struct {
Key Uint128
}
func (n *Node) UnmarshalBinary(decoder *Decoder) error {
return n.BaseVariant.UnmarshalBinaryVariant(decoder, NodeVariantDef)
}
func (n *Node) MarshalBinary(encoder *Encoder) error {
err := encoder.WriteUint32(n.TypeID, LE())
if err != nil {
return err
}
return encoder.Encode(n.Impl)
}
func TestDecode_Variant(t *testing.T) {
buf := []byte{
0x73, 0x65, 0x72, 0x75, 0x6d, // Padding[5]byte
0x05, 0x00, 0x00, 0x00, // Node length 5
0xff, 0xff, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, // ROOT 65,535
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63, // left node -> key = 3, description "abc"
0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // right node -> owner = 3, quantity 13
0x01, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // right node -> owner = 82, quantity 923
0x02, 0x00, 0x00, 0x00, 0xff, 0x7f, 0xc6, 0xa4, 0x7e, 0x8d, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // inner node -> key = 999999999999999
0x02, 0x00, 0x00, 0x00, 0x23, 0xd3, 0xd8, 0x9a, 0x99, 0x7e, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // inner node -> key = 983623123129123
}
decoder := NewDecoder(buf)
forest := Forest{}
err := decoder.Decode(&forest)
require.NoError(t, err)
require.Equal(t, 0, decoder.Remaining())
assert.Equal(t, Tree{
Padding: [5]byte{0x73, 0x65, 0x72, 0x75, 0x6d},
NodeCount: 5,
Random: 65535,
Nodes: []*Node{
{
BaseVariant: BaseVariant{
TypeID: 0,
Impl: &NodeLeft{
Key: 3,
Description: "abc",
},
},
},
{
BaseVariant: BaseVariant{
TypeID: 1,
Impl: &NodeRight{
Owner: 3,
Padding: [2]byte{0x00, 0x00},
Quantity: 13,
},
},
},
{
BaseVariant: BaseVariant{
TypeID: 1,
Impl: &NodeRight{
Owner: 82,
Padding: [2]byte{0x00, 0x00},
Quantity: 923,
},
},
},
{
BaseVariant: BaseVariant{
TypeID: 2,
Impl: &NodeInner{
Key: Uint128{
Lo: 999999999999999,
Hi: 0,
},
},
},
},
{
BaseVariant: BaseVariant{
TypeID: 2,
Impl: &NodeInner{
Key: Uint128{
Lo: 983623123129123,
Hi: 0,
},
},
},
},
},
}, forest.T)
}
func TestEncode_Variant(t *testing.T) {
expectBuf := []byte{
0x73, 0x65, 0x72, 0x75, 0x6d, // Padding[5]byte
0x05, 0x00, 0x00, 0x00, // Node length 5
0xff, 0xff, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, // ROOT 65,535
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63, // left node -> key = 3, description "abc"
0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // right node -> owner = 3, quantity 13
0x01, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // right node -> owner = 82, quantity 923
0x02, 0x00, 0x00, 0x00, 0xff, 0x7f, 0xc6, 0xa4, 0x7e, 0x8d, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // inner node -> key = 999999999999999
0x02, 0x00, 0x00, 0x00, 0x23, 0xd3, 0xd8, 0x9a, 0x99, 0x7e, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // inner node -> key = 983623123129123
}
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
enc.Encode(&Forest{T: Tree{
Padding: [5]byte{0x73, 0x65, 0x72, 0x75, 0x6d},
NodeCount: 5,
Random: 65535,
Nodes: []*Node{
{
BaseVariant: BaseVariant{
TypeID: 0,
Impl: &NodeLeft{
Key: 3,
Description: "abc",
},
},
},
{
BaseVariant: BaseVariant{
TypeID: 1,
Impl: &NodeRight{
Owner: 3,
Padding: [2]byte{0x00, 0x00},
Quantity: 13,
},
},
},
{
BaseVariant: BaseVariant{
TypeID: 1,
Impl: &NodeRight{
Owner: 82,
Padding: [2]byte{0x00, 0x00},
Quantity: 923,
},
},
},
{
BaseVariant: BaseVariant{
TypeID: 2,
Impl: &NodeInner{
Key: Uint128{
Lo: 999999999999999,
Hi: 0,
},
},
},
},
{
BaseVariant: BaseVariant{
TypeID: 2,
Impl: &NodeInner{
Key: Uint128{
Lo: 983623123129123,
Hi: 0,
},
},
},
},
},
}})
assert.Equal(t, expectBuf, buf.Bytes())
}
type unexportesStruct struct {
value uint32
}
func TestDecode_UnexporterStruct(t *testing.T) {
buf := []byte{
0x05, 0x00, 0x00, 0x00,
}
decoder := NewDecoder(buf)
s := unexportesStruct{}
err := decoder.Decode(&s)
require.NoError(t, err)
require.Equal(t, 4, decoder.Remaining())
assert.Equal(t, unexportesStruct{value: 0}, s)
}
func TestEncode_UnexporterStruct(t *testing.T) {
var expectData []byte
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
enc.Encode(&unexportesStruct{value: 5})
assert.Equal(t, expectData, buf.Bytes())
}