github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/syndtr/goleveldb/leveldb/iterator/indexed_iter_test.go (about) 1 // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com> 2 // All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 package iterator_test 8 9 import ( 10 "sort" 11 12 . "github.com/insionng/yougam/libraries/onsi/ginkgo" 13 14 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/comparer" 15 . "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/iterator" 16 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/testutil" 17 ) 18 19 type keyValue struct { 20 key []byte 21 testutil.KeyValue 22 } 23 24 type keyValueIndex []keyValue 25 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 29 }) 30 } 31 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]) } 35 36 var _ = testutil.Defer(func() { 37 Describe("Indexed iterator", func() { 38 Test := func(n ...int) func() { 39 if len(n) == 0 { 40 rnd := testutil.NewRand() 41 n = make([]int, rnd.Intn(17)+3) 42 for i := range n { 43 n[i] = rnd.Intn(19) + 1 44 } 45 } 46 47 return func() { 48 It("Should iterates and seeks correctly", func(done Done) { 49 // Build key/value. 50 index := make(keyValueIndex, len(n)) 51 sum := 0 52 for _, x := range n { 53 sum += x 54 } 55 kv := testutil.KeyValue_Generate(nil, sum, 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) 59 index[i].key = key 60 index[i].Put(key, value) 61 j++ 62 } 63 } 64 65 // Test the iterator. 66 t := testutil.IteratorTesting{ 67 KeyValue: kv.Clone(), 68 Iter: NewIndexedIterator(NewArrayIndexer(index), true), 69 } 70 testutil.DoIteratorTesting(&t) 71 done <- true 72 }, 1.5) 73 } 74 } 75 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()) 82 }) 83 })