1 // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
12 . "github.com/onsi/ginkgo"
14 "github.com/syndtr/goleveldb/leveldb/comparer"
15 . "github.com/syndtr/goleveldb/leveldb/iterator"
16 "github.com/syndtr/goleveldb/leveldb/testutil"
19 type keyValue struct {
24 type keyValueIndex []keyValue
26 func (x keyValueIndex) Search(key []byte) int {
27 return sort.Search(x.Len(), func(i int) bool {
28 return comparer.DefaultComparer.Compare(x[i].key, key) >= 0
32 func (x keyValueIndex) Len() int { return len(x) }
33 func (x keyValueIndex) Index(i int) (key, value []byte) { return x[i].key, nil }
34 func (x keyValueIndex) Get(i int) Iterator { return NewArrayIterator(x[i]) }
36 var _ = testutil.Defer(func() {
37 Describe("Indexed iterator", func() {
38 Test := func(n ...int) func() {
40 rnd := testutil.NewRand()
41 n = make([]int, rnd.Intn(17)+3)
43 n[i] = rnd.Intn(19) + 1
48 It("Should iterates and seeks correctly", func(done Done) {
50 index := make(keyValueIndex, len(n))
55 kv := testutil.KeyValue_Generate(nil, sum, 1, 1, 10, 4, 4)
56 for i, j := 0, 0; i < len(n); i++ {
57 for x := n[i]; x > 0; x-- {
58 key, value := kv.Index(j)
60 index[i].Put(key, value)
66 t := testutil.IteratorTesting{
68 Iter: NewIndexedIterator(NewArrayIndexer(index), true),
70 testutil.DoIteratorTesting(&t)
76 Describe("with 100 keys", Test(100))
77 Describe("with 50-50 keys", Test(50, 50))
78 Describe("with 50-1 keys", Test(50, 1))
79 Describe("with 50-1-50 keys", Test(50, 1, 50))
80 Describe("with 1-50 keys", Test(1, 50))
81 Describe("with random N-keys", Test())