gods/trees/redblacktree/redblacktree_test.go

71 lines
1.2 KiB
Go
Raw Normal View History

2015-03-04 18:38:46 +00:00
package redblacktree
import (
"testing"
"log"
2015-03-04 18:38:46 +00:00
)
func TestRedBlackTree(t *testing.T) {
2015-03-04 18:38:46 +00:00
tree := NewWithIntComparator()
// insertions
tree.Put(5, "e")
tree.Put(6, "f")
tree.Put(7, "g")
2015-03-04 18:38:46 +00:00
tree.Put(3, "c")
tree.Put(4, "d")
tree.Put(1, "x")
tree.Put(2, "b")
tree.Put(1, "a") //overwrite
2015-03-04 18:38:46 +00:00
// key,expectedValue,expectedFound
tests1 := [][]interface{}{
{1, "a", true},
{2, "b", true},
{3, "c", true},
{4, "d", true},
{5, "e", true},
{6, "f", true},
{7, "g", true},
{8, nil, false},
}
for _, test := range tests1 {
// retrievals
actualValue, actualFound := tree.Get(test[0])
if actualValue != test[1] || actualFound != test[2] {
t.Errorf("Got %v expected %v", actualValue, test[1])
}
}
// removals
log.Println(tree)
tree.Remove(5)
log.Println(tree)
tree.Remove(6)
tree.Remove(7)
tree.Remove(8)
tests2 := [][]interface{}{
2015-03-04 18:38:46 +00:00
{1, "a", true},
{2, "b", true},
{3, "c", true},
{4, "d", true},
{5, nil, false},
{6, nil, false},
{7, nil, false},
{8, nil, false},
2015-03-04 18:38:46 +00:00
}
for _, test := range tests2 {
// retrievals
2015-03-04 18:38:46 +00:00
actualValue, actualFound := tree.Get(test[0])
if actualValue != test[1] || actualFound != test[2] {
t.Errorf("Got %v expected %v", actualValue, test[1])
}
}
2015-03-04 18:38:46 +00:00
}