-
Notifications
You must be signed in to change notification settings - Fork 2
/
flit_test.go
166 lines (141 loc) · 3.4 KB
/
flit_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package flit
import (
"encoding/binary"
"encoding/hex"
"testing"
)
var tests = []struct {
hex string
val uint64
}{
{"01", 0},
{"ff", 127},
{"0202", 128},
{"feff", 16383},
{"040002", 16384},
{"fcffff", 2097151},
{"08000002", 2097152},
{"f8ffffff", 268435455},
{"1000000002", 268435456},
{"f0ffffffff", 34359738367},
{"200000000002", 34359738368},
{"e0ffffffffff", 4398046511103},
{"40000000000002", 4398046511104},
{"c0ffffffffffff", 562949953421311},
{"8000000000000002", 562949953421312},
{"80ffffffffffffff", 72057594037927935},
{"000000000000000001", 72057594037927936},
{"00ffffffffffffffff", 18446744073709551615},
}
func TestPutUint64(t *testing.T) {
for _, gold := range tests {
buf := [...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
n := PutUint64(buf[:], gold.val)
got := hex.EncodeToString(buf[:n])
if got != gold.hex {
t.Errorf("%d: got 0x%s, want 0x%s", gold.val, got, gold.hex)
continue
}
}
}
func TestUint64(t *testing.T) {
for _, gold := range tests {
buf := [...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
n, err := hex.Decode(buf[:], []byte(gold.hex))
if err != nil {
t.Fatal(err)
}
got, read := Uint64(buf[:])
if got != gold.val || read != n {
t.Errorf("got %d, want %d; read %d bytes, want %d", got, gold.val, read, n)
}
}
}
func TestSigned64(t *testing.T) {
buf := make([]byte, 9)
// verify preservation of each bit for
// both negative and positive range
for bit := uint(0); bit < 63; bit++ {
for _, u := range []uint64{0, 1 << 63} {
u |= uint64(1) << bit
want := int64(u)
wrote := PutInt64(buf, want)
got, read := Int64(buf)
if read != wrote {
t.Errorf("%016x: wrote %d bytes, read %d", u, wrote, read)
}
if got != want {
t.Errorf("%016x: got %d for %d", u, got, want)
}
}
}
}
// benchmark data
var values [18]uint64
var serials [18][]byte
var varints [18][]byte
func init() {
for i, gold := range tests {
values[i] = gold.val
buf := make([]byte, 9)
if _, err := hex.Decode(buf, []byte(gold.hex)); err != nil {
panic(err)
}
serials[i] = buf
buf = make([]byte, 10)
binary.PutUvarint(buf, gold.val)
varints[i] = buf
}
}
// prevents compiler optimization
var ckv uint64
var ckn int
var ckb byte
func BenchmarkPutUint64(b *testing.B) {
b.SetBytes(8)
buf := make([]byte, 9)
for i := 0; i < b.N; i++ {
ckn += PutUint64(buf, values[i%len(values)])
}
}
// BenchmarkPutUint64Raw is for comparison with fixed-width values.
func BenchmarkPutUint64Raw(b *testing.B) {
b.SetBytes(8)
buf := make([]byte, 8)
for i := 0; i < b.N; i++ {
binary.LittleEndian.PutUint64(buf, values[i%len(values)])
ckb += buf[0]
}
}
// BenchmarkPutUint64VQL is for comparison the core library.
func BenchmarkPutUint64VQL(b *testing.B) {
b.SetBytes(8)
buf := make([]byte, 10)
for i := 0; i < b.N; i++ {
ckn += binary.PutUvarint(buf, values[i%len(values)])
}
}
func BenchmarkUint64(b *testing.B) {
b.SetBytes(8)
for i := 0; i < b.N; i++ {
v, n := Uint64(serials[i%len(serials)])
ckv += v
ckn += n
}
}
// BenchmarkUint64Raw is for comparison with fixed-width values.
func BenchmarkUint64Raw(b *testing.B) {
b.SetBytes(8)
for i := 0; i < b.N; i++ {
ckv += binary.LittleEndian.Uint64(serials[i%len(serials)])
}
}
// BenchmarkUint64VQL is for comparison with the core library.
func BenchmarkUint64VQL(b *testing.B) {
b.SetBytes(8)
for i := 0; i < b.N; i++ {
v, n := binary.Uvarint(varints[i%len(varints)])
ckv += v
ckn += n
}
}