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.
13 . "github.com/onsi/gomega"
15 "github.com/syndtr/goleveldb/leveldb/iterator"
20 func (a IterAct) String() string {
43 IterNone IterAct = iota
53 type IteratorTesting struct {
55 Iter iterator.Iterator
57 PostFn func(t *IteratorTesting)
64 func (t *IteratorTesting) init() {
71 func (t *IteratorTesting) post() {
77 func (t *IteratorTesting) setAct(act IterAct) {
78 t.LastAct, t.Act = t.Act, act
81 func (t *IteratorTesting) text() string {
82 return fmt.Sprintf("at pos %d and last action was <%v> -> <%v>", t.Pos, t.LastAct, t.Act)
85 func (t *IteratorTesting) Text() string {
86 return "IteratorTesting is " + t.text()
89 func (t *IteratorTesting) IsFirst() bool {
91 return t.Len() > 0 && t.Pos == 0
94 func (t *IteratorTesting) IsLast() bool {
96 return t.Len() > 0 && t.Pos == t.Len()-1
99 func (t *IteratorTesting) TestKV() {
101 key, value := t.Index(t.Pos)
102 Expect(t.Iter.Key()).NotTo(BeNil())
103 Expect(t.Iter.Key()).Should(Equal(key), "Key is invalid, %s", t.text())
104 Expect(t.Iter.Value()).Should(Equal(value), "Value for key %q, %s", key, t.text())
107 func (t *IteratorTesting) First() {
112 Expect(t.Iter.Error()).ShouldNot(HaveOccurred())
115 Expect(ok).Should(BeTrue(), t.Text())
119 Expect(ok).ShouldNot(BeTrue(), t.Text())
124 func (t *IteratorTesting) Last() {
129 Expect(t.Iter.Error()).ShouldNot(HaveOccurred())
132 Expect(ok).Should(BeTrue(), t.Text())
136 Expect(ok).ShouldNot(BeTrue(), t.Text())
141 func (t *IteratorTesting) Next() {
146 Expect(t.Iter.Error()).ShouldNot(HaveOccurred())
147 if t.Pos < t.Len()-1 {
149 Expect(ok).Should(BeTrue(), t.Text())
153 Expect(ok).ShouldNot(BeTrue(), t.Text())
158 func (t *IteratorTesting) Prev() {
163 Expect(t.Iter.Error()).ShouldNot(HaveOccurred())
166 Expect(ok).Should(BeTrue(), t.Text())
170 Expect(ok).ShouldNot(BeTrue(), t.Text())
175 func (t *IteratorTesting) Seek(i int) {
180 oldKey, _ := t.IndexOrNil(t.Pos)
182 ok := t.Iter.Seek(key)
183 Expect(t.Iter.Error()).ShouldNot(HaveOccurred())
184 Expect(ok).Should(BeTrue(), fmt.Sprintf("Seek from key %q to %q, to pos %d, %s", oldKey, key, i, t.text()))
191 func (t *IteratorTesting) SeekInexact(i int) {
195 key1, _ := t.Index(i)
197 key0, _ = t.Index(i - 1)
199 key := BytesSeparator(key0, key1)
200 oldKey, _ := t.IndexOrNil(t.Pos)
202 ok := t.Iter.Seek(key)
203 Expect(t.Iter.Error()).ShouldNot(HaveOccurred())
204 Expect(ok).Should(BeTrue(), fmt.Sprintf("Seek from key %q to %q (%q), to pos %d, %s", oldKey, key, key1, i, t.text()))
211 func (t *IteratorTesting) SeekKey(key []byte) {
214 oldKey, _ := t.IndexOrNil(t.Pos)
217 ok := t.Iter.Seek(key)
218 Expect(t.Iter.Error()).ShouldNot(HaveOccurred())
220 key_, _ := t.Index(i)
221 Expect(ok).Should(BeTrue(), fmt.Sprintf("Seek from key %q to %q (%q), to pos %d, %s", oldKey, key, key_, i, t.text()))
225 Expect(ok).ShouldNot(BeTrue(), fmt.Sprintf("Seek from key %q to %q, %s", oldKey, key, t.text()))
232 func (t *IteratorTesting) SOI() {
235 Expect(t.Pos).Should(BeNumerically("<=", 0), t.Text())
236 for i := 0; i < 3; i++ {
242 func (t *IteratorTesting) EOI() {
245 Expect(t.Pos).Should(BeNumerically(">=", t.Len()-1), t.Text())
246 for i := 0; i < 3; i++ {
252 func (t *IteratorTesting) WalkPrev(fn func(t *IteratorTesting)) {
254 for old := t.Pos; t.Pos > 0; old = t.Pos {
256 Expect(t.Pos).Should(BeNumerically("<", old), t.Text())
260 func (t *IteratorTesting) WalkNext(fn func(t *IteratorTesting)) {
262 for old := t.Pos; t.Pos < t.Len()-1; old = t.Pos {
264 Expect(t.Pos).Should(BeNumerically(">", old), t.Text())
268 func (t *IteratorTesting) PrevAll() {
269 t.WalkPrev(func(t *IteratorTesting) {
274 func (t *IteratorTesting) NextAll() {
275 t.WalkNext(func(t *IteratorTesting) {
280 func DoIteratorTesting(t *IteratorTesting) {
305 ShuffledIndex(t.Rand, t.Len(), 1, func(i int) {
309 ShuffledIndex(t.Rand, t.Len(), 1, func(i int) {
313 ShuffledIndex(t.Rand, t.Len(), 1, func(i int) {
324 for _, key := range []string{"", "foo", "bar", "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} {
325 t.SeekKey([]byte(key))