-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmemtable_test.go
103 lines (96 loc) · 2.29 KB
/
memtable_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
package lsmtree
import (
"math/rand"
"testing"
"time"
)
func TestMemTable_put(t *testing.T) {
const keySize = 64
const valueSize = 1024
const length = 100
mt := newMemTable()
for i := 0; i < length; i++ {
err := mt.put(randBytes(keySize), randBytes(valueSize))
if err != nil {
t.Error(err)
}
}
if mt.data.Size() != length {
t.Errorf("length of underlying tree is not as expected, expected: %d, actual: %d", length, mt.data.Size())
}
expectedSize := (keySize + valueSize) * length
if mt.bytes() != expectedSize {
t.Errorf("size of data is not as expected, expected: %d, actual: %d", expectedSize, mt.bytes())
}
}
func TestMemTable_get(t *testing.T) {
const length = 100
mt := newMemTable()
keys := make([][]byte, 0, length)
for i := 0; i < length; i++ {
key := randBytes(64)
keys = append(keys, key)
err := mt.put(key, randBytes(1024))
if err != nil {
t.Error(err)
}
}
for _, k := range keys {
_, ok := mt.get(k)
if !ok {
t.Error("the key does not exist in memtable")
}
}
}
func TestMemTable_delete(t *testing.T) {
const keySize = 64
const length = 100
mt := newMemTable()
keys := make([][]byte, 0, length)
for i := 0; i < length; i++ {
key := randBytes(keySize)
keys = append(keys, key)
err := mt.put(key, randBytes(1024))
if err != nil {
t.Error(err)
}
}
for _, k := range keys {
err := mt.delete(k)
if err != nil {
t.Error(err)
}
}
if mt.data.Size() != length {
t.Errorf("length of underlying tree is not as expected, expected: %d, actual: %d", length, mt.data.Size())
}
expectedSize := keySize * length
if mt.bytes() != expectedSize {
t.Errorf("size of data is not as expected, expected: %d, actual: %d", expectedSize, mt.bytes())
}
}
func TestMemTable_clear(t *testing.T) {
const length = 100
mt := newMemTable()
for i := 0; i < length; i++ {
err := mt.put(randBytes(64), randBytes(1024))
if err != nil {
t.Error(err)
}
}
mt.clear()
if mt.data.Size() != 0 {
t.Errorf("length of underlying tree is not zero, actual: %d", mt.data.Size())
}
if mt.bytes() != 0 {
t.Errorf("size of data is not zero, actual: %d", mt.bytes())
}
}
var r = rand.New(rand.NewSource(time.Now().Unix()))
func randBytes(size int) []byte {
b := make([]byte, 0, size)
for i := 0; i < size; i++ {
b = append(b, byte(r.Uint32()))
}
return b
}