forked from naqvijafar91/cuteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_service_test.go
138 lines (116 loc) · 3.3 KB
/
block_service_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
135
136
137
138
package cutedb
import (
"testing"
)
func TestShouldGetNegativeIfBlockNotPresent(t *testing.T) {
blockService := initBlockService()
latestBlockID, _ := blockService.getLatestBlockID()
if latestBlockID != -1 {
t.Error("Should get negative block id")
}
}
func TestShouldSuccessfullyInitializeNewBlock(t *testing.T) {
blockService := initBlockService()
block, err := blockService.getRootBlock()
if err != nil {
t.Error(err)
}
if block.id != 0 {
t.Error("Root Block id should be zero")
}
if block.currentLeafSize != 0 {
t.Error("Block leaf size should be zero")
}
}
func TestShouldSaveNewBlockOnDisk(t *testing.T) {
blockService := initBlockService()
block, err := blockService.getRootBlock()
if err != nil {
t.Error(err)
}
if block.id != 0 {
t.Error("Root Block id should be zero")
}
if block.currentLeafSize != 0 {
t.Error("Block leaf size should be zero")
}
elements := make([]*pairs, 3)
elements[0] = newPair("hola", "amigos")
elements[1] = newPair("foo", "bar")
elements[2] = newPair("gooz", "bumps")
block.setData(elements)
err = blockService.writeBlockToDisk(block)
if err != nil {
t.Error(err)
}
block, err = blockService.getRootBlock()
if err != nil {
t.Error(err)
}
if len(block.dataSet) == 0 {
t.Error("Length of data field should not be zero")
}
}
func TestShouldConvertPairToAndFromBytes(t *testing.T) {
pair := &pairs{}
pair.setKey("Hola ")
pair.setValue("Amigos")
pairBytes := convertPairsToBytes(pair)
convertedPair := convertBytesToPair(pairBytes)
if pair.keyLen != convertedPair.keyLen || pair.valueLen != convertedPair.valueLen {
t.Error("Lengths do not match")
}
if pair.key != convertedPair.key || pair.value != convertedPair.value {
t.Error("Values do not match")
}
}
func TestShouldConvertBlockToAndFromBytes(t *testing.T) {
blockService := initBlockService()
block := &diskBlock{}
block.setChildren([]uint64{2, 3, 4, 6})
elements := make([]*pairs, 3)
elements[0] = newPair("hola", "amigos")
elements[1] = newPair("foo", "bar")
elements[2] = newPair("gooz", "bumps")
block.setData(elements)
blockBuffer := block.getBufferFromBlock()
convertedBlock := blockService.getBlockFromBuffer(blockBuffer)
if convertedBlock.childrenBlockIds[2] != 4 {
t.Error("Should contain 4 at 2nd index")
}
if len(convertedBlock.dataSet) != len(block.dataSet) {
t.Error("Length of blocks should be same")
}
if convertedBlock.dataSet[1].key != block.dataSet[1].key {
t.Error("Keys dont match")
}
if convertedBlock.dataSet[2].value != block.dataSet[2].value {
t.Error("Values dont match")
}
}
func TestShouldConvertToAndFromDiskNode(t *testing.T) {
bs := initBlockService()
node := &DiskNode{}
node.blockID = 55
elements := make([]*pairs, 3)
elements[0] = newPair("hola", "amigos")
node.keys = elements
node.childrenBlockIDs = []uint64{1000, 10001}
block := node.convertDiskNodeToBlock()
if block.id != 55 {
t.Error("Should have same block id as node block id")
}
if block.childrenBlockIds[1] != 10001 {
t.Error("Block ids should match")
}
nodeFromBlock := bs.convertBlockToDiskNode(block)
if nodeFromBlock.blockID != node.blockID {
t.Error("Block ids should match")
}
if nodeFromBlock.childrenBlockIDs[0] != 1000 {
t.Error("Child Block ids should match")
}
if nodeFromBlock.keys[0].key != "hola" {
t.Error("Data elements should match")
}
}