From f73f6faaf578983affadf33c61d9a624ac3cfacd Mon Sep 17 00:00:00 2001 From: huangsimin Date: Tue, 9 Apr 2019 17:33:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- avlkeydup/avlkeydup_test.go | 127 +++++++----------------------------- 1 file changed, 25 insertions(+), 102 deletions(-) diff --git a/avlkeydup/avlkeydup_test.go b/avlkeydup/avlkeydup_test.go index 4af3c98..41ed0e2 100644 --- a/avlkeydup/avlkeydup_test.go +++ b/avlkeydup/avlkeydup_test.go @@ -12,6 +12,8 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/emirpasic/gods/trees/avltree" "github.com/emirpasic/gods/trees/redblacktree" + + "github.com/emirpasic/gods/utils" ) func loadTestData() []int { @@ -25,62 +27,6 @@ func loadTestData() []int { return l } -func TestIterator(t *testing.T) { - tree := New(compare.Int) - for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} { - // t.Error(v) - tree.Put(v, v) - - } - // ` AVLTree - // │ ┌── 30 - // │ │ └── 21 - // │ ┌── 20 - // │ │ └── 15 - // └── 14 - // │ ┌── 7 - // │ ┌── 7 - // │ │ └── 6 - // └── 5 - // │ ┌── 4 - // │ │ └── 3 - // └── 2 - // └── 1` - - iter := tree.Iterator() // root start point - - l := []int{14, 15, 20, 21, 30} - - for i := 0; iter.Next(); i++ { - if iter.Value().(int) != l[i] { - t.Error("iter Next error", iter.Value(), l[i]) - } - } - - iter.Next() - if iter.Value().(int) != 30 { - t.Error("Next == false", iter.Value(), iter.Next(), iter.Value()) - } - - l = []int{21, 20, 15, 14, 7, 7, 6, 5, 4, 3, 2, 1} - for i := 0; iter.Prev(); i++ { // cur is 30 next is 21 - if iter.Value().(int) != l[i] { - t.Error(iter.Value()) - } - } - - if iter.Prev() != false { - t.Error("Prev is error, cur is tail, val = 1 Prev return false") - } - if iter.Value().(int) != 1 { // cur is 1 - t.Error("next == false", iter.Value(), iter.Prev(), iter.Value()) - } - - if iter.Next() != true && iter.Value().(int) != 2 { - t.Error("next to prev is error") - } -} - func TestGetRange(t *testing.T) { tree := New(compare.Int) for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} { @@ -147,9 +93,9 @@ func TestGetAround(t *testing.T) { var Result string Result = spew.Sprint(tree.GetAround(14)) - if Result != "[7 14 14]" { + if Result != "[7 14 15]" { t.Error(tree.Values()) - t.Error("14 is root, tree.GetAround(14)) is error", Result) + t.Error("17 is root, tree.GetAround(14)) is error", Result) t.Error(tree.debugString()) } @@ -168,7 +114,7 @@ func TestGetAround(t *testing.T) { } Result = spew.Sprint(tree.GetAround(40)) - if Result != "[30 40 40]" { + if Result != "[30 40 50]" { t.Error(tree.Values()) t.Error("tree.GetAround(40)) is error", Result) t.Error(tree.debugString()) @@ -213,16 +159,6 @@ 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) - // 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} - - // tree := New(compare.Int) - // for i := 0; i < 10; i++ { - // tree.Put(randomdata.Number(0, 100)) - // } - // t.Error(tree.debugString()) - // t.Error(tree.debugString(), tree.TraversalBreadth(), "\n", "-----------") } @@ -294,13 +230,13 @@ func TestGet(t *testing.T) { func TestRemoveAll(t *testing.T) { ALL: - for c := 0; c < 50000; c++ { + for c := 0; c < 5000; c++ { tree := New(compare.Int) gods := avltree.NewWithIntComparator() var l []int m := make(map[int]int) - for i := 0; len(l) < 50; i++ { + for i := 0; len(l) < 100; i++ { v := randomdata.Number(0, 100000) if _, ok := m[v]; !ok { m[v] = v @@ -310,7 +246,7 @@ ALL: } } - for i := 0; i < 50; i++ { + for i := 0; i < 100; i++ { tree.Remove(l[i]) gods.Remove(l[i]) s1 := spew.Sprint(tree.Values()) @@ -368,7 +304,7 @@ ALL: } func BenchmarkIterator(b *testing.B) { - tree := New(compare.Int) + tree := New(utils.IntComparator) l := loadTestData() @@ -396,19 +332,20 @@ func BenchmarkIterator(b *testing.B) { } func BenchmarkRemove(b *testing.B) { - tree := New(compare.Int) + tree := New(utils.IntComparator) l := loadTestData() - b.N = len(l) for _, v := range l { tree.Put(v, v) } + ll := tree.Values() + b.N = len(ll) b.ResetTimer() b.StartTimer() - for i := 0; i < len(l); i++ { + for i := 0; i < len(ll); i++ { tree.Remove(l[i]) } } @@ -423,10 +360,12 @@ func BenchmarkGodsRemove(b *testing.B) { tree.Put(v, v) } + ll := tree.Values() + b.N = len(ll) b.ResetTimer() b.StartTimer() - for i := 0; i < len(l); i++ { + for i := 0; i < len(ll); i++ { tree.Remove(l[i]) } } @@ -441,10 +380,13 @@ func BenchmarkGodsRBRemove(b *testing.B) { tree.Put(v, v) } + ll := tree.Values() + b.N = len(ll) + b.ResetTimer() b.StartTimer() - for i := 0; i < len(l); i++ { + for i := 0; i < len(ll); i++ { tree.Remove(l[i]) } } @@ -464,7 +406,6 @@ func BenchmarkGet(b *testing.B) { execCount := 10 b.N = len(l) * execCount - for i := 0; i < execCount; i++ { for _, v := range l { tree.Get(v) @@ -477,20 +418,11 @@ func BenchmarkGodsRBGet(b *testing.B) { l := loadTestData() b.N = len(l) - for i := 0; i < b.N; i++ { - tree.Put(l[i], l[i]) - } b.ResetTimer() b.StartTimer() - - execCount := 10 - b.N = len(l) * execCount - - for i := 0; i < execCount; i++ { - for _, v := range l { - tree.Get(v) - } + for i := 0; i < b.N; i++ { + tree.Get(l[i]) } } @@ -499,20 +431,11 @@ func BenchmarkGodsAvlGet(b *testing.B) { l := loadTestData() b.N = len(l) - for i := 0; i < b.N; i++ { - tree.Put(l[i], l[i]) - } b.ResetTimer() b.StartTimer() - - execCount := 10 - b.N = len(l) * execCount - - for i := 0; i < execCount; i++ { - for _, v := range l { - tree.Get(v) - } + for i := 0; i < b.N; i++ { + tree.Get(l[i]) } } @@ -523,7 +446,7 @@ func BenchmarkPut(b *testing.B) { b.ResetTimer() b.StartTimer() - execCount := 10 + execCount := 1000 b.N = len(l) * execCount for i := 0; i < execCount; i++ { tree := New(compare.Int)