diff options
Diffstat (limited to 'quick_test.go')
-rw-r--r-- | quick_test.go | 81 |
1 files changed, 59 insertions, 22 deletions
diff --git a/quick_test.go b/quick_test.go index d85249d..e7fb22a 100644 --- a/quick_test.go +++ b/quick_test.go @@ -1,9 +1,13 @@ package bolt import ( + "bytes" "flag" + "fmt" "math/rand" + "os" "reflect" + "testing" "testing/quick" "time" ) @@ -18,45 +22,78 @@ import ( // -quick.maxvsize The maximum size of a value. // -var seed, testMaxItemCount, testMaxKeySize, testMaxValueSize int +var qseed, qmaxitems, qmaxksize, qmaxvsize int func init() { - flag.IntVar(&seed, "quick.seed", int(time.Now().UnixNano())%100000, "") - flag.IntVar(&testMaxItemCount, "quick.maxitems", 1024, "") - flag.IntVar(&testMaxKeySize, "quick.maxksize", 1024, "") - flag.IntVar(&testMaxValueSize, "quick.maxvsize", 1024, "") - warn("seed:", seed) + 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() + warn("seed:", qseed) } -// qc creates a testing/quick configuration. -func qc() *quick.Config { - return &quick.Config{Rand: rand.New(rand.NewSource(int64(seed)))} +// Ensure that a bucket can write random keys and values across multiple txns. +func TestQuickPut(t *testing.T) { + index := 0 + f := func(items testdata) bool { + withOpenDB(func(db *DB, path string) { + m := make(map[string][]byte) + + db.CreateBucket("widgets") + + for _, item := range items { + if err := db.Put("widgets", item.Key, item.Value); err != nil { + panic("put error: " + err.Error()) + } + m[string(item.Key)] = item.Value + + // Verify all key/values so far. + i := 0 + for k, v := range m { + value, err := db.Get("widgets", []byte(k)) + if err != nil { + panic("get error: " + err.Error()) + } + if !bytes.Equal(value, v) { + db.CopyFile("/tmp/bolt.random.db") + t.Fatalf("value mismatch [run %d] (%d of %d):\nkey: %x\ngot: %x\nexp: %x", index, i, len(m), []byte(k), v, value) + } + i++ + } + } + + fmt.Fprint(os.Stderr, ".") + }) + index++ + return true + } + if err := quick.Check(f, &quick.Config{Rand: rand.New(rand.NewSource(int64(qseed)))}); err != nil { + t.Error(err) + } + fmt.Fprint(os.Stderr, "\n") } -type testKeyValuePairs []testKeyValuePair +type testdata []testdataitem -func (t testKeyValuePairs) Generate(rand *rand.Rand, size int) reflect.Value { - n := rand.Intn(testMaxItemCount-1) + 1 - items := make(testKeyValuePairs, n) +func (t testdata) Generate(rand *rand.Rand, size int) reflect.Value { + n := rand.Intn(qmaxitems-1) + 1 + items := make(testdata, n) for i := 0; i < n; i++ { - items[i].Generate(rand, size) + item := &items[i] + item.Key = randByteSlice(rand, 1, qmaxksize) + item.Value = randByteSlice(rand, 0, qmaxvsize) } return reflect.ValueOf(items) } -type testKeyValuePair struct { +type testdataitem struct { Key []byte Value []byte } -func (t testKeyValuePair) Generate(rand *rand.Rand, size int) reflect.Value { - t.Key = randByteSlice(rand, 1, testMaxKeySize) - t.Value = randByteSlice(rand, 0, testMaxValueSize) - return reflect.ValueOf(t) -} - func randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte { - n := rand.Intn(maxSize - minSize) + minSize + n := rand.Intn(maxSize-minSize) + minSize b := make([]byte, n) for i := 0; i < n; i++ { b[i] = byte(rand.Intn(255)) |