Skip to content

Commit

Permalink
optimization for unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
vision9527 committed Mar 25, 2022
1 parent 8faf445 commit 055106e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
17 changes: 10 additions & 7 deletions btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ func StartNewTree(leafMaxSize, internalMaxSize int) (*BPlusTree, error) {
if leafMaxSize < 3 || internalMaxSize < 3 {
return nil, errors.New("need more than 2")
}
return &BPlusTree{
tree := &BPlusTree{
leafMaxSize: leafMaxSize,
internalMaxSize: internalMaxSize,
stat: new(stat),
}, nil
}
tree.root = tree.makeEmptyLeafNode()
return tree, nil
}

func StartDefaultNewTree() (*BPlusTree, error) {
return &BPlusTree{
tree := &BPlusTree{
leafMaxSize: defaultLeafMaxSize,
internalMaxSize: defaultInternalMaxSize,
stat: new(stat),
}, nil
}
tree.root = tree.makeEmptyLeafNode()
return tree, nil
}

// 功能接口
Expand Down Expand Up @@ -387,9 +391,6 @@ func (b *BPlusTree) insert(targetKey key, et *entry) {

func (b *BPlusTree) findFirstLeafNode() *node {
currentNode := b.root
if currentNode == nil {
panic("findFirstLeafNode should not be nil")
}
for currentNode != nil {
if currentNode.isLeaf {
break
Expand Down Expand Up @@ -560,6 +561,8 @@ func (b *BPlusTree) deleteNode(nd *node, ky key, p interface{}) {
b.root = nd.lastOrNextNode
if b.root != nil {
b.root.parent = nil
} else {
b.root = b.makeEmptyLeafNode()
}
return
}
Expand Down
50 changes: 35 additions & 15 deletions btree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ func TestInsertCaseOne(t *testing.T) {
t.Fatalf("value should be empty")
}
tree.check(false)

firstLeafNode := tree.findFirstLeafNode()
firstLeafNode.checkOrder()
}

func TestInsertCaseTwo(t *testing.T) {
Expand All @@ -322,6 +323,8 @@ func TestInsertCaseTwo(t *testing.T) {
}
}
tree.check(false)
firstLeafNode := tree.findFirstLeafNode()
firstLeafNode.checkOrder()

}

Expand Down Expand Up @@ -421,6 +424,8 @@ func TestInsertCaseShuffleTestkv3(t *testing.T) {
}
}
tree.check(false)
firstLeafNode := tree.findFirstLeafNode()
firstLeafNode.checkOrder()
}
}

Expand All @@ -444,6 +449,8 @@ func TestInsertCaseShuffleTestkv4(t *testing.T) {
}
}
tree.check(false)
firstLeafNode := tree.findFirstLeafNode()
firstLeafNode.checkOrder()
}

for n := 3; n < 100; n++ {
Expand All @@ -464,6 +471,8 @@ func TestInsertCaseShuffleTestkv4(t *testing.T) {
}
}
tree.check(false)
firstLeafNode := tree.findFirstLeafNode()
firstLeafNode.checkOrder()
}
}

Expand Down Expand Up @@ -688,6 +697,8 @@ func TestBPlusTree_DeleteOne(t *testing.T) {
}
fmt.Println(g.(string))
tree.check(false)
firstLeafNode := tree.findFirstLeafNode()
firstLeafNode.checkOrder()
}

func TestBPlusTree_DeleteTwo(t *testing.T) {
Expand Down Expand Up @@ -746,7 +757,6 @@ func TestBPlusTree_DeleteFour(t *testing.T) {
for i := 0; i < len(testkv); i++ {
ky := testkv[i]
value := testkv[i]
// tree.Print()
v, ok := tree.Delete(ky)
tree.check(false)
if !ok {
Expand All @@ -757,8 +767,7 @@ func TestBPlusTree_DeleteFour(t *testing.T) {
}
}

num := 100
for n := 4; n < num; n++ {
for n := 4; n < 100; n++ {
tree, _ := StartNewTree(4, 4)
testkv := GenTestKeyAndValue(n)
ShuffleTestkv(testkv)
Expand All @@ -779,35 +788,46 @@ func TestBPlusTree_DeleteFour(t *testing.T) {
if toString(v) != value {
t.Fatalf("value should be %s, but value:%v", ky, v)
}
firstLeafNode := tree.findFirstLeafNode()
if firstLeafNode == nil {
continue
}
firstLeafNode.checkOrder()
}
}

}

func TestBPlusTree_DeleteFive(t *testing.T) {
num := 100
for n := 4; n < num; n++ {
testkv := GenTestKeyAndValue(100)
for n := 3; n <= 10; n++ {
tree, _ := StartNewTree(n, n)
testkv := GenTestKeyAndValue(n)
ShuffleTestkv(testkv)
for i := 0; i < len(testkv); i++ {
ky := testkv[i]
value := testkv[i]
shuffleTestKv := make([]string, len(testkv))
copy(shuffleTestKv, testkv)
ShuffleTestkv(shuffleTestKv)
for i := 0; i < len(shuffleTestKv); i++ {
ky := shuffleTestKv[i]
value := shuffleTestKv[i]
tree.Insert(ky, value)
}

for i := 0; i < len(testkv); i++ {
ky := testkv[i]
value := testkv[i]
for i := 0; i < len(shuffleTestKv); i++ {
ky := shuffleTestKv[i]
value := shuffleTestKv[i]
v, ok := tree.Delete(ky)
tree.check(false)
if !ok {
tree.Print()
t.Fatalf("value should exsit")
}
if toString(v) != value {
t.Fatalf("value should be %s, but value:%v", ky, v)
}
firstLeafNode := tree.findFirstLeafNode()
if firstLeafNode == nil {
continue
}
firstLeafNode.checkOrder()
}

}
}
18 changes: 18 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,21 @@ func (n *node) isHalf() bool {
func (n *node) getHalf() int {
return n.maxSize / 2
}

// 从此节点检查key顺序
func (n *node) checkOrder() {
if !n.isLeaf {
panic("only leaf node for use")
}
currentNode := n
lastKey := key("")
for currentNode != nil {
for _, k := range currentNode.keys {
if k.compare(lastKey) < 1 {
panic("wrong key order")
}
lastKey = k
}
currentNode = currentNode.lastOrNextNode
}
}

0 comments on commit 055106e

Please sign in to comment.