-
Notifications
You must be signed in to change notification settings - Fork 1
/
varint_test.go
109 lines (99 loc) · 2.54 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
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
package varint
import (
"encoding/binary"
"fmt"
"math/rand"
"testing"
)
func TestVarInt(t *testing.T) {
var buf [16]byte
for bits := 0; bits <= 64; bits++ {
var value uint64 = 0xAAAAAAAAAAAAAAAA >> (64 - bits)
t.Run(fmt.Sprintf("bits_%d-", bits), func(t *testing.T) {
n1 := Encode(buf[:], value)
out, n2 := Decode(buf[:])
if value != out {
t.Fatalf("bits %d, expect value %X, got value %X", bits, value, out)
}
if n1 != n2 {
t.Fatalf("bits %d, expect length %d, got length %d", bits, n1, n2)
}
})
}
}
func TestTruncated(t *testing.T) {
var buf []byte
if l := Encode(buf, 1234); l != 0 {
t.Fatalf("expected 0, got %d", l)
}
if l, v := Decode(buf); l != 0 || v != 0 {
t.Fatalf("expected 0 and 0, got %d and %d", l, v)
}
}
var out int
var buf0 = make([]byte, 1024)
var buf = buf0[:rand.Int()%1010]
func BenchmarkEncode(b *testing.B) {
for nBits := 7; nBits < 64; nBits += 7 {
var value uint64 = 0xAAAAAAAAAAAAAAAA >> (64 - nBits)
b.Run(fmt.Sprintf("bits=%d", nBits), func(b *testing.B) {
for n := 0; n < b.N; n++ {
out = Encode(buf, value)
}
})
}
}
func BenchmarkDecode(b *testing.B) {
for nBits := 7; nBits < 64; nBits += 7 {
var value uint64 = 0xAAAAAAAAAAAAAAAA >> (64 - nBits)
Encode(buf, value)
b.Run(fmt.Sprintf("bits=%d", nBits), func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, out = Decode(buf)
}
})
}
}
func BenchmarkStdEncode(b *testing.B) {
for nBits := 7; nBits < 64; nBits += 7 {
var value uint64 = 0xAAAAAAAAAAAAAAAA >> (64 - nBits)
b.Run(fmt.Sprintf("bits=%d", nBits), func(b *testing.B) {
for n := 0; n < b.N; n++ {
out = binary.PutUvarint(buf, value)
}
})
}
}
func BenchmarkStdDecode(b *testing.B) {
for nBits := 7; nBits < 64; nBits += 7 {
var value uint64 = 0xAAAAAAAAAAAAAAAA >> (64 - nBits)
binary.PutUvarint(buf, value)
b.Run(fmt.Sprintf("bits=%d", nBits), func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, out = binary.Uvarint(buf)
}
})
}
}
func BenchmarkReadWrite(b *testing.B) {
for nBits := 7; nBits < 64; nBits += 7 {
var value uint64 = 0xAAAAAAAAAAAAAAAA >> (64 - nBits)
b.Run(fmt.Sprintf("bits=%d", nBits), func(b *testing.B) {
for n := 0; n < b.N; n++ {
Encode(buf, value)
_, out = Decode(buf)
}
})
}
}
func BenchmarkStdReadWrite(b *testing.B) {
for nBits := 7; nBits < 64; nBits += 7 {
var value uint64 = 0xAAAAAAAAAAAAAAAA >> (64 - nBits)
b.Run(fmt.Sprintf("bits=%d", nBits), func(b *testing.B) {
for n := 0; n < b.N; n++ {
binary.PutUvarint(buf, value)
_, out = binary.Uvarint(buf)
}
})
}
}