forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_decode_test.go
60 lines (53 loc) · 1017 Bytes
/
binary_decode_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
package test
import (
"bytes"
"encoding/binary"
"testing"
)
type message struct {
Flag uint8
Version uint8
Type uint64
}
// 定义编码函数
func EncodeMessage(msg message) ([]byte, error) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, msg)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// 定义解码函数
func DecodeMessage(data []byte) (message, error) {
var msg message
buf := bytes.NewReader(data)
err := binary.Read(buf, binary.BigEndian, &msg)
if err != nil {
return message{}, err
}
return msg, nil
}
// go test -timeout 30s -run ^TestOk github.com/hootrhino/rulex/test -v -count=1
func TestEncodeMessage(t *testing.T) {
// 创建一个消息
msg := message{
Flag: 1,
Version: 2,
Type: 3,
}
// 编码消息
encoded, err := EncodeMessage(msg)
if err != nil {
t.Log(err)
return
}
t.Log(encoded)
// 解码消息
decoded, err := DecodeMessage(encoded)
if err != nil {
t.Log(err)
return
}
t.Log(decoded)
}