structure/avldup/avldup_test.go
2019-03-12 19:18:42 +08:00

317 lines
5.9 KiB
Go

package bstree
import (
"testing"
"github.com/emirpasic/gods/trees/redblacktree"
"github.com/emirpasic/gods/trees/avltree"
"github.com/davecgh/go-spew/spew"
"github.com/Pallinder/go-randomdata"
"github.com/emirpasic/gods/utils"
)
func TestRotate(t *testing.T) {
avl := New(utils.IntComparator)
content := ""
for i := 0; i < 10; i++ {
v := randomdata.Number(0, 1000)
content += spew.Sprint(v) + " "
avl.Put(v)
}
t.Error(content)
src := avl.String()
t.Error(src)
lsrc := avl.String()
t.Error(lsrc)
// rrotate(&avl.root)
rsrc := avl.String()
t.Error(rsrc)
if src == rsrc {
t.Error("src == rsrc")
}
}
func TestPutStable(t *testing.T) {
avl := New(utils.IntComparator)
for _, v := range []int{7, 14, 15, 20, 30, 21} {
t.Error(v)
avl.Put(v)
t.Error(avl.debugString())
}
// avl = New(utils.IntComparator)
// for _, v := range []int{88, 77, 80} {
// avl.Put(v)
// t.Error(avl.String())
// }
}
func TestDiffPutRandom(t *testing.T) {
avl := New(utils.IntComparator)
godsavl := avltree.NewWithIntComparator()
content := ""
m := make(map[int]int)
for i := 0; len(m) < 10; i++ {
v := randomdata.Number(0, 10000)
if _, ok := m[v]; !ok {
m[v] = v
content += spew.Sprint(v) + " "
t.Error(v)
avl.Put(v)
t.Error(avl.String())
godsavl.Put(v, v)
}
}
t.Error(godsavl.String())
}
func TestPutComparatorRandom(t *testing.T) {
for n := 0; n < 1000000; n++ {
avl := New(utils.IntComparator)
godsavl := avltree.NewWithIntComparator()
content := ""
m := make(map[int]int)
for i := 0; len(m) < 10; i++ {
v := randomdata.Number(0, 65535)
if _, ok := m[v]; !ok {
m[v] = v
content += spew.Sprint(v) + " "
// t.Error(v)
avl.Put(v)
// t.Error(avl.String())
godsavl.Put(v, v)
}
}
if avl.String() != godsavl.String() {
t.Error(content)
break
}
}
// t.Error(content)
// t.Error(avl.String())
// t.Error(godsavl.String())
// t.Error(avl.String() == godsavl.String())
}
func TestGet(t *testing.T) {
avl := New(utils.IntComparator)
for i := 0; i < 15; i++ {
avl.Put(randomdata.Number(0, 1000))
}
t.Error(avl.String())
t.Error(avl.Get(500))
}
func TestRemoveAll(t *testing.T) {
for c := 0; c < 10000; c++ {
// f, _ := os.OpenFile("./out.log", os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0666)
// log.SetOutput(f)
avl := New(utils.IntComparator)
var l []int
for i := 0; i < 100; i++ {
v := randomdata.Number(0, 100000)
l = append(l, v)
avl.Put(v)
}
// defer func() {
// if err := recover(); err != nil {
// panic(avl.String())
// }
// }()
// log.Println(avl.TraversalBreadth())
for i := 0; i < 100; i++ {
// log.Println(l[i])
// log.Println(avl.debugString())
avl.Remove(l[i])
}
}
}
func TestRemove(t *testing.T) {
// avl := New(utils.IntComparator)
// var l []int
// for _, v := range []int{86, 97, 9, 61, 37, 45, 97, 43, 95, 8} {
// l = append(l, v)
// avl.Put(v)
// }
// for i := 0; i < len(l); i++ {
// // log.Println(i)
// log.Println("begin", l[i], avl.debugString())
// avl.Remove(l[i])
// log.Println("end", l[i], avl.debugString())
// }
ALL:
for N := 0; N < 500000; N++ {
avl := New(utils.IntComparator)
gods := avltree.NewWithIntComparator()
var l []int
m := make(map[int]int)
for i := 0; len(l) < 10; i++ {
v := randomdata.Number(0, 100)
if _, ok := m[v]; !ok {
l = append(l, v)
m[v] = v
avl.Put(v)
gods.Put(v, v)
}
}
src1 := avl.String()
src2 := gods.String()
for i := 0; i < 10; i++ {
avl.Remove(l[i])
gods.Remove(l[i])
if spew.Sprint(gods.Values()) != spew.Sprint(avl.TraversalDepth(-1)) && avl.size != 0 {
// if gods.String() != avl.String() && gods.Size() != 0 && avl.size != 0 {
t.Error(src1)
t.Error(src2)
t.Error(avl.debugString())
t.Error(gods.String())
t.Error(l[i])
// t.Error(avl.TraversalDepth(-1))
// t.Error(gods.Values())
break ALL
}
}
}
}
const PutCompartorSize = 300000
const NumberMax = 60000000
func BenchmarkRemove(b *testing.B) {
avl := New(utils.IntComparator)
b.N = PutCompartorSize
var l []int
for i := 0; i < b.N; i++ {
v := randomdata.Number(0, NumberMax)
l = append(l, v)
avl.Put(v)
}
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
avl.Remove(l[i])
}
}
func BenchmarkGodsRemove(b *testing.B) {
avl := avltree.NewWithIntComparator()
b.N = PutCompartorSize
var l []int
for i := 0; i < b.N; i++ {
v := randomdata.Number(0, NumberMax)
l = append(l, v)
avl.Put(v, v)
}
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
avl.Remove(l[i])
}
}
func BenchmarkGet(b *testing.B) {
avl := New(utils.IntComparator)
b.N = PutCompartorSize
for i := 0; i < b.N/2; i++ {
avl.Put(randomdata.Number(0, NumberMax))
}
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
avl.Get(randomdata.Number(0, NumberMax))
}
}
func BenchmarkGodsRBGet(b *testing.B) {
rb := redblacktree.NewWithIntComparator()
b.N = PutCompartorSize
for i := 0; i < b.N/2; i++ {
rb.Put(randomdata.Number(0, NumberMax), i)
}
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
rb.Get(randomdata.Number(0, NumberMax))
}
}
func BenchmarkGodsAvlGet(b *testing.B) {
rb := avltree.NewWithIntComparator()
b.N = PutCompartorSize
for i := 0; i < b.N/2; i++ {
rb.Put(randomdata.Number(0, NumberMax), i)
}
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
rb.Get(randomdata.Number(0, NumberMax))
}
}
func BenchmarkPut(b *testing.B) {
avl := New(utils.IntComparator)
b.N = PutCompartorSize
for i := 0; i < b.N; i++ {
avl.Put(randomdata.Number(0, NumberMax))
}
}
func BenchmarkGodsRBPut(b *testing.B) {
rb := redblacktree.NewWithIntComparator()
b.N = PutCompartorSize
for i := 0; i < b.N; i++ {
rb.Put(randomdata.Number(0, NumberMax), i)
}
}
func BenchmarkGodsPut(b *testing.B) {
avl := avltree.NewWithIntComparator()
b.N = PutCompartorSize
for i := 0; i < b.N; i++ {
avl.Put(randomdata.Number(0, NumberMax), i)
}
}