forked from rosedblabs/mini-bitcask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
84 lines (72 loc) · 1.4 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
package minidb
import (
"math/rand"
"strconv"
"testing"
"time"
)
func TestOpen(t *testing.T) {
db, err := Open("/tmp/minidb")
if err != nil {
t.Error(err)
}
t.Log(db)
}
func TestMiniDB_Put(t *testing.T) {
db, err := Open("/tmp/minidb")
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 TestMiniDB_Get(t *testing.T) {
db, err := Open("/tmp/minidb")
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"))
getVal([]byte("test_key_5"))
}
func TestMiniDB_Del(t *testing.T) {
db, err := Open("/tmp/minidb")
if err != nil {
t.Error(err)
}
key := []byte("test_key_78")
err = db.Del(key)
if err != nil {
t.Error("del err: ", err)
}
}
func TestMiniDB_Merge(t *testing.T) {
db, err := Open("/tmp/minidb")
if err != nil {
t.Error(err)
}
err = db.Merge()
if err != nil {
t.Error("merge err: ", err)
}
}