-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
74 lines (72 loc) · 1.54 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
66
67
68
69
70
71
72
73
74
package cdb_test
import (
"fmt"
"github.com/orestonce/cdb"
"io"
"io/ioutil"
"strconv"
"testing"
)
func TestCdb_NewIterator(t *testing.T) {
// 顺序迭代
const maxIndex = 9
writer := cdb.NewMemoryWriter()
for idx := 0; idx < maxIndex; idx++ {
err := writer.WriteKeyValue([]byte(strconv.Itoa(idx)), []byte(strconv.Itoa(idx+1)))
if err != nil {
panic(err)
}
}
db, err := writer.Freeze()
if err != nil {
panic(err)
}
it := db.BeginIterator()
for idx := 0; idx < maxIndex; idx++ {
var key, value []byte
if idx%2 == 0 {
key, value, err = it.ReadNextKeyValue()
} else {
var vdata *io.SectionReader
key, vdata, err = it.ReadNextKey()
if err != nil {
panic(err)
}
value, err = ioutil.ReadAll(vdata)
}
if err != nil {
panic(err)
}
if string(key) != strconv.Itoa(idx) || string(value) != strconv.Itoa(idx+1) {
panic("key value not match.")
}
}
_, _, err = it.ReadNextKeyValue()
if err != cdb.ErrNoData {
panic(fmt.Sprint("unexpect error value", err))
}
if writer.GetByteNum() != 2282 {
panic("unexpected ByteNum: " + strconv.Itoa(int(writer.GetByteNum())))
}
content, err := writer.GetMemoryWriterBytes()
if err != nil {
panic(err)
}
if len(content) != 2282 {
panic("invalid content length: " + strconv.Itoa(len(content)))
}
exists, err := db.IsKeyExists([]byte("5"))
if err != nil {
panic(err)
}
if !exists {
panic("key not exists: 5")
}
exists, err = db.IsKeyExists([]byte("60"))
if err != nil {
panic(err)
}
if exists {
panic("expect key not exists, but it's exists.")
}
}