-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
366 lines (319 loc) · 8.15 KB
/
example_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package binary_test
import (
"bytes"
"encoding/gob"
"fmt"
"math"
std "encoding/binary"
"github.com/vipally/binary"
)
func ExampleWrite() {
buf := new(bytes.Buffer)
var pi float64 = math.Pi
err := binary.Write(buf, binary.LittleEndian, pi)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
fmt.Printf("%#v", buf.Bytes())
// Output:
// []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x9, 0x40}
}
func ExampleWrite_multi() {
buf := new(bytes.Buffer)
var data = []interface{}{
uint16(61374),
int8(-54),
uint8(254),
}
for _, v := range data {
err := binary.Write(buf, binary.LittleEndian, v)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
}
fmt.Printf("%#v", buf.Bytes())
// Output:
// []byte{0xbe, 0xef, 0xca, 0xfe}
}
func ExampleRead() {
var pi float64
b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
buf := bytes.NewReader(b)
err := binary.Read(buf, binary.LittleEndian, &pi)
if err != nil {
fmt.Println("binary.Read failed:", err)
}
fmt.Print(pi)
// Output:
// 3.141592653589793
}
func ExampleEncode() {
var data struct {
A uint32
B int
C string
}
data.A = 0x11223344
data.B = -5
data.C = "hello"
b, err := binary.Encode(data, nil)
if err != nil {
fmt.Println("binary.Encode failed:", err)
}
fmt.Printf("Encode:\n%+v\n%#v", data, b)
// Output:
// Encode:
// {A:287454020 B:-5 C:hello}
// []byte{0x44, 0x33, 0x22, 0x11, 0x9, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
}
func ExampleEncode_withbuffer() {
var data struct {
A uint32
B int
C string
}
data.A = 0x11223344
data.B = -5
data.C = "hello"
size := binary.Sizeof(data)
buffer := make([]byte, size)
b, err := binary.Encode(data, buffer)
if err != nil {
fmt.Println("binary.Encode failed:", err)
}
fmt.Printf("Encode:\n%+v\n%#v", data, b)
// Output:
// Encode:
// {A:287454020 B:-5 C:hello}
// []byte{0x44, 0x33, 0x22, 0x11, 0x9, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
}
func ExampleEncode_bools() {
type boolset struct {
A uint8 //0xa
B bool //true
C uint8 //0xc
D []bool //[]bool{true, false, true}
E bool //true
F *uint32 //false
G bool //true
H uint8 //0x8
}
var data = boolset{
0xa, true, 0xc, []bool{true, false, true}, true, nil, true, 0x8,
}
b, err := binary.Encode(data, nil)
if err != nil {
fmt.Println(err)
}
if size := binary.Sizeof(data); size != len(b) {
fmt.Printf("Encode got %#v %+v\nneed %+v\n", len(b), b, size)
}
fmt.Printf("Encode bools:\n%+v\nsize=%d result=%#v", data, len(b), b)
// Output:
// Encode bools:
// {A:10 B:true C:12 D:[true false true] E:true F:<nil> G:true H:8}
// size=6 result=[]byte{0xa, 0xb, 0xc, 0x3, 0x5, 0x8}
}
func ExampleEncode_boolArray() {
var data = []bool{true, true, true, false, true, true, false, false, true}
b, err := binary.Encode(data, nil)
if err != nil {
fmt.Println(err)
}
if size := binary.Sizeof(data); size != len(b) {
fmt.Printf("Encode bool array:\ngot %#v %+v\nneed %+v\n", len(b), b, size)
}
fmt.Printf("Encode bool array:\n%#v\nsize=%d result=%#v", data, len(b), b)
// Output:
// Encode bool array:
// []bool{true, true, true, false, true, true, false, false, true}
// size=3 result=[]byte{0x9, 0x37, 0x1}
}
func ExampleEncode_packedInts() {
type regedPackedInts struct {
A int16 `binary:"packed"`
B int32 `binary:"packed"`
C int64 `binary:"packed"`
D uint16 `binary:"packed"`
E uint32 `binary:"packed"`
F uint64 `binary:"packed"`
G []uint64 `binary:"packed"`
H uint `binary:"ignore"`
}
binary.RegStruct((*regedPackedInts)(nil))
var data = regedPackedInts{1, 2, 3, 4, 5, 6, []uint64{7, 8, 9}, 10}
b, err := binary.Encode(data, nil)
if err != nil {
fmt.Println(err)
}
if size := binary.Sizeof(data); size != len(b) {
fmt.Printf("PackedInts got %+v %+v\nneed %+v\n", len(b), b, size)
}
fmt.Printf("Encode packed ints:\n%+v\nsize=%d result=%#v", data, len(b), b)
// Output:
// Encode packed ints:
// {A:1 B:2 C:3 D:4 E:5 F:6 G:[7 8 9] H:10}
// size=10 result=[]byte{0x2, 0x4, 0x6, 0x4, 0x5, 0x6, 0x3, 0x7, 0x8, 0x9}
}
func ExampleDecode() {
var s struct {
A uint32
B int
C string
}
buffer := []byte{0x44, 0x33, 0x22, 0x11, 0x9, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
err := binary.Decode(buffer, &s)
if err != nil {
fmt.Println("binary.Decode failed:", err)
}
fmt.Printf("%+v", s)
// Output:
// {A:287454020 B:-5 C:hello}
}
func ExampleEncoder() {
encoder := binary.NewEncoder(100)
encoder.Uint32(0x11223344, false)
encoder.Varint(-5)
encoder.String("hello")
encodeResult := encoder.Buffer()
fmt.Printf("%#v", encodeResult)
// Output:
// []byte{0x44, 0x33, 0x22, 0x11, 0x9, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
}
func ExampleDecoder() {
buffer := []byte{0x44, 0x33, 0x22, 0x11, 0x9, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
decoder := binary.NewDecoder(buffer)
u32 := decoder.Uint32(false)
i, _ := decoder.Varint()
str := decoder.String()
fmt.Printf("%#v %#v %#v", u32, i, str)
// Output:
// 0x11223344 -5 "hello"
}
type S struct {
A uint32
B int
C string
}
func (this *S) Size() int {
size := binary.Sizeof(this.A) + binary.Sizeof(this.C) + binary.Sizeof(int16(this.B))
return size
}
func (this *S) Encode(buffer []byte) ([]byte, error) {
buff, err := binary.MakeEncodeBuffer(this, buffer)
if err != nil {
return nil, err
}
encoder := binary.NewEncoderBuffer(buff)
encoder.Value(this.A)
encoder.Int16(int16(this.B), false)
encoder.Value(this.C)
return encoder.Buffer(), nil
}
func (this *S) Decode(buffer []byte) error {
decoder := binary.NewDecoder(buffer)
decoder.Value(&this.A)
this.B = int(decoder.Int16(false))
decoder.Value(&this.C)
return nil
}
func ExampleBinarySerializer() {
/*
type S struct {
A uint32
B int
C string
}
func (this *S) Size() int {
size := binary.Sizeof(this.A) + binary.Sizeof(this.C) + binary.Sizeof(int16(this.B))
return size
}
func (this *S) Encode() ([]byte, error) {
encoder := binary.NewEncoder(this.Size())
encoder.Value(this.A)
encoder.Int16(int16(this.B), false)
encoder.Value(this.C)
return encoder.Buffer(), nil
}
func (this *S) Decode(buffer []byte) error {
decoder := binary.NewDecoder(buffer)
decoder.Value(&this.A)
this.B = int(decoder.Int16(false))
decoder.Value(&this.C)
return nil
}
*/
var data, dataDecode S
data.A = 0x11223344
data.B = -5
data.C = "hello"
b, err := binary.Encode(&data, nil)
if err != nil {
fmt.Println("binary.Encode failed:", err)
}
err = binary.Decode(b, &dataDecode)
if err != nil {
fmt.Println("binary.Decode failed:", err)
}
fmt.Printf("%+v\n%#v\n%+v", data, b, dataDecode)
// Output:
// {A:287454020 B:-5 C:hello}
// []byte{0x44, 0x33, 0x22, 0x11, 0xfb, 0xff, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
// {A:287454020 B:-5 C:hello}
}
func ExampleSizeof() {
var s struct {
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float32 float32
Float64 float64
Complex64 complex64
Complex128 complex128
Array [10]uint8
Bool bool
BoolArray [100]bool
Uint32Array [10]uint32
}
buf := bytes.NewBuffer(make([]byte, 0, 1024))
coder := gob.NewEncoder(buf)
coder.Encode(s)
gobSize := len(buf.Bytes())
stdSize := std.Size(s)
size := binary.Sizeof(s)
fmt.Printf("Sizeof(s) = %d\nstd Size(s)= %d\ngob Size(s)= %d", size, stdSize, gobSize)
// Output:
// Sizeof(s) = 133
// std Size(s)= 217
// gob Size(s)= 412
}
func ExampleRegStruct() {
type someRegedStruct struct {
A int `binary:"ignore"`
B uint64 `binary:"packed"`
C string
D uint
}
binary.RegStruct((*someRegedStruct)(nil))
var data = someRegedStruct{1, 2, "hello", 3}
b, err := binary.Encode(data, nil)
if err != nil {
fmt.Println(err)
}
if size := binary.Sizeof(data); size != len(b) {
fmt.Printf("RegedStruct got %+v %+v\nneed %+v\n", len(b), b, size)
}
fmt.Printf("Encode reged struct:\n%+v\nsize=%d result=%#v", data, len(b), b)
// Output:
// Encode reged struct:
// {A:1 B:2 C:hello D:3}
// size=8 result=[]byte{0x2, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x3}
}