chunks: add randomized test

This commit is contained in:
Fabian Reinartz 2016-11-29 22:43:24 +01:00
parent fa181a34c1
commit f392c01c12
1 changed files with 53 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"io"
"math/rand"
"reflect"
"testing"
"github.com/stretchr/testify/require"
@ -14,6 +15,58 @@ type pair struct {
v float64
}
func TestChunk(t *testing.T) {
for enc, nc := range map[Encoding]func(sz int) Chunk{
EncXOR: func(sz int) Chunk { return NewXORChunk(sz) },
} {
t.Run(fmt.Sprintf("%s", enc), func(t *testing.T) {
for range make([]struct{}, 10000) {
c := nc(rand.Intn(512))
testChunk(t, c)
}
})
}
}
func testChunk(t *testing.T, c Chunk) {
app, err := c.Appender()
if err != nil {
t.Fatal(err)
}
var exp []pair
var (
ts = int64(1234123324)
v = 1243535.123
)
for {
ts += int64(rand.Intn(10000) + 1)
v = rand.Float64()
err := app.Append(ts, v)
if err != nil {
if err == ErrChunkFull {
break
}
t.Fatal(err)
}
exp = append(exp, pair{t: ts, v: v})
}
it := c.Iterator()
var res []pair
for it.Next() {
ts, v := it.Values()
res = append(res, pair{t: ts, v: v})
}
if it.Err() != nil {
t.Fatal(it.Err())
}
if !reflect.DeepEqual(exp, res) {
t.Fatalf("unexpected result\n\ngot: %v\n\nexp: %v", res, exp)
}
}
func benchmarkIterator(b *testing.B, newChunk func(int) Chunk) {
var (
t = int64(1234123324)