-
Notifications
You must be signed in to change notification settings - Fork 88
/
db_test.go
88 lines (75 loc) · 1.52 KB
/
db_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
package minibitcask
import (
"math/rand"
"strconv"
"testing"
"time"
)
func TestOpen(t *testing.T) {
db, err := Open("/tmp/minibitcask")
if err != nil {
t.Error(err)
}
t.Log(db)
}
func TestMiniBitcask_Put(t *testing.T) {
db, err := Open("/tmp/minibitcask")
if err != nil {
t.Error(err)
}
rand.Seed(time.Now().UnixNano())
keyPrefix := "test_key_"
valPrefix := "test_val_"
for i := 0; i < 10000; i++ {
key := []byte(keyPrefix + strconv.Itoa(i%5))
val := []byte(valPrefix + strconv.FormatInt(rand.Int63(), 10))
err = db.Put(key, val)
}
if err != nil {
t.Log(err)
}
}
func TestMiniBitcask_Get(t *testing.T) {
db, err := Open("/tmp/minibitcask")
if err != nil {
t.Error(err)
}
getVal := func(key []byte) {
val, err := db.Get(key)
if err != nil {
t.Error("read val err: ", err)
} else {
t.Logf("key = %s, val = %s\n", string(key), string(val))
}
}
getVal([]byte("test_key_0"))
getVal([]byte("test_key_1"))
getVal([]byte("test_key_2"))
getVal([]byte("test_key_3"))
getVal([]byte("test_key_4"))
_, err = db.Get([]byte("test_key_5"))
if err == nil {
t.Error("expected test_Key_5 does not exist")
}
}
func TestMiniBitcask_Del(t *testing.T) {
db, err := Open("/tmp/minibitcask")
if err != nil {
t.Error(err)
}
key := []byte("test_key_78")
err = db.Del(key)
if err != nil {
t.Error("del err: ", err)
}
}
func TestMiniBitcask_Merge(t *testing.T) {
db, err := Open("/tmp/minibitcask")
if err != nil {
t.Error(err)
}
err = db.Merge()
if err != nil {
t.Error("merge err: ", err)
}
}