bolt/quick_test.go

80 lines
2.2 KiB
Go
Raw Normal View History

2014-07-26 21:11:47 +00:00
package bolt_test
2014-01-31 18:18:51 +00:00
import (
2014-02-11 15:41:22 +00:00
"bytes"
2014-01-31 18:18:51 +00:00
"flag"
2014-07-26 21:11:47 +00:00
"fmt"
2014-01-31 18:18:51 +00:00
"math/rand"
2014-07-26 21:11:47 +00:00
"os"
2014-01-31 18:18:51 +00:00
"reflect"
"testing/quick"
"time"
)
2014-02-06 23:06:13 +00:00
// testing/quick defaults to 5 iterations and a random seed.
2014-01-31 18:18:51 +00:00
// You can override these settings from the command line:
//
2014-02-06 23:06:13 +00:00
// -quick.count The number of iterations to perform.
2014-01-31 18:18:51 +00:00
// -quick.seed The seed to use for randomizing.
// -quick.maxitems The maximum number of items to insert into a DB.
// -quick.maxksize The maximum size of a key.
// -quick.maxvsize The maximum size of a value.
//
2014-02-06 23:06:13 +00:00
var qcount, qseed, qmaxitems, qmaxksize, qmaxvsize int
2014-01-31 18:18:51 +00:00
func init() {
2014-02-06 23:06:13 +00:00
flag.IntVar(&qcount, "quick.count", 5, "")
2014-02-04 22:30:05 +00:00
flag.IntVar(&qseed, "quick.seed", int(time.Now().UnixNano())%100000, "")
flag.IntVar(&qmaxitems, "quick.maxitems", 1000, "")
flag.IntVar(&qmaxksize, "quick.maxksize", 1024, "")
flag.IntVar(&qmaxvsize, "quick.maxvsize", 1024, "")
flag.Parse()
2014-07-26 21:11:47 +00:00
fmt.Fprintln(os.Stderr, "seed:", qseed)
fmt.Fprintf(os.Stderr, "quick settings: count=%v, items=%v, ksize=%v, vsize=%v\n", qcount, qmaxitems, qmaxksize, qmaxvsize)
2014-01-31 18:18:51 +00:00
}
2014-02-06 23:06:13 +00:00
func qconfig() *quick.Config {
return &quick.Config{
MaxCount: qcount,
Rand: rand.New(rand.NewSource(int64(qseed))),
2014-02-04 22:30:05 +00:00
}
2014-01-31 18:18:51 +00:00
}
2014-02-04 22:30:05 +00:00
type testdata []testdataitem
2014-01-31 18:18:51 +00:00
2014-02-11 15:41:22 +00:00
func (t testdata) Len() int { return len(t) }
func (t testdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t testdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == -1 }
2014-02-04 22:30:05 +00:00
func (t testdata) Generate(rand *rand.Rand, size int) reflect.Value {
n := rand.Intn(qmaxitems-1) + 1
items := make(testdata, n)
2014-01-31 18:18:51 +00:00
for i := 0; i < n; i++ {
2014-02-04 22:30:05 +00:00
item := &items[i]
item.Key = randByteSlice(rand, 1, qmaxksize)
item.Value = randByteSlice(rand, 0, qmaxvsize)
2014-01-31 18:18:51 +00:00
}
return reflect.ValueOf(items)
}
2014-02-20 20:53:40 +00:00
type revtestdata []testdataitem
func (t revtestdata) Len() int { return len(t) }
func (t revtestdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t revtestdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == 1 }
2014-02-04 22:30:05 +00:00
type testdataitem struct {
2014-01-31 18:18:51 +00:00
Key []byte
Value []byte
}
func randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte {
2014-02-04 22:30:05 +00:00
n := rand.Intn(maxSize-minSize) + minSize
2014-01-31 18:18:51 +00:00
b := make([]byte, n)
for i := 0; i < n; i++ {
b[i] = byte(rand.Intn(255))
}
return b
}