Skip to content

Commit 3b0b4ee

Browse files
committed
add unittest case
1 parent bf4d5fb commit 3b0b4ee

File tree

1 file changed

+138
-8
lines changed

1 file changed

+138
-8
lines changed

encoding_test.go

+138-8
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,145 @@ func TestDecode(t *testing.T) {
106106
assert.Equal(t, uint(256), v)
107107
}
108108

109+
// case: 畸形编码的byte slice,没有设置结束标志位
110+
{
111+
// 0x82中的0x80表示期望后面还有数组,但是实际上是没有的
112+
// 不能报错,并且能够正确处理到期望的值
113+
v := Decode[uint]([]byte{0x80, 0x82})
114+
assert.Equal(t, uint(256), v)
115+
}
116+
117+
// case: byte slice中只有一部分是varint编码,后面还有其它的内容,但是正确设置了varint的结束标志位
118+
{
119+
// 期望:只读取到第一个无符号数字,后边的bytes不管是无符号数字的varint编码还是其它内容都不会读取
120+
v := Decode[uint]([]byte{
121+
0x80, 0x2,
122+
0xFF, 0xFF,
123+
0xFF, 0xFF,
124+
0xFF, 0xFF,
125+
0xFF, 0xFF,
126+
0xFF, 0xFF,
127+
0xFF, 0xFF,
128+
})
129+
assert.Equal(t, uint(256), v)
130+
}
131+
132+
// case: 解码的时候会发生溢出
133+
{
134+
v := Decode[uint8]([]byte{
135+
0xFF, 0xFF,
136+
0xFF, 0xFF,
137+
0xFF, 0xFF,
138+
0xFF, 0xFF,
139+
0xFF, 0xFF,
140+
0xFF, 0xFF,
141+
0xFF, 0xFF,
142+
})
143+
assert.Equal(t, uint8(0xff), v)
144+
}
145+
109146
}
110147

111148
func TestDecodeSlice(t *testing.T) {
112-
slice := DecodeSlice[uint]([]byte{
113-
0x1, // 1
114-
0x7f, // 127
115-
0x0, // 0
116-
0xff, 0x1, // 255
117-
0x80, 0x2, // 256
118-
})
119-
assert.Equal(t, []uint{1, 127, 0, 255, 256}, slice)
149+
150+
// case: 多个无符号整数解码,varint byte数组是合法的
151+
{
152+
slice := DecodeSlice[uint]([]byte{
153+
0x1, // 1
154+
0x7f, // 127
155+
0x0, // 0
156+
0xff, 0x1, // 255
157+
0x80, 0x2, // 256
158+
})
159+
assert.Equal(t, []uint{1, 127, 0, 255, 256}, slice)
160+
}
161+
162+
// case: 1个无符号整数解码,varint byte数组畸形,但是不会发生溢出
163+
{
164+
// 期望能够正常解码,不会发生报错,并且返回值是期望值
165+
slice := DecodeSlice[uint]([]byte{
166+
0x81, // 1
167+
})
168+
assert.Equal(t, []uint{1}, slice)
169+
}
170+
171+
// case: 多个无符号整数解码,varint byte数组畸形,但是不会发生溢出
172+
{
173+
// 期望能够正常解码,不会发生报错,并且返回值是期望值
174+
slice := DecodeSlice[uint]([]byte{
175+
0x1, // 1
176+
0x7f, // 127
177+
0x0, // 0
178+
0xff, 0x1, // 255
179+
0x80, 0x82, // 256
180+
})
181+
assert.Equal(t, []uint{1, 127, 0, 255, 256}, slice)
182+
}
183+
184+
// case: varint byte数组是畸形的,会发生溢出
185+
{
186+
slice := DecodeSlice[uint8]([]byte{
187+
0xFF,
188+
0xFF,
189+
0xFF,
190+
0xFF,
191+
0xFF,
192+
0xFF,
193+
0xFF,
194+
0xFF,
195+
0xFF,
196+
0xFF,
197+
0xFF,
198+
0xFF,
199+
0xFF,
200+
0xFF,
201+
0xFF,
202+
0xFF,
203+
0xFF,
204+
0xFF,
205+
0xFF,
206+
0xFF,
207+
0xFF,
208+
0xFF,
209+
0xFF,
210+
0xFF,
211+
0xFF,
212+
0xFF,
213+
0xFF,
214+
0xFF,
215+
0xFF,
216+
0xFF,
217+
0xFF,
218+
0xFF,
219+
0xFF,
220+
0xFF,
221+
0xFF,
222+
0xFF,
223+
0xFF,
224+
0xFF,
225+
0xFF,
226+
0xFF,
227+
0xFF,
228+
0xFF,
229+
0xFF,
230+
0xFF,
231+
0xFF,
232+
0xFF,
233+
0xFF,
234+
0xFF,
235+
0xFF,
236+
0xFF,
237+
0xFF,
238+
0xFF,
239+
0xFF,
240+
0xFF,
241+
0xFF,
242+
0xFF,
243+
0xFF,
244+
0xFF,
245+
0xFF,
246+
})
247+
assert.Equal(t, []uint8{0xff}, slice)
248+
}
249+
120250
}

0 commit comments

Comments
 (0)