2019-03-14 11:14:44 +00:00
|
|
|
package avlkeydup
|
2019-03-14 10:35:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
"474420502.top/eson/structure/compare"
|
2019-03-14 10:35:41 +00:00
|
|
|
"github.com/Pallinder/go-randomdata"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/emirpasic/gods/trees/avltree"
|
|
|
|
"github.com/emirpasic/gods/trees/redblacktree"
|
2019-04-09 09:33:32 +00:00
|
|
|
|
|
|
|
"github.com/emirpasic/gods/utils"
|
2019-03-14 10:35:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func loadTestData() []int {
|
|
|
|
data, err := ioutil.ReadFile("../l.log")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
var l []int
|
|
|
|
decoder := gob.NewDecoder(bytes.NewReader(data))
|
|
|
|
decoder.Decode(&l)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
func TestGetRange(t *testing.T) {
|
|
|
|
tree := New(compare.Int)
|
|
|
|
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
|
|
|
tree.Put(v, v)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
// t.Error(tree.debugString())
|
|
|
|
// t.Error(tree.getArountNode(20))
|
|
|
|
// t.Error(tree.Values())
|
2019-03-14 10:35:41 +00:00
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
result := tree.GetRange(0, 20)
|
|
|
|
if spew.Sprint(result) != "[1 2 5 6 8 10 13 17]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
2019-03-14 10:35:41 +00:00
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
result = tree.GetRange(-5, -1)
|
|
|
|
if spew.Sprint(result) != "[]" {
|
|
|
|
t.Error(result)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
result = tree.GetRange(7, 20)
|
|
|
|
if spew.Sprint(result) != "[8 10 13 17]" {
|
|
|
|
t.Error(result)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
result = tree.GetRange(30, 40)
|
|
|
|
if spew.Sprint(result) != "[30 40]" {
|
|
|
|
t.Error(result)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
result = tree.GetRange(30, 60)
|
|
|
|
if spew.Sprint(result) != "[30 40]" {
|
|
|
|
t.Error(result)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
result = tree.GetRange(40, 40)
|
|
|
|
if spew.Sprint(result) != "[40]" {
|
|
|
|
t.Error(result)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
result = tree.GetRange(50, 60)
|
|
|
|
if spew.Sprint(result) != "[]" {
|
|
|
|
t.Error(result)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
2019-03-24 14:21:25 +00:00
|
|
|
|
|
|
|
result = tree.GetRange(50, 1)
|
|
|
|
if spew.Sprint(result) != "[40 30 17 13 10 8 6 5 2 1]" {
|
|
|
|
t.Error(result)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
result = tree.GetRange(30, 20)
|
|
|
|
if spew.Sprint(result) != "[30]" {
|
|
|
|
t.Error(result)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
2019-03-24 14:21:25 +00:00
|
|
|
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAround(t *testing.T) {
|
2019-03-24 14:21:25 +00:00
|
|
|
tree := New(compare.Int)
|
|
|
|
for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15} {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
var Result string
|
|
|
|
|
|
|
|
Result = spew.Sprint(tree.GetAround(14))
|
2019-04-09 09:33:32 +00:00
|
|
|
if Result != "[7 14 15]" {
|
2019-03-24 14:21:25 +00:00
|
|
|
t.Error(tree.Values())
|
2019-04-09 09:33:32 +00:00
|
|
|
t.Error("17 is root, tree.GetAround(14)) is error", Result)
|
2019-03-24 14:21:25 +00:00
|
|
|
t.Error(tree.debugString())
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = spew.Sprint(tree.GetAround(17))
|
|
|
|
if Result != "[16 17 20]" {
|
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("tree.GetAround(17)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = spew.Sprint(tree.GetAround(3))
|
|
|
|
if Result != "[<nil> 3 7]" {
|
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("tree.GetAround(3)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = spew.Sprint(tree.GetAround(40))
|
2019-04-09 09:33:32 +00:00
|
|
|
if Result != "[30 40 50]" {
|
2019-03-24 14:21:25 +00:00
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("tree.GetAround(40)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = spew.Sprint(tree.GetAround(50))
|
|
|
|
if Result != "[40 50 <nil>]" {
|
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("tree.GetAround(50)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
Result = spew.Sprint(tree.GetAround(18))
|
|
|
|
if Result != "[17 <nil> 20]" {
|
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("18 is not in list, tree.GetAround(18)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
Result = spew.Sprint(tree.GetAround(5))
|
|
|
|
if Result != "[3 <nil> 7]" {
|
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("5 is not in list, tree.GetAround(5)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
Result = spew.Sprint(tree.GetAround(2))
|
|
|
|
if Result != "[<nil> <nil> 3]" {
|
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("2 is not in list, tree.GetAround(2)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
2019-03-24 14:21:25 +00:00
|
|
|
|
|
|
|
Result = spew.Sprint(tree.GetAround(100))
|
|
|
|
if Result != "[50 <nil> <nil>]" {
|
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("50 is not in list, tree.GetAround(50)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// for test error case
|
|
|
|
func TestPutStable(t *testing.T) {
|
2019-03-24 14:21:25 +00:00
|
|
|
// t.Error(tree.debugString(), tree.TraversalBreadth(), "\n", "-----------")
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutComparatorRandom(t *testing.T) {
|
|
|
|
|
|
|
|
for n := 0; n < 300000; n++ {
|
2019-03-24 14:21:25 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-14 10:35:41 +00:00
|
|
|
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) + ","
|
2019-03-24 14:21:25 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-14 10:35:41 +00:00
|
|
|
godsavl.Put(v, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
if tree.String() != godsavl.String() {
|
2019-03-14 10:35:41 +00:00
|
|
|
t.Error(godsavl.String())
|
2019-03-24 14:21:25 +00:00
|
|
|
t.Error(tree.debugString())
|
2019-03-14 10:35:41 +00:00
|
|
|
t.Error(content, n)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2019-03-24 14:21:25 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-14 10:35:41 +00:00
|
|
|
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
2019-03-24 14:21:25 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := `
|
|
|
|
│ ┌── 57092
|
|
|
|
│ ┌── 39016
|
|
|
|
│ │ └── 36513
|
|
|
|
│ ┌── 27897
|
|
|
|
│ │ │ ┌── 23202
|
|
|
|
│ │ └── 22574
|
|
|
|
└── 7666
|
|
|
|
│ ┌── 3055
|
|
|
|
└── 2383
|
|
|
|
└── 1562
|
|
|
|
`
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
s1 := tree.String()
|
2019-03-14 10:35:41 +00:00
|
|
|
s2 := "AVLTree" + result
|
|
|
|
if s1 != s2 {
|
|
|
|
t.Error(s1, s2)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
2019-03-24 14:21:25 +00:00
|
|
|
v, ok := tree.Get(v)
|
2019-03-14 10:35:41 +00:00
|
|
|
if !ok {
|
|
|
|
t.Error("the val not found ", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
if v, ok := tree.Get(10000); ok {
|
|
|
|
t.Error("the val(10000) is not in tree, but is found", v)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveAll(t *testing.T) {
|
|
|
|
|
|
|
|
ALL:
|
2019-04-09 09:33:32 +00:00
|
|
|
for c := 0; c < 5000; c++ {
|
2019-03-24 14:21:25 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-14 10:35:41 +00:00
|
|
|
gods := avltree.NewWithIntComparator()
|
|
|
|
var l []int
|
|
|
|
m := make(map[int]int)
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
for i := 0; len(l) < 100; i++ {
|
2019-03-14 10:35:41 +00:00
|
|
|
v := randomdata.Number(0, 100000)
|
|
|
|
if _, ok := m[v]; !ok {
|
|
|
|
m[v] = v
|
|
|
|
l = append(l, v)
|
2019-03-24 14:21:25 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-14 10:35:41 +00:00
|
|
|
gods.Put(v, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2019-03-24 14:21:25 +00:00
|
|
|
tree.Remove(l[i])
|
2019-03-14 10:35:41 +00:00
|
|
|
gods.Remove(l[i])
|
2019-03-24 14:21:25 +00:00
|
|
|
s1 := spew.Sprint(tree.Values())
|
2019-03-14 10:35:41 +00:00
|
|
|
s2 := spew.Sprint(gods.Values())
|
|
|
|
if s1 != s2 {
|
2019-03-24 14:21:25 +00:00
|
|
|
t.Error("avl remove error", "avlsize = ", tree.Size())
|
2019-03-14 10:35:41 +00:00
|
|
|
t.Error(s1)
|
|
|
|
t.Error(s2)
|
|
|
|
break ALL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 14:21:25 +00:00
|
|
|
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
|
|
|
|
|
|
|
ALL:
|
|
|
|
for N := 0; N < 500000; N++ {
|
2019-03-24 14:21:25 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-14 10:35:41 +00:00
|
|
|
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
|
2019-03-24 14:21:25 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-14 10:35:41 +00:00
|
|
|
gods.Put(v, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
src1 := tree.String()
|
2019-03-14 10:35:41 +00:00
|
|
|
src2 := gods.String()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2019-03-24 14:21:25 +00:00
|
|
|
tree.Remove(l[i])
|
2019-03-14 10:35:41 +00:00
|
|
|
gods.Remove(l[i])
|
2019-03-24 14:21:25 +00:00
|
|
|
if spew.Sprint(gods.Values()) != spew.Sprint(tree.Values()) && tree.size != 0 {
|
|
|
|
// if gods.String() != tree.String() && gods.Size() != 0 && tree.size != 0 {
|
2019-03-14 10:35:41 +00:00
|
|
|
t.Error(src1)
|
|
|
|
t.Error(src2)
|
2019-03-24 14:21:25 +00:00
|
|
|
t.Error(tree.debugString())
|
2019-03-14 10:35:41 +00:00
|
|
|
t.Error(gods.String())
|
|
|
|
t.Error(l[i])
|
2019-03-24 14:21:25 +00:00
|
|
|
// t.Error(tree.TraversalDepth(-1))
|
2019-03-14 10:35:41 +00:00
|
|
|
// t.Error(gods.Values())
|
|
|
|
break ALL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkIterator(b *testing.B) {
|
2019-04-09 09:33:32 +00:00
|
|
|
tree := New(utils.IntComparator)
|
2019-03-14 10:35:41 +00:00
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-03-24 14:21:25 +00:00
|
|
|
b.N = 0
|
2019-03-14 10:35:41 +00:00
|
|
|
iter := tree.Iterator()
|
|
|
|
for iter.Next() {
|
2019-03-24 14:21:25 +00:00
|
|
|
b.N++
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
for iter.Prev() {
|
2019-03-24 14:21:25 +00:00
|
|
|
b.N++
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
for iter.Next() {
|
2019-03-24 14:21:25 +00:00
|
|
|
b.N++
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
for iter.Prev() {
|
2019-03-24 14:21:25 +00:00
|
|
|
b.N++
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
2019-03-24 14:21:25 +00:00
|
|
|
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRemove(b *testing.B) {
|
2019-04-09 09:33:32 +00:00
|
|
|
tree := New(utils.IntComparator)
|
2019-03-14 10:35:41 +00:00
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
ll := tree.Values()
|
|
|
|
b.N = len(ll)
|
2019-03-14 10:35:41 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
for i := 0; i < len(ll); i++ {
|
2019-03-14 10:35:41 +00:00
|
|
|
tree.Remove(l[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsRemove(b *testing.B) {
|
|
|
|
tree := avltree.NewWithIntComparator()
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.N = len(l)
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
ll := tree.Values()
|
|
|
|
b.N = len(ll)
|
2019-03-14 10:35:41 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
for i := 0; i < len(ll); i++ {
|
2019-03-14 10:35:41 +00:00
|
|
|
tree.Remove(l[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsRBRemove(b *testing.B) {
|
|
|
|
tree := redblacktree.NewWithIntComparator()
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.N = len(l)
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
ll := tree.Values()
|
|
|
|
b.N = len(ll)
|
|
|
|
|
2019-03-14 10:35:41 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
for i := 0; i < len(ll); i++ {
|
2019-03-14 10:35:41 +00:00
|
|
|
tree.Remove(l[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGet(b *testing.B) {
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-14 10:35:41 +00:00
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
2019-04-09 09:26:01 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tree.Put(l[i], l[i])
|
|
|
|
}
|
2019-03-14 10:35:41 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-04-09 09:26:01 +00:00
|
|
|
|
|
|
|
execCount := 10
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Get(v)
|
|
|
|
}
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsRBGet(b *testing.B) {
|
|
|
|
tree := redblacktree.NewWithIntComparator()
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-04-09 09:33:32 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tree.Get(l[i])
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsAvlGet(b *testing.B) {
|
|
|
|
tree := avltree.NewWithIntComparator()
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-04-09 09:33:32 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tree.Get(l[i])
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPut(b *testing.B) {
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
2019-04-09 09:33:32 +00:00
|
|
|
execCount := 1000
|
2019-03-24 14:21:25 +00:00
|
|
|
b.N = len(l) * execCount
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
tree := New(compare.Int)
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
2019-03-24 14:21:25 +00:00
|
|
|
// b.Log(tree.count)
|
2019-03-14 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsRBPut(b *testing.B) {
|
|
|
|
tree := redblacktree.NewWithIntComparator()
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
b.N = len(l)
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsPut(b *testing.B) {
|
|
|
|
tree := avltree.NewWithIntComparator()
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
b.N = len(l)
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
|
|
|
}
|