-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmap_test.go
240 lines (217 loc) · 4.57 KB
/
map_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
package sproto
import (
"reflect"
"sort"
"testing"
)
/*
.NestData {
A 1 : string
B 3 : boolean
C 5 : integer
D 6 : double
}
.SimpleMapItem {
K 1 : integer
V 2 : string
}
.StructMapItem {
Key 3 : integer
Value 5 : NestData
}
.MapMsg {
SimpleMap 0 : *SimpleMapItem()
StructMap 1 : *StructMapItem()
MainIndexMap 2 : *NestData(A)
}
*/
type NestData struct {
A string `sproto:"string,1"`
B bool `sproto:"boolean,3"`
C int `sproto:"integer,5"`
D float64 `sproto:"double,6"`
}
type SimpleMapItem struct {
K int `sproto:"integer,1"`
V string `sproto:"string,2"`
}
type SimpleMapPtrItem struct {
K *int `sproto:"integer,1"`
V *string `sproto:"string,2"`
}
type StructMapItem struct {
Key int `sproto:"integer,3"`
Value *NestData `sproto:"struct,5"`
}
type MapMsg struct {
SimpleMap map[int]string `sproto:"struct,0,array,key=1,value=2,subtype=simpleMapItem"`
SimpleMapPtr map[int]string `sproto:"struct,1,array,key=1,value=2,subtype=simpleMapPtrItem"` // 支持item中定义为指针类型,但map定义为值类型
StructMap map[int]*NestData `sproto:"struct,2,array,key=3,value=5,subtype=structMapItem"`
MainIndexMap map[string]*NestData `sproto:"struct,3,array,key=1"` // value=all
simpleMapItem *SimpleMapItem
simpleMapPtrItem *SimpleMapPtrItem
structMapItem *StructMapItem
}
type ArrayMsg struct {
SimpleMap []*SimpleMapItem `sproto:"struct,0,array"`
SimpleMapPtr []*SimpleMapPtrItem `sproto:"struct,1,array"`
StructMap []*StructMapItem `sproto:"struct,2,array"`
MainIndexMap []*NestData `sproto:"struct,3,array"`
}
var (
mapMsg = MapMsg{
SimpleMap: map[int]string{
1: "v1",
2: "v2",
},
SimpleMapPtr: map[int]string{
1: "v1",
2: "v2",
},
StructMap: map[int]*NestData{
11: {
A: "11va",
B: true,
C: 123,
D: 0.123,
},
12: {
A: "12va",
B: false,
C: 456,
D: 0.456,
},
},
MainIndexMap: map[string]*NestData{
"11va": {
A: "11va",
B: true,
C: 123,
D: 0.123,
},
"12va": {
A: "12va",
B: false,
C: 456,
D: 0.456,
},
},
}
arrayMsg = ArrayMsg{
SimpleMap: []*SimpleMapItem{
{K: 1, V: "v1"},
{K: 2, V: "v2"},
},
SimpleMapPtr: []*SimpleMapPtrItem{
{K: Int(1), V: String("v1")},
{K: Int(2), V: String("v2")},
},
StructMap: []*StructMapItem{
{
Key: 11,
Value: &NestData{
A: "11va",
B: true,
C: 123,
D: 0.123,
},
},
{
Key: 12,
Value: &NestData{
A: "12va",
B: false,
C: 456,
D: 0.456,
},
},
},
MainIndexMap: []*NestData{
{
A: "11va",
B: true,
C: 123,
D: 0.123,
},
{
A: "12va",
B: false,
C: 456,
D: 0.456,
},
},
}
)
func TestMapEncode(t *testing.T) {
mapMsgData, err := Encode(&mapMsg)
if err != nil {
t.Error(err)
return
}
mapMsg2 := MapMsg{}
used, err := Decode(mapMsgData, &mapMsg2)
if err != nil {
t.Error(err)
return
}
if used != len(mapMsgData) {
t.Error("decode failed: unexpected used")
return
}
if !reflect.DeepEqual(mapMsg, mapMsg2) {
t.Error("mapMsg is not equal to mapMsg2")
return
}
}
func TestArrayToMap(t *testing.T) {
arrayMsgData, err := Encode(&arrayMsg)
if err != nil {
t.Error(err)
return
}
mapMsg2 := MapMsg{}
used, err := Decode(arrayMsgData, &mapMsg2)
if err != nil {
t.Error(err)
return
}
if used != len(arrayMsgData) {
t.Error("decode failed: unexpected used")
return
}
if !reflect.DeepEqual(mapMsg, mapMsg2) {
t.Error("mapMsg is not equal to mapMsg2")
return
}
}
func TestMapToArray(t *testing.T) {
mapMsgData, err := Encode(&mapMsg)
if err != nil {
t.Error(err)
return
}
arrayMsg2 := ArrayMsg{}
used, err := Decode(mapMsgData, &arrayMsg2)
if err != nil {
t.Error(err)
return
}
if used != len(mapMsgData) {
t.Error("decode failed: unexpected used")
return
}
// 数组排序
sort.Slice(arrayMsg2.SimpleMap, func(i, j int) bool {
return arrayMsg2.SimpleMap[i].K < arrayMsg2.SimpleMap[j].K
})
sort.Slice(arrayMsg2.StructMap, func(i, j int) bool {
return arrayMsg2.StructMap[i].Key < arrayMsg2.StructMap[j].Key
})
sort.Slice(arrayMsg2.MainIndexMap, func(i, j int) bool {
return arrayMsg2.MainIndexMap[i].A < arrayMsg2.MainIndexMap[j].A
})
if !reflect.DeepEqual(arrayMsg, arrayMsg2) {
t.Error("arrayMsg is not equal to arrayMsg2")
return
}
}