-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathbuffer_test.go
More file actions
329 lines (291 loc) · 8.78 KB
/
Copy pathbuffer_test.go
File metadata and controls
329 lines (291 loc) · 8.78 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package commit
import (
"bytes"
"testing"
"time"
"unsafe"
"github.com/kelindar/bitmap"
"github.com/stretchr/testify/assert"
)
/*
cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
BenchmarkQueue/u16-rw-8 154 7691836 ns/op 19 B/op 0 allocs/op
BenchmarkQueue/u16-next-8 214 5542922 ns/op 7 B/op 0 allocs/op
BenchmarkQueue/u32-rw-8 152 7743216 ns/op 20 B/op 0 allocs/op
BenchmarkQueue/u32-next-8 212 5616605 ns/op 7 B/op 0 allocs/op
BenchmarkQueue/u64-rw-8 148 8000536 ns/op 20 B/op 0 allocs/op
BenchmarkQueue/u64-next-8 194 6126377 ns/op 7 B/op 0 allocs/op
BenchmarkQueue/str-rw-8 91 12935521 ns/op 33 B/op 0 allocs/op
BenchmarkQueue/str-next-8 98 10901156 ns/op 15 B/op 0 allocs/op
BenchmarkQueue/bool-rw-8 169 6950441 ns/op 18 B/op 0 allocs/op
BenchmarkQueue/bool-next-8 228 5195821 ns/op 6 B/op 0 allocs/op
*/
func BenchmarkQueue(b *testing.B) {
const count = 1000000
run("u16-rw", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count*2; i += 2 {
buf.PutUint16(Put, i, uint16(i))
}
for r.Seek(buf); r.Next(); {
_ = r.Uint16()
}
})
run("u16-next", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count; i++ {
buf.PutUint16(Put, i, uint16(i))
}
for r.Seek(buf); r.Next(); {
_ = r.Uint16()
}
})
run("u32-rw", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count*2; i += 2 {
buf.PutUint32(Put, i, i)
}
for r.Seek(buf); r.Next(); {
_ = r.Uint32()
}
})
run("u32-next", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count; i++ {
buf.PutUint32(Put, i, i)
}
for r.Seek(buf); r.Next(); {
_ = r.Uint32()
}
})
run("u64-rw", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count*2; i += 2 {
buf.PutUint64(Put, i, uint64(i))
}
for r.Seek(buf); r.Next(); {
_ = r.Uint64()
}
})
run("u64-next", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count; i++ {
buf.PutUint64(Put, i, uint64(i))
}
for r.Seek(buf); r.Next(); {
_ = r.Uint64()
}
})
run("str-rw", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count*2; i += 2 {
buf.PutString(Put, i, "hello world")
}
for r.Seek(buf); r.Next(); {
_ = r.String()
}
})
run("str-next", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count; i++ {
buf.PutString(Put, i, "hello world")
}
for r.Seek(buf); r.Next(); {
_ = r.String()
}
})
run("bool-rw", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count*2; i += 2 {
buf.PutBool(i, true)
}
for r.Seek(buf); r.Next(); {
_ = r.Bool()
}
})
run("bool-next", b, count, func(buf *Buffer, r *Reader) {
for i := uint32(0); i < count; i++ {
buf.PutBool(i, true)
}
for r.Seek(buf); r.Next(); {
_ = r.Bool()
}
})
}
// Run runs a single benchmark
func run(name string, b *testing.B, count int, fn func(buf *Buffer, r *Reader)) {
b.Run(name, func(b *testing.B) {
buf := NewBuffer(count * 20)
r := NewReader()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
buf.Reset("test")
fn(buf, r)
}
})
}
func TestSizeof(t *testing.T) {
assert.Equal(t, 96, int(unsafe.Sizeof(Reader{})))
assert.Equal(t, 80, int(unsafe.Sizeof(Buffer{})))
}
func TestReadWrite(t *testing.T) {
buf := NewBuffer(0)
buf.PutInt16(Put, 10, 100)
buf.PutInt16(Put, 11, 100)
buf.PutInt32(Put, 20, 200)
buf.PutInt32(Put, 21, 200)
buf.PutInt64(Put, 30, 300)
buf.PutInt64(Put, 31, 300)
buf.PutUint16(Put, 40, 400)
buf.PutUint16(Put, 41, 400)
buf.PutUint32(Put, 50, 500)
buf.PutUint32(Put, 51, 500)
buf.PutUint64(Put, 60, 600)
buf.PutUint64(Put, 61, 600)
buf.PutFloat32(Put, 70, 700)
buf.PutFloat32(Put, 71, 700)
buf.PutFloat64(Put, 80, 800)
buf.PutFloat64(Put, 81, 800)
buf.PutString(Put, 90, "900")
buf.PutString(Put, 91, "hello world")
buf.PutBytes(Put, 100, []byte("binary"))
buf.PutBool(110, true)
buf.PutBool(111, false)
buf.PutInt(Put, 120, 1000)
buf.PutUint(Put, 130, 1100)
buf.PutNumber(Put, 140, 12.34)
// Read values back
r := NewReader()
r.Seek(buf)
assert.True(t, r.Next())
assert.Equal(t, int16(100), r.Int16())
assert.True(t, r.Next())
assert.Equal(t, int16(100), r.Int16())
assert.True(t, r.Next())
assert.Equal(t, int32(200), r.Int32())
assert.True(t, r.Next())
assert.Equal(t, int32(200), r.Int32())
assert.True(t, r.Next())
assert.Equal(t, int64(300), r.Int64())
assert.True(t, r.Next())
assert.Equal(t, int64(300), r.Int64())
assert.True(t, r.Next())
assert.Equal(t, uint16(400), r.Uint16())
assert.True(t, r.Next())
assert.Equal(t, uint16(400), r.Uint16())
assert.True(t, r.Next())
assert.Equal(t, uint32(500), r.Uint32())
assert.True(t, r.Next())
assert.Equal(t, uint32(500), r.Uint32())
assert.True(t, r.Next())
assert.Equal(t, uint64(600), r.Uint64())
assert.True(t, r.Next())
assert.Equal(t, uint64(600), r.Uint64())
assert.True(t, r.Next())
assert.Equal(t, float32(700), r.Float32())
assert.True(t, r.Next())
assert.Equal(t, float32(700), r.Float32())
assert.True(t, r.Next())
assert.Equal(t, float64(800), r.Float64())
assert.True(t, r.Next())
assert.Equal(t, float64(800), r.Float64())
assert.True(t, r.Next())
assert.Equal(t, "900", r.String())
assert.True(t, r.Next())
assert.Equal(t, "hello world", r.String())
assert.True(t, r.Next())
assert.Equal(t, "binary", string(r.Bytes()))
assert.True(t, r.Next())
assert.Equal(t, true, r.Bool())
assert.True(t, r.Next())
assert.Equal(t, false, r.Bool())
assert.True(t, r.Next())
assert.Equal(t, int(1000), r.Int())
assert.True(t, r.Next())
assert.Equal(t, uint(1100), r.Uint())
assert.True(t, r.Next())
assert.Equal(t, 12.34, r.Number())
assert.False(t, r.Next())
}
func TestBufferClone(t *testing.T) {
buf := NewBuffer(0)
buf.PutInt16(Put, 10, 100)
buf.PutString(Put, 20, "hello")
cloned := buf.Clone()
assert.EqualValues(t, buf, cloned)
}
func TestPutNil(t *testing.T) {
buf := NewBuffer(0)
buf.PutAny(PutTrue, 0, nil)
r := NewReader()
r.Seek(buf)
assert.True(t, r.Next())
assert.True(t, r.Bool())
}
func TestPutTime(t *testing.T) {
buf := NewBuffer(0)
buf.PutAny(Put, 0, time.Unix(0, 0))
r := NewReader()
r.Seek(buf)
assert.True(t, r.Next())
assert.NotEmpty(t, r.Bytes())
}
func TestPutBitmap(t *testing.T) {
buf := NewBuffer(0)
buf.PutBitmap(Insert, 0, bitmap.Bitmap{0xff})
r := NewReader()
r.Seek(buf)
assert.True(t, r.Next())
assert.Equal(t, Insert, r.Type)
}
func TestBufferWriteTo(t *testing.T) {
input := NewBuffer(0)
input.Column = "test"
input.PutInt16(Put, 10, 100)
input.PutString(Put, 20, "hello")
buffer := bytes.NewBuffer(nil)
n, err := input.WriteTo(buffer)
assert.NoError(t, err)
assert.Equal(t, int64(buffer.Len()), n)
assert.Equal(t, int64(36), n)
output := NewBuffer(0)
m, err := output.ReadFrom(buffer)
assert.NoError(t, err)
assert.Equal(t, int64(buffer.Len()), m)
assert.Equal(t, input, output)
}
func TestBufferWriteToFailures(t *testing.T) {
buf := NewBuffer(0)
buf.Column = "test"
buf.PutInt16(Put, 10, 100)
buf.PutString(Put, 20, "hello")
for size := 0; size < 30; size++ {
output := &limitWriter{Limit: size}
_, err := buf.WriteTo(output)
assert.Error(t, err)
}
}
func TestBufferReadFromFailures(t *testing.T) {
input := NewBuffer(0)
input.Column = "test"
input.PutInt16(Put, 10, 100)
input.PutString(Put, 20, "hello")
buffer := bytes.NewBuffer(nil)
n, err := input.WriteTo(buffer)
assert.NoError(t, err)
for size := 0; size < int(n)-1; size++ {
output := NewBuffer(0)
_, err := output.ReadFrom(bytes.NewReader(buffer.Bytes()[:size]))
assert.Error(t, err)
}
}
func FuzzBufferString(f *testing.F) {
f.Add(uint32(1), "test")
f.Fuzz(func(t *testing.T, i uint32, v string) {
buf := NewBuffer(0)
buf.PutString(Put, i, v)
r := NewReader()
r.Seek(buf)
assert.True(t, r.Next())
assert.Equal(t, v, r.String())
})
}
func TestOpString(t *testing.T) {
for i := 0; i < 255; i++ {
assert.NotEmpty(t, OpType(i).String())
}
}