/
varint_test.go
69 lines (64 loc) · 1.7 KB
/
varint_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
package sqlite4_varint_bench
import (
"bytes"
"testing"
"github.com/dchest/varuint"
"github.com/mohae/uvarint"
)
func TestUvarint(t *testing.T) {
for i, test := range tests {
b := make([]byte, len(test.encoded))
n := uvarint.PutUvarint(b, test.decoded)
if n != test.n {
t.Errorf("encode %d: got %d want %d", i, n, test.n)
}
if !bytes.Equal(b, test.encoded) {
t.Errorf("encode %d: got %v want %v", i, b[0:n], test.encoded)
}
v, n := uvarint.Uvarint(test.encoded)
if n != test.n {
t.Errorf("decode %d: got %d want %d", i, n, test.n)
}
if v != test.decoded {
t.Errorf("decode %d: got %d want %d", i, v, test.decoded)
}
}
}
func TestVaruint(t *testing.T) {
for i, test := range tests {
b := make([]byte, len(test.encoded))
n := varuint.PutUint64(b, test.decoded)
if n != test.n {
t.Errorf("encode %d: got %d want %d", i, n, test.n)
}
if !bytes.Equal(b, test.encoded) {
t.Errorf("encode %d: got %v want %v", i, b[0:n], test.encoded)
}
v, n := varuint.Uint64(test.encoded)
if n != test.n {
t.Errorf("decode %d: got %d want %d", i, n, test.n)
}
if v != test.decoded {
t.Errorf("decode %d: got %d want %d", i, v, test.decoded)
}
}
}
func TestCoackroach(t *testing.T) {
for i, test := range tests {
b := make([]byte, len(test.encoded))
n := putUvarint(b, test.decoded)
if n != test.n {
t.Errorf("encode %d: got %d want %d", i, n, test.n)
}
if !bytes.Equal(b, test.encoded) {
t.Errorf("encode %d: got %v want %v", i, b[0:n], test.encoded)
}
v, n := getUvarint(test.encoded)
if n != test.n {
t.Errorf("decode %d: got %d want %d", i, n, test.n)
}
if v != test.decoded {
t.Errorf("decode %d: got %d want %d", i, v, test.decoded)
}
}
}