forked from naqvijafar91/cuteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree_test.go
70 lines (64 loc) · 1.28 KB
/
btree_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
package cutedb
import (
"fmt"
"os"
"testing"
)
func clearDB() string {
path := "./db/test.db"
if _, err := os.Stat(path); err == nil {
// path/to/whatever exists
err := os.Remove(path)
if err != nil {
panic(err)
}
}
return path
}
func TestBtreeInsert(t *testing.T) {
tree, err := initializeBtree(clearDB())
if err != nil {
t.Error(err)
}
for i := 1; i <= 100; i++ {
key := fmt.Sprintf("key-%d", i)
value := fmt.Sprintf("value-%d", i)
if i == 230 {
println("Inserted 229 elements")
}
tree.insert(newPair(key, value))
}
tree.print()
}
func TestBtreeGet(t *testing.T) {
tree, err := initializeBtree(clearDB())
if err != nil {
t.Error(err)
}
totalElements := 500
for i := 1; i <= totalElements; i++ {
key := fmt.Sprintf("key-%d", i)
value := fmt.Sprintf("value-%d", i)
tree.insert(newPair(key, value))
}
for i := 1; i <= totalElements; i++ {
key := fmt.Sprintf("key-%d", i)
value, found, err := tree.get(key)
if err != nil {
t.Error(err)
}
if !found || value == "" {
t.Error("Value should be found ", key)
}
}
for i := totalElements + 1; i <= totalElements+1+1000; i++ {
key := fmt.Sprintf("key-%d", i)
_, found, err := tree.get(key)
if err != nil {
t.Error(err)
}
if found {
t.Error("Value should not be found")
}
}
}