Add seek forward test.

master
Ben Johnson 2014-06-09 12:31:52 -06:00
parent 6ddb056698
commit 63a9afd028
1 changed files with 52 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package bolt package bolt
import ( import (
"encoding/binary"
"sort" "sort"
"testing" "testing"
"testing/quick" "testing/quick"
@ -66,6 +67,57 @@ func TestCursor_Seek(t *testing.T) {
}) })
} }
// Ensure that a Tx cursor can seek to the appropriate keys when there are a
// large number of keys. This test also checks that seek will always move
// forward to the next key.
//
// Related: https://github.com/boltdb/bolt/pull/187
func TestCursor_Seek_Large(t *testing.T) {
withOpenDB(func(db *DB, path string) {
var count = 10000
// Insert every other key between 0 and $count.
db.Update(func(tx *Tx) error {
b, _ := tx.CreateBucket([]byte("widgets"))
for i := 0; i < count; i += 100 {
for j := i; j < i+100; j += 2 {
k := make([]byte, 8)
binary.BigEndian.PutUint64(k, uint64(j))
b.Put(k, make([]byte, 100))
}
}
return nil
})
db.View(func(tx *Tx) error {
c := tx.Bucket([]byte("widgets")).Cursor()
for i := 0; i < count; i++ {
seek := make([]byte, 8)
binary.BigEndian.PutUint64(seek, uint64(i))
k, _ := c.Seek(seek)
// The last seek is beyond the end of the the range so
// it should return nil.
if i == count-1 {
assert.Nil(t, k)
continue
}
// Otherwise we should seek to the exact key or the next key.
num := binary.BigEndian.Uint64(k)
if i%2 == 0 {
assert.Equal(t, uint64(i), num)
} else {
assert.Equal(t, uint64(i+1), num)
}
}
return nil
})
})
}
// Ensure that a cursor can iterate over an empty bucket without error. // Ensure that a cursor can iterate over an empty bucket without error.
func TestCursor_EmptyBucket(t *testing.T) { func TestCursor_EmptyBucket(t *testing.T) {
withOpenDB(func(db *DB, path string) { withOpenDB(func(db *DB, path string) {