- btree get and put tests

pull/26/head
Emir Pasic 9 years ago
parent 9663093961
commit 4e3ff20469

@ -195,6 +195,7 @@ func (tree *Tree) middle() int {
return (tree.m - 1) / 2 // "-1" to favor right nodes to have more keys when splitting return (tree.m - 1) / 2 // "-1" to favor right nodes to have more keys when splitting
} }
// search searches only within the single node among its entries
func (tree *Tree) search(node *Node, key interface{}) (index int, found bool) { func (tree *Tree) search(node *Node, key interface{}) (index int, found bool) {
low, high := 0, len(node.Entries)-1 low, high := 0, len(node.Entries)-1
var mid int var mid int
@ -265,8 +266,8 @@ func (tree *Tree) splitNonRoot(node *Node) {
// Move children from the node to be split into left and right nodes // Move children from the node to be split into left and right nodes
if !tree.isLeaf(node) { if !tree.isLeaf(node) {
left.Children = node.Children[:middle+1] left.Children = append([]*Node(nil), node.Children[:middle+1]...)
right.Children = node.Children[middle+1:] right.Children = append([]*Node(nil), node.Children[middle+1:]...)
setParent(left.Children, left) setParent(left.Children, left)
setParent(right.Children, right) setParent(right.Children, right)
} }
@ -297,8 +298,8 @@ func (tree *Tree) splitRoot() {
// Move children from the node to be split into left and right nodes // Move children from the node to be split into left and right nodes
if !tree.isLeaf(tree.Root) { if !tree.isLeaf(tree.Root) {
left.Children = tree.Root.Children[:middle+1] left.Children = append([]*Node(nil), tree.Root.Children[:middle+1]...)
right.Children = tree.Root.Children[middle+1:] right.Children = append([]*Node(nil), tree.Root.Children[middle+1:]...)
setParent(left.Children, left) setParent(left.Children, left)
setParent(right.Children, right) setParent(right.Children, right)
} }

@ -5,7 +5,7 @@
package btree package btree
import ( import (
"fmt" _ "fmt"
"testing" "testing"
) )
@ -39,39 +39,38 @@ func TestBTreeGet1(t *testing.T) {
} }
func TestBTreeGet2(t *testing.T) { func TestBTreeGet2(t *testing.T) {
//tree := NewWithIntComparator(3) tree := NewWithIntComparator(3)
//tree.Put(7, "g") tree.Put(7, "g")
//tree.Put(9, "i") tree.Put(9, "i")
//tree.Put(10, "j") tree.Put(10, "j")
//tree.Put(6, "f") tree.Put(6, "f")
//tree.Put(3, "c") tree.Put(3, "c")
//tree.Put(4, "d") tree.Put(4, "d")
//tree.Put(5, "e") tree.Put(5, "e")
//tree.Put(8, "h") tree.Put(8, "h")
//tree.Put(2, "b") tree.Put(2, "b")
////tree.Put(1, "a") tree.Put(1, "a")
//fmt.Println(tree)
// tests := [][]interface{}{
//tests := [][]interface{}{ {0, nil, false},
// {0, nil, false}, {1, "a", true},
// {1, "a", true}, {2, "b", true},
// {2, "b", true}, {3, "c", true},
// {3, "c", true}, {4, "d", true},
// {4, "d", true}, {5, "e", true},
// {5, "e", true}, {6, "f", true},
// {6, "f", true}, {7, "g", true},
// {7, "g", true}, {8, "h", true},
// {8, "h", true}, {9, "i", true},
// {9, "i", true}, {10, "j", true},
// {10, "j", true}, {11, nil, false},
// {11, nil, false}, }
//}
// for _, test := range tests {
//for _, test := range tests { if value, found := tree.Get(test[0]); value != test[1] || found != test[2] {
// if value, found := tree.Get(test[0]); value != test[1] || found != test[2] { t.Errorf("Got %v,%v expected %v,%v", value, found, test[1], test[2])
// t.Errorf("Got %v,%v expected %v,%v", value, found, test[1], test[2]) }
// } }
//}
} }
func TestBTreePut1(t *testing.T) { func TestBTreePut1(t *testing.T) {
@ -234,23 +233,27 @@ func TestBTreePut4(t *testing.T) {
assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{6}, true) assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{6}, true)
tree.Put(3, nil) tree.Put(3, nil)
assertValidTree(t, tree, 4)
assertValidTreeNode(t, tree.Root, 1, 2, []int{5}, false) assertValidTreeNode(t, tree.Root, 1, 2, []int{5}, false)
assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{3, 4}, true) assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{3, 4}, true)
assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{6}, true) assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{6}, true)
tree.Put(2, nil) tree.Put(2, nil)
assertValidTree(t, tree, 5)
assertValidTreeNode(t, tree.Root, 2, 3, []int{3, 5}, false) assertValidTreeNode(t, tree.Root, 2, 3, []int{3, 5}, false)
assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{2}, true) assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{2}, true)
assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true) assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true)
assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{6}, true) assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{6}, true)
tree.Put(1, nil) tree.Put(1, nil)
assertValidTree(t, tree, 6)
assertValidTreeNode(t, tree.Root, 2, 3, []int{3, 5}, false) assertValidTreeNode(t, tree.Root, 2, 3, []int{3, 5}, false)
assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{1, 2}, true) assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{1, 2}, true)
assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true) assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true)
assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{6}, true) assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{6}, true)
tree.Put(0, nil) tree.Put(0, nil)
assertValidTree(t, tree, 7)
assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false) assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{1}, true) assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{1}, true)
assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true) assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
@ -260,6 +263,7 @@ func TestBTreePut4(t *testing.T) {
assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true) assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
tree.Put(-1, nil) tree.Put(-1, nil)
assertValidTree(t, tree, 8)
assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false) assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{1}, true) assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{1}, true)
assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true) assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
@ -268,13 +272,40 @@ func TestBTreePut4(t *testing.T) {
assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true) assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true) assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
fmt.Println(tree) tree.Put(-2, nil)
//tree.Put(-2, nil) assertValidTree(t, tree, 9)
//tree.Put(-3, nil) assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
//tree.Put(-4, nil) assertValidTreeNode(t, tree.Root.Children[0], 2, 3, []int{-1, 1}, true)
//tree.Put(-5, nil) assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
//tree.Put(-6, nil) assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{-2}, true)
//fmt.Println(tree) assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{0}, true)
assertValidTreeNode(t, tree.Root.Children[0].Children[2], 1, 0, []int{2}, true)
assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
tree.Put(-3, nil)
assertValidTree(t, tree, 10)
assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
assertValidTreeNode(t, tree.Root.Children[0], 2, 3, []int{-1, 1}, true)
assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
assertValidTreeNode(t, tree.Root.Children[0].Children[0], 2, 0, []int{-3, -2}, true)
assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{0}, true)
assertValidTreeNode(t, tree.Root.Children[0].Children[2], 1, 0, []int{2}, true)
assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
tree.Put(-4, nil)
assertValidTree(t, tree, 11)
assertValidTreeNode(t, tree.Root, 2, 3, []int{-1, 3}, false)
assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{-3}, true)
assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{1}, true)
assertValidTreeNode(t, tree.Root.Children[2], 1, 2, []int{5}, true)
assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{-4}, true)
assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{-2}, true)
assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{0}, true)
assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{2}, true)
assertValidTreeNode(t, tree.Root.Children[2].Children[0], 1, 0, []int{4}, true)
assertValidTreeNode(t, tree.Root.Children[2].Children[1], 1, 0, []int{6}, true)
} }
func TestBTreeHeight(t *testing.T) { func TestBTreeHeight(t *testing.T) {

Loading…
Cancel
Save