-
Notifications
You must be signed in to change notification settings - Fork 12
/
varint_test.go
83 lines (68 loc) · 1.87 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package msgio
import (
"bytes"
"encoding/binary"
"io"
"testing"
"github.com/multiformats/go-varint"
)
func TestVarintReadWrite(t *testing.T) {
buf := bytes.NewBuffer(nil)
writer := NewVarintWriter(buf)
reader := NewVarintReader(buf)
SubtestReadWrite(t, writer, reader)
}
func TestVarintReadWriteMsg(t *testing.T) {
buf := bytes.NewBuffer(nil)
writer := NewVarintWriter(buf)
reader := NewVarintReader(buf)
SubtestReadWriteMsg(t, writer, reader)
}
func TestVarintReadWriteMsgSync(t *testing.T) {
buf := bytes.NewBuffer(nil)
writer := NewVarintWriter(buf)
reader := NewVarintReader(buf)
SubtestReadWriteMsgSync(t, writer, reader)
}
func TestVarintWrite(t *testing.T) {
SubtestVarintWrite(t, []byte("hello world"))
SubtestVarintWrite(t, []byte("hello world hello world hello world"))
SubtestVarintWrite(t, make([]byte, 1<<20))
SubtestVarintWrite(t, []byte(""))
}
func SubtestVarintWrite(t *testing.T, msg []byte) {
buf := bytes.NewBuffer(nil)
writer := NewVarintWriter(buf)
if err := writer.WriteMsg(msg); err != nil {
t.Fatal(err)
}
bb := buf.Bytes()
sbr := simpleByteReader{R: buf}
length, err := varint.ReadUvarint(&sbr)
if err != nil {
t.Fatal(err)
}
t.Logf("checking varint is %d", len(msg))
if int(length) != len(msg) {
t.Fatalf("incorrect varint: %d != %d", length, len(msg))
}
lbuf := make([]byte, binary.MaxVarintLen64)
n := varint.PutUvarint(lbuf, length)
bblen := int(length) + n
t.Logf("checking wrote (%d + %d) bytes", length, n)
if len(bb) != bblen {
t.Fatalf("wrote incorrect number of bytes: %d != %d", len(bb), bblen)
}
}
func TestVarintReadClose(t *testing.T) {
r, w := io.Pipe()
writer := NewVarintWriter(w)
reader := NewVarintReader(r)
SubtestReadClose(t, writer, reader)
}
func TestVarintWriteClose(t *testing.T) {
r, w := io.Pipe()
writer := NewVarintWriter(w)
reader := NewVarintReader(r)
SubtestWriteClose(t, writer, reader)
}