-
Notifications
You must be signed in to change notification settings - Fork 13
/
iterator_test.go
60 lines (49 loc) · 1.08 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
package cdb_test
import (
"log"
"math/rand"
"testing"
"time"
"github.com/colinmarc/cdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIterator(t *testing.T) {
db, err := cdb.Open("./test/test.cdb")
require.NoError(t, err)
require.NotNil(t, db)
n := 0
iter := db.Iter()
for iter.Next() {
assert.Equal(t, string(expectedRecords[n][0]), string(iter.Key()))
assert.Equal(t, string(expectedRecords[n][1]), string(iter.Value()))
require.NoError(t, iter.Err())
n++
}
assert.Equal(t, len(expectedRecords)-1, n)
require.NoError(t, iter.Err())
}
func BenchmarkIterator(b *testing.B) {
db, _ := cdb.Open("./test/test.cdb")
iter := db.Iter()
b.ResetTimer()
rand.Seed(time.Now().UnixNano())
for i := 0; i < b.N; i++ {
for iter.Next() {
}
}
}
func ExampleIterator() {
db, err := cdb.Open("./test/test.cdb")
if err != nil {
log.Fatal(err)
}
// Create an iterator for the database.
iter := db.Iter()
for iter.Next() {
// Do something with iter.Key()/iter.Value()
}
if err := iter.Err(); err != nil {
log.Fatal(err)
}
}