forked from akrylysov/pogreb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator_test.go
65 lines (56 loc) · 1.16 KB
/
iterator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pogreb
import (
"bytes"
"testing"
"github.com/akrylysov/pogreb/internal/assert"
)
func TestIteratorEmpty(t *testing.T) {
db, err := createTestDB(nil)
assert.Nil(t, err)
it := db.Items()
for i := 0; i < 8; i++ {
_, _, err := it.Next()
if err != ErrIterationDone {
t.Fatalf("expected %v; got %v", ErrIterationDone, err)
}
}
assert.Nil(t, db.Close())
}
func TestIterator(t *testing.T) {
db, err := createTestDB(nil)
assert.Nil(t, err)
items := map[byte]bool{}
var i byte
for i = 0; i < 255; i++ {
items[i] = false
err := db.Put([]byte{i}, []byte{i})
assert.Nil(t, err)
}
it := db.Items()
for {
key, value, err := it.Next()
if err == ErrIterationDone {
break
}
assert.Nil(t, err)
if k, ok := items[key[0]]; !ok {
t.Fatalf("unknown key %v", k)
}
if !bytes.Equal(key, value) {
t.Fatalf("expected %v; got %v", key, value)
}
items[key[0]] = true
}
for k, v := range items {
if !v {
t.Fatalf("expected to iterate over key %v", k)
}
}
for i := 0; i < 8; i++ {
_, _, err := it.Next()
if err != ErrIterationDone {
t.Fatalf("expected %v; got %v", ErrIterationDone, err)
}
}
assert.Nil(t, db.Close())
}