-
Notifications
You must be signed in to change notification settings - Fork 3
/
encoder_test.go
160 lines (137 loc) · 3.54 KB
/
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
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
package y3
import (
"testing"
)
// JSON-like node:
// { '0x01': -1 }
// YoMo YomoCodec should ->
// 0x01 (is a node, sequence id=1)
// 0x01 (node value length is 1 byte)
// 0x01 (pvarint: -1)
func TestEncoderPrimitiveInt32(t *testing.T) {
expected := []byte{0x01, 0x01, 0x7F}
// 0x01 - SeqID=1
var prim = NewPrimitivePacketEncoder(0x01)
// Value = -1
prim.SetInt32Value(-1)
res := prim.Encode()
for i, p := range res {
if p != expected[i] {
t.Errorf("i=%v, expected=%#x, actual=%#x", i, expected[i], res[i])
}
}
}
// JSON-like node:
// { '0x01': "YoMo" }
// YoMo YomoCodec should ->
// 0x01 (is a node, sequence id=1)
// 0x04 (pvarint, node value length is 4 bytes)
// 0x59, 0x6F, 0x4D, 0x6F (utf-8 string: "YoMo")
func TestEncoderPrimitiveString(t *testing.T) {
expected := []byte{0x01, 0x04, 0x59, 0x6F, 0x4D, 0x6F}
// 0x01 - SeqID=1
var prim = NewPrimitivePacketEncoder(0x01)
// Value = "YoMo"
prim.SetStringValue("YoMo")
res := prim.Encode()
for i, p := range res {
if p != expected[i] {
t.Errorf("i=%v, expected=%v, actual=%v", i, expected[i], res[i])
}
}
}
// 0x81 : {
// 0x02: "YoMo",
// },
func TestEncoderNode1(t *testing.T) {
expected := []byte{0x81, 0x06, 0x02, 0x04, 0x59, 0x6F, 0x4D, 0x6F}
var prim = NewPrimitivePacketEncoder(0x02)
prim.SetStringValue("YoMo")
var node = NewNodePacketEncoder(0x01)
node.AddPrimitivePacket(prim)
res := node.Encode()
for i, p := range res {
if p != expected[i] {
t.Errorf("i=%v, expected=%#x, actual=%#x", i, expected[i], res[i])
}
}
}
// type bar struct {
// Name string
// }
// type foo struct {
// ID int
// *bar
// }
//
// var obj = &foo{ID: 1, bar: &bar{Name: "C"}}
//
// encode obj as:
//
// 0x81: {
// 0x02: 1,
// 0x83 : {
// 0x04: "C",
// },
// }
//
// to
//
// [0x81, 0x08, 0x02, 0x01, 0x01, 0x83, 0x03, 0x04, 0x01, 0x43]
func TestEncoderNode2(t *testing.T) {
expected := []byte{0x81, 0x08, 0x02, 0x01, 0x01, 0x83, 0x03, 0x04, 0x01, 0x43}
// 0x81 - node
var node1 = NewNodePacketEncoder(0x01)
// 0x02 - ID=1
var prim1 = NewPrimitivePacketEncoder(0x02)
prim1.SetInt32Value(1)
node1.AddPrimitivePacket(prim1)
// 0x83 - &bar{}
var node2 = NewNodePacketEncoder(0x03)
// 0x04 - Name: "C"
var prim2 = NewPrimitivePacketEncoder(0x04)
prim2.SetStringValue("C")
node2.AddPrimitivePacket(prim2)
node1.AddNodePacket(node2)
res := node1.Encode()
if len(expected) != len(res) {
t.Errorf("len(expected)=%v, len(res)=%v", len(expected), len(res))
}
for i, p := range res {
if p != expected[i] {
t.Errorf("i=%v, expected=%#x, actual=%#x", i, expected[i], res[i])
}
}
}
// 0x01 (is a node, sequence id=1)
// 0x04 (pvarint, node value length is 4 bytes)
// 0x59, 0x6F, 0x4D, 0x6F (utf-8 string: "YoMo")
func TestEncoderPrimitiveBinary(t *testing.T) {
expected := []byte{0x01, 0x03, 0x01, 0x23, 0xFF}
// 0x01 - SeqID=1
var prim = NewPrimitivePacketEncoder(0x01)
// Value = 0x0123FF
prim.SetBytes([]byte{0x01, 0x23, 0xFF})
res := prim.Encode()
for i, p := range res {
if p != expected[i] {
t.Errorf("i=%v, expected=%v, actual=%v", i, expected[i], res[i])
}
}
}
// 0x01 (is a node, sequence id=1)
// 0x04 (pvarint, node value length is 4 bytes)
// 0x59, 0x6F, 0x4D, 0x6F (utf-8 string: "YoMo")
func TestEncoderPrimitiveBinaryWithEmptyValue(t *testing.T) {
expected := []byte{0x01, 0x00}
// 0x01 - SeqID=1
var prim = NewPrimitivePacketEncoder(0x01)
// Value = 0x0123FF
prim.SetBytes([]byte{})
res := prim.Encode()
for i, p := range res {
if p != expected[i] {
t.Errorf("i=%v, expected=%v, actual=%v", i, expected[i], res[i])
}
}
}