完成大部分的copy
This commit is contained in:
@@ -13,8 +13,6 @@ import (
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/emirpasic/gods/trees/avltree"
|
||||
"github.com/emirpasic/gods/trees/redblacktree"
|
||||
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
const CompartorSize = 100
|
||||
@@ -59,10 +57,10 @@ func loadTestData() []int {
|
||||
}
|
||||
|
||||
func TestIterator(t *testing.T) {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} {
|
||||
// t.Error(v)
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
|
||||
}
|
||||
// ` AVLTree
|
||||
@@ -80,7 +78,7 @@ func TestIterator(t *testing.T) {
|
||||
// └── 2
|
||||
// └── 1`
|
||||
|
||||
iter := avl.Iterator() // root start point
|
||||
iter := tree.Iterator() // root start point
|
||||
l := []int{14, 15, 20, 21, 30}
|
||||
|
||||
for i := 0; iter.Next(); i++ {
|
||||
@@ -245,30 +243,29 @@ func TestGetAround(t *testing.T) {
|
||||
|
||||
// for test error case
|
||||
func TestPutStable(t *testing.T) {
|
||||
f, _ := os.OpenFile("./test.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
|
||||
log.SetOutput(f)
|
||||
// f, _ := os.OpenFile("./test.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
|
||||
// log.SetOutput(f)
|
||||
// 0-1 3 | 2-3 7-8 | 4-7 12-16 | 8-15 20-32 | 16-31 33-58 l := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 30, 41, 41, 41}
|
||||
|
||||
for i := 0; i < 100000; i++ {
|
||||
var l []int
|
||||
for len(l) < 1000 {
|
||||
l = append(l, randomdata.Number(0, 100))
|
||||
}
|
||||
// for i := 0; i < 100000; i++ {
|
||||
// var l []int
|
||||
// for len(l) < 1000 {
|
||||
// l = append(l, randomdata.Number(0, 100))
|
||||
// }
|
||||
|
||||
avl := New(utils.IntComparator)
|
||||
for _, v := range l {
|
||||
avl.Put(v)
|
||||
}
|
||||
}
|
||||
|
||||
// t.Error(avl.debugString(), avl.TraversalBreadth(), "\n", "-----------")
|
||||
// tree := New(compare.Int)
|
||||
// for _, v := range l {
|
||||
// tree.Put(v)
|
||||
// }
|
||||
// }
|
||||
|
||||
// t.Error(tree.debugString(), tree.TraversalBreadth(), "\n", "-----------")
|
||||
}
|
||||
|
||||
func TestPutComparatorRandom(t *testing.T) {
|
||||
|
||||
for n := 0; n < 300000; n++ {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
godsavl := avltree.NewWithIntComparator()
|
||||
|
||||
content := ""
|
||||
@@ -278,14 +275,14 @@ func TestPutComparatorRandom(t *testing.T) {
|
||||
if _, ok := m[v]; !ok {
|
||||
m[v] = v
|
||||
content += spew.Sprint(v) + ","
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
godsavl.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
||||
if avl.String() != godsavl.String() {
|
||||
if tree.String() != godsavl.String() {
|
||||
t.Error(godsavl.String())
|
||||
t.Error(avl.debugString())
|
||||
t.Error(tree.debugString())
|
||||
t.Error(content, n)
|
||||
break
|
||||
}
|
||||
@@ -293,9 +290,9 @@ func TestPutComparatorRandom(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
result := `
|
||||
@@ -311,21 +308,21 @@ func TestGet(t *testing.T) {
|
||||
└── 1562
|
||||
`
|
||||
|
||||
s1 := avl.String()
|
||||
s1 := tree.String()
|
||||
s2 := "AVLTree" + result
|
||||
if s1 != s2 {
|
||||
t.Error(s1, s2)
|
||||
}
|
||||
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
v, ok := avl.Get(v)
|
||||
v, ok := tree.Get(v)
|
||||
if !ok {
|
||||
t.Error("the val not found ", v)
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := avl.Get(10000); ok {
|
||||
t.Error("the val(1000) is not in tree, but is found", v)
|
||||
if v, ok := tree.Get(10000); ok {
|
||||
t.Error("the val(10000) is not in tree, but is found", v)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -334,7 +331,7 @@ func TestRemoveAll(t *testing.T) {
|
||||
|
||||
ALL:
|
||||
for c := 0; c < 5000; c++ {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
gods := avltree.NewWithIntComparator()
|
||||
var l []int
|
||||
m := make(map[int]int)
|
||||
@@ -344,18 +341,18 @@ ALL:
|
||||
if _, ok := m[v]; !ok {
|
||||
m[v] = v
|
||||
l = append(l, v)
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
gods.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
avl.Remove(l[i])
|
||||
tree.Remove(l[i])
|
||||
gods.Remove(l[i])
|
||||
s1 := spew.Sprint(avl.Values())
|
||||
s1 := spew.Sprint(tree.Values())
|
||||
s2 := spew.Sprint(gods.Values())
|
||||
if s1 != s2 {
|
||||
t.Error("avl remove error", "avlsize = ", avl.Size())
|
||||
t.Error("avl remove error", "avlsize = ", tree.Size())
|
||||
t.Error(s1)
|
||||
t.Error(s2)
|
||||
break ALL
|
||||
@@ -369,7 +366,7 @@ func TestRemove(t *testing.T) {
|
||||
|
||||
ALL:
|
||||
for N := 0; N < 500000; N++ {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
gods := avltree.NewWithIntComparator()
|
||||
|
||||
var l []int
|
||||
@@ -380,25 +377,25 @@ ALL:
|
||||
if _, ok := m[v]; !ok {
|
||||
l = append(l, v)
|
||||
m[v] = v
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
gods.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
||||
src1 := avl.String()
|
||||
src1 := tree.String()
|
||||
src2 := gods.String()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
avl.Remove(l[i])
|
||||
tree.Remove(l[i])
|
||||
gods.Remove(l[i])
|
||||
if spew.Sprint(gods.Values()) != spew.Sprint(avl.Values()) && avl.size != 0 {
|
||||
// if gods.String() != avl.String() && gods.Size() != 0 && avl.size != 0 {
|
||||
if spew.Sprint(gods.Values()) != spew.Sprint(tree.Values()) && tree.size != 0 {
|
||||
// if gods.String() != tree.String() && gods.Size() != 0 && tree.size != 0 {
|
||||
t.Error(src1)
|
||||
t.Error(src2)
|
||||
t.Error(avl.debugString())
|
||||
t.Error(tree.debugString())
|
||||
t.Error(gods.String())
|
||||
t.Error(l[i])
|
||||
// t.Error(avl.TraversalDepth(-1))
|
||||
// t.Error(tree.TraversalDepth(-1))
|
||||
// t.Error(gods.Values())
|
||||
break ALL
|
||||
}
|
||||
@@ -407,7 +404,7 @@ ALL:
|
||||
}
|
||||
|
||||
func BenchmarkIterator(b *testing.B) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
|
||||
@@ -435,7 +432,7 @@ func BenchmarkIterator(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkRemove(b *testing.B) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
|
||||
@@ -490,7 +487,7 @@ func BenchmarkGodsRBRemove(b *testing.B) {
|
||||
|
||||
func BenchmarkGet(b *testing.B) {
|
||||
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
@@ -498,7 +495,7 @@ func BenchmarkGet(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
avl.Get(l[i])
|
||||
tree.Get(l[i])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -538,12 +535,12 @@ func BenchmarkPut(b *testing.B) {
|
||||
execCount := 50
|
||||
b.N = len(l) * execCount
|
||||
for i := 0; i < execCount; i++ {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
for _, v := range l {
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
}
|
||||
}
|
||||
// b.Log(avl.count)
|
||||
// b.Log(tree.count)
|
||||
}
|
||||
|
||||
func BenchmarkGodsRBPut(b *testing.B) {
|
||||
|
||||
Reference in New Issue
Block a user