tpop/datastructures_test.go

73 lines
1.2 KiB
Go

package main
import (
"os"
"testing"
)
func TestBuildingTree(t *testing.T) {
tree := NewStringyBTree("foo", 1).Insert("bar", 2).Insert("baz", 3).Insert("quuz", 4)
if tree.Get("foo") != 1 {
t.Fail()
}
if tree.Get("bar") != 2 {
t.Fail()
}
if tree.Get("baz") != 3 {
t.Fail()
}
if tree.Get("quuz") != 4 {
t.Fail()
}
tree.Insert("bar", 5)
if tree.Get("bar") != 5 {
t.Fail()
}
if tree.Get("zot") != nil {
t.Fail()
}
}
func TestDeletingFromTree(t *testing.T) {
tree := NewStringyBTree("foo", 1).Insert("bar", 2).Insert("baz", 3).Insert("quuz", 4)
tree.Delete("bar")
if tree.Get("foo") != 1 {
t.Fail()
}
if tree.Get("bar") != nil {
t.Fail()
}
if tree.Get("baz") != 3 {
t.Fail()
}
if tree.Get("quuz") != 4 {
t.Fail()
}
tree.Print(os.Stdout)
tree = NewStringyBTree("foo", 1).Insert("bar", 2).Insert("baz", 3).Insert("quuz", 4)
tree.Delete("foo")
if tree.Get("foo") != nil {
t.Fail()
}
if tree.Get("bar") != 2 {
t.Fail()
}
if tree.Get("baz") != 3 {
t.Fail()
}
if tree.Get("quuz") != 4 {
t.Fail()
}
}
var r TreeNode
func BenchmarkInserts(b *testing.B) {
var result TreeNode
for n := 0; n < b.N; n++ {
NewStringyBTree("foo", 1).Insert("bar", 2).Insert("baz", 3).Insert("quuz", 4)
}
r = result
}