-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
134 lines (121 loc) · 4.12 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package bitcask
import (
"bitcask/utils"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
// TestDB_NewIterator is a test function for the NewIterator method of the DB struct.
func TestDB_NewIterator(t *testing.T) {
// Create a new configuration for the DB.
cfg := DefaultConfig
// Create a temporary directory for testing.
dir, err := os.MkdirTemp("", "bitcask-test-iterator")
assert.Nil(t, err)
cfg.DirPath = dir
// Open a new DB instance with the default configuration.
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Create a new iterator with the default configuration.
iterator := db.NewIterator(DefaultIteratorConfig)
defer iterator.Close()
assert.NotNil(t, iterator)
assert.Equal(t, false, iterator.Valid())
}
// TestDB_NewIterator_One_Value is a test function for the NewIterator method of the DB struct.
// It tests the scenario where the iterator is created with a single value.
func TestDB_NewIterator_One_Value(t *testing.T) {
// Create a new configuration for the DB.
cfg := DefaultConfig
// Create a temporary directory for testing.
dir, err := os.MkdirTemp("", "bitcask-test-iterator-one-value")
assert.Nil(t, err)
cfg.DirPath = dir
// Open a new DB instance with the default configuration.
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Put a test key-value pair into the DB.
key := utils.GetTestKey(10)
value := utils.GetTestValue(10)
err = db.Put(key, value)
assert.Nil(t, err)
// Create a new iterator with the default configuration.
iterator := db.NewIterator(DefaultIteratorConfig)
defer iterator.Close()
assert.NotNil(t, iterator)
// Verify that the iterator is valid and has the correct key.
assert.Equal(t, true, iterator.Valid())
assert.Equal(t, key, iterator.Key())
// Verify that the iterator has the correct value.
val, err := iterator.Value()
assert.Nil(t, err)
assert.Equal(t, value, val)
}
// TestDB_NewIterator_Multi_Values is a test function for the NewIterator method of the DB struct.
func TestDB_NewIterator_Multi_Values(t *testing.T) {
// Create a new configuration for the DB.
cfg := DefaultConfig
// Create a temporary directory for the DB.
dir, err := os.MkdirTemp("", "bitcask-test-iterator-multi-values")
assert.Nil(t, err)
cfg.DirPath = dir
// Open a new DB instance.
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Create a key and value for testing.
key := utils.GetTestKey(10)
value := utils.GetTestValue(10)
// Put the key-value pair into the DB.
err = db.Put(key, value)
assert.Nil(t, err)
// Put multiple key-value pairs into the DB.
err = db.Put([]byte("a"), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put([]byte("b"), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put([]byte("c"), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put([]byte("d"), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put([]byte("e"), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put([]byte("ae"), utils.GetTestValue(10))
assert.Nil(t, err)
// Create a new iterator for the DB.
it1 := db.NewIterator(DefaultIteratorConfig)
defer it1.Close()
assert.NotNil(t, it1)
// Iterate over all key-value pairs in the DB.
for it1.Rewind(); it1.Valid(); it1.Next() {
assert.NotNil(t, it1.Key())
}
// Rewind the iterator to the beginning.
it1.Rewind()
// Iterate over all key-value pairs starting from "c" in the DB.
for it1.Seek([]byte("c")); it1.Valid(); it1.Next() {
assert.NotNil(t, it1.Key())
}
// Reverse iteration
it2 := db.NewIterator(IteratorConfig{Reverse: true})
defer it2.Close()
assert.NotNil(t, it2)
// Iterate over all key-value pairs in the DB in reverse order.
for it2.Rewind(); it2.Valid(); it2.Next() {
assert.NotNil(t, it2.Key())
}
// Rewind the iterator to the beginning.
it2.Rewind()
// Iterate over all key-value pairs starting from "c" in the DB in reverse order.
for it2.Seek([]byte("c")); it2.Valid(); it2.Next() {
assert.NotNil(t, it2.Key())
}
// Iterate over all key-value pairs starting from "a" in the DB.
it3 := db.NewIterator(IteratorConfig{Prefix: []byte("a")})
assert.NotNil(t, it3)
for it3.Rewind(); it3.Valid(); it3.Next() {
assert.NotNil(t, it3.Key())
}
}