Skip to content

Commit 9e35285

Browse files
committed
add test case for Encode function
1 parent 5b3ac01 commit 9e35285

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

encoding_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package varint
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestEncode(t *testing.T) {
9+
10+
// case: value 0
11+
{
12+
v := Encode[uint64](0)
13+
assert.Equal(t, []byte{0x0}, v)
14+
}
15+
16+
// case: value 1
17+
{
18+
v := Encode[uint64](1)
19+
assert.Equal(t, []byte{0x1}, v)
20+
}
21+
22+
// case: value 2
23+
{
24+
v := Encode[uint64](2)
25+
assert.Equal(t, []byte{0x2}, v)
26+
}
27+
28+
// case: value 127
29+
{
30+
v := Encode[uint64](127)
31+
assert.Equal(t, []byte{0x7f}, v)
32+
}
33+
34+
// case: value 128
35+
{
36+
v := Encode[uint64](128)
37+
assert.Equal(t, []byte{0x80, 0x1}, v)
38+
}
39+
40+
// case: value 255
41+
{
42+
v := Encode[uint64](255)
43+
assert.Equal(t, []byte{0xff, 0x1}, v)
44+
}
45+
46+
// case: case: value 256
47+
{
48+
v := Encode[uint64](256)
49+
assert.Equal(t, []byte{0x80, 0x2}, v)
50+
}
51+
52+
}
53+
54+
func TestEncodeSlice(t *testing.T) {
55+
}
56+
57+
func TestDecode(t *testing.T) {
58+
var a uint64
59+
a = 123456
60+
bytes := Encode(a)
61+
t.Log(bytes)
62+
t.Log(Decode[uint64](bytes))
63+
}
64+
65+
func TestDecodeSlice(t *testing.T) {
66+
ints := []uint64{
67+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
68+
}
69+
bytes := EncodeSlice(ints)
70+
t.Log(len(bytes))
71+
t.Log(bytes)
72+
slice := DecodeSlice[uint64](bytes)
73+
t.Log(slice)
74+
}

0 commit comments

Comments
 (0)