-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcommit_test.go
More file actions
213 lines (184 loc) · 5 KB
/
Copy pathcommit_test.go
File metadata and controls
213 lines (184 loc) · 5 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
// 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"
"fmt"
"io"
"sync/atomic"
"testing"
"github.com/kelindar/bitmap"
"github.com/stretchr/testify/assert"
)
/*
cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
BenchmarkColumn/chunkOf-8 8466814 136.2 ns/op 0 B/op 0 allocs/op
*/
func BenchmarkColumn(b *testing.B) {
b.Run("chunkOf", func(b *testing.B) {
var temp bitmap.Bitmap
temp.Grow(2 * chunkSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
for i := 0; i < 100; i++ {
Chunk(1).OfBitmap(temp)
}
}
})
}
func TestCommitClone(t *testing.T) {
commit := Commit{
Updates: []*Buffer{{
buffer: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
chunks: []header{{
Chunk: 0,
}},
}},
}
clone := commit.Clone()
assert.EqualValues(t, commit, clone)
}
func TestWriterChannel(t *testing.T) {
w := make(Channel, 1)
w.Append(Commit{
Chunk: 123,
})
out := <-w
assert.Equal(t, 123, int(out.Chunk))
}
func TestChunkMinMax(t *testing.T) {
tests := []struct {
chunk Chunk
min, max uint32
}{
{chunk: 0, min: 0, max: chunkSize - 1},
{chunk: 1, min: chunkSize, max: 2*chunkSize - 1},
{chunk: 2, min: 2 * chunkSize, max: 3*chunkSize - 1},
}
for _, tc := range tests {
assert.Equal(t, tc.min, tc.chunk.Min())
assert.Equal(t, tc.max, tc.chunk.Max())
}
}
func TestChunkAt(t *testing.T) {
tests := []struct {
index uint32
chunk Chunk
}{
{index: 0, chunk: 0},
{index: chunkSize - 1, chunk: 0},
{index: chunkSize, chunk: 1},
{index: chunkSize + 1, chunk: 1},
}
for _, tc := range tests {
assert.Equal(t, tc.chunk, ChunkAt(tc.index))
}
}
func TestChunkOf(t *testing.T) {
tests := []struct {
size uint32
chunk Chunk
expect int
}{
{size: 3 * chunkSize, expect: chunkSize, chunk: 0},
{size: 3 * chunkSize, expect: chunkSize, chunk: 1},
{size: 3 * chunkSize, expect: chunkSize, chunk: 2},
{size: 3 * chunkSize, expect: 0, chunk: 3},
{size: 2*chunkSize - 70, expect: chunkSize, chunk: 0},
{size: 2*chunkSize - 70, expect: 16320, chunk: 1},
{size: 2*chunkSize - 70, expect: 0, chunk: 2},
{size: 2*chunkSize - 10, expect: chunkSize, chunk: 0},
{size: 2*chunkSize - 10, expect: chunkSize, chunk: 1},
{size: 2*chunkSize - 10, expect: 0, chunk: 2},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("%v-%v", tc.chunk, tc.size), func(t *testing.T) {
var tmp bitmap.Bitmap
tmp.Grow(tc.size - 1)
assert.Equal(t, tc.expect, len(tc.chunk.OfBitmap(tmp))*64)
})
}
}
func TestMin(t *testing.T) {
tests := []struct {
v1, v2 int32
expect int32
}{
{v1: 0, v2: 0, expect: 0},
{v1: 10, v2: 0, expect: 0},
{v1: 0, v2: 10, expect: 0},
{v1: 10, v2: 20, expect: 10},
{v1: 20, v2: 10, expect: 10},
{v1: 20, v2: 20, expect: 20},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("%v,%v", tc.v1, tc.v2), func(t *testing.T) {
assert.Equal(t, int(tc.expect), int(min(tc.v1, tc.v2)))
})
}
}
// --------------------------- Recorder ----------------------------
func TestCommitCodec(t *testing.T) {
buffer := bytes.NewBuffer(nil)
input := Commit{
ID: Next(),
Chunk: 0,
Updates: []*Buffer{
newInterleaved("a"),
newInterleaved("b"),
},
}
// Write into the buffer
n, err := input.WriteTo(buffer)
assert.Equal(t, int64(197), n)
assert.NoError(t, err)
// Read the commit back
output := Commit{}
m, err := output.ReadFrom(buffer)
assert.NoError(t, err)
assert.Equal(t, n, m)
// Make sure commit can be read back
assert.Equal(t, input.ID, output.ID)
assert.Equal(t, input.Chunk, output.Chunk)
updates := make([]int64, 0, 64)
reader := NewReader()
reader.Range(output.Updates[0], 0, func(r *Reader) {
for r.Next() {
updates = append(updates, int64(r.Offset), r.Int64())
}
})
assert.Equal(t, []int64{20, 1, 21, 2, 40, 4, 41, 5, 60, 7, 61, 8}, updates)
}
// newInterleaved creates a new interleaved buffer
func newInterleaved(columnName string) *Buffer {
buf := NewBuffer(10)
buf.Reset(columnName)
buf.PutInt64(Put, 20, 1)
buf.PutInt64(Put, 21, 2)
buf.PutInt64(Put, 20000, 3)
buf.PutInt64(Put, 40, 4)
buf.PutInt64(Put, 41, 5)
buf.PutInt64(Put, 40000, 6)
buf.PutInt64(Put, 60, 7)
buf.PutInt64(Put, 61, 8)
return buf
}
// --------------------------- Mocks ----------------------------
type limitWriter struct {
value uint32
Limit int
SeekErr error
}
func (w *limitWriter) Write(p []byte) (int, error) {
if n := atomic.AddUint32(&w.value, uint32(len(p))); int(n) > w.Limit {
return 0, io.ErrShortBuffer
}
return len(p), nil
}
func (w *limitWriter) Read(p []byte) (int, error) {
return 0, io.EOF
}
func (w *limitWriter) Seek(offset int64, whence int) (int64, error) {
return 0, w.SeekErr
}