2019-03-22 21:01:47 +00:00
|
|
|
package vbtkey
|
2019-03-15 11:58:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2019-03-22 21:01:47 +00:00
|
|
|
"474420502.top/eson/structure/compare"
|
2019-03-20 20:39:25 +00:00
|
|
|
"github.com/huandu/skiplist"
|
|
|
|
|
2019-03-15 11:58:05 +00:00
|
|
|
"github.com/Pallinder/go-randomdata"
|
2019-03-18 17:52:34 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-03-15 11:58:05 +00:00
|
|
|
"github.com/emirpasic/gods/trees/avltree"
|
|
|
|
"github.com/emirpasic/gods/trees/redblacktree"
|
|
|
|
)
|
|
|
|
|
2019-03-22 21:01:47 +00:00
|
|
|
const CompareSize = 1000000
|
2019-03-18 09:54:08 +00:00
|
|
|
const NumberMax = 50000000
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-28 16:50:55 +00:00
|
|
|
func TestSave(t *testing.T) {
|
2019-03-15 11:58:05 +00:00
|
|
|
|
|
|
|
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
var l []int
|
2019-03-16 17:41:07 +00:00
|
|
|
// for i := 0; len(l) < 1000; i++ {
|
|
|
|
// v := randomdata.Number(0, 65535)
|
|
|
|
// l = append(l, v)
|
|
|
|
// }
|
|
|
|
|
|
|
|
//m := make(map[int]int)
|
2019-03-22 21:01:47 +00:00
|
|
|
for i := 0; len(l) < CompareSize; i++ {
|
2019-03-15 11:58:05 +00:00
|
|
|
v := randomdata.Number(0, NumberMax)
|
2019-03-16 17:41:07 +00:00
|
|
|
// if _, ok := m[v]; !ok {
|
|
|
|
// m[v] = v
|
|
|
|
l = append(l, v)
|
|
|
|
// }
|
2019-03-15 11:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var result bytes.Buffer
|
|
|
|
encoder := gob.NewEncoder(&result)
|
|
|
|
encoder.Encode(l)
|
|
|
|
lbytes := result.Bytes()
|
|
|
|
f.Write(lbytes)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 07:27:08 +00:00
|
|
|
func TestIndexRange(t *testing.T) {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-20 11:20:06 +00:00
|
|
|
l := []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15}
|
|
|
|
for _, v := range l {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-20 11:20:06 +00:00
|
|
|
}
|
|
|
|
// [3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50]
|
|
|
|
// t.Error(tree.Values(), tree.Size())
|
|
|
|
|
|
|
|
var result string
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(0, 5))
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[3 7 14 14 14 15] true" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(2, 5))
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[14 14 14 15] true" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(10, 100))
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[30 40 40 40 40 50] false" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(15, 0)) // size = 16, index max = 15
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] true" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(16, 0)) // size = 16, index max = 15
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] false" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(5, 1)) // size = 16, index max = 15
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[15 14 14 14 7] true" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(-1, -5)) // size = 16, index max = 15
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[50 40 40 40 40] true" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(-1, -16)) // size = 16, index max = 0 - 15 (-1,-16)
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] true" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(-1, -17)) // size = 16, index max = 0 - 15 (-1,-16)
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] false" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
result = spew.Sprint(tree.IndexRange(-5, -1)) // size = 16, index max = 0 - 15 (-1,-16)
|
2019-03-20 11:20:06 +00:00
|
|
|
if result != "[40 40 40 40 50] true" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
func TestGetAround(t *testing.T) {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-19 11:15:54 +00:00
|
|
|
for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15} {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-19 11:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var Result string
|
|
|
|
|
2019-03-21 07:27:08 +00:00
|
|
|
Result = spew.Sprint(tree.GetAround(17))
|
|
|
|
if Result != "[16 17 20]" {
|
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("17 is root, 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))
|
|
|
|
if Result != "[30 40 40]" {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = spew.Sprint(tree.GetAround(18))
|
2019-03-22 10:50:22 +00:00
|
|
|
if Result != "[17 <nil> 20]" {
|
2019-03-21 07:27:08 +00:00
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("18 is not in list, tree.GetAround(18)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = spew.Sprint(tree.GetAround(5))
|
2019-03-22 10:50:22 +00:00
|
|
|
if Result != "[3 <nil> 7]" {
|
2019-03-21 07:27:08 +00:00
|
|
|
t.Error(tree.Values())
|
|
|
|
t.Error("5 is not in list, tree.GetAround(5)) is error", Result)
|
|
|
|
t.Error(tree.debugString())
|
2019-03-19 11:15:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +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-19 11:15:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 07:27:08 +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-19 11:15:54 +00:00
|
|
|
}
|
2019-03-21 07:27:08 +00:00
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
|
|
|
// // for test error case
|
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
func TestPutComparatorRandom(t *testing.T) {
|
|
|
|
|
|
|
|
for n := 0; n < 300000; n++ {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-19 11:15:54 +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-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-19 11:15:54 +00:00
|
|
|
godsavl.Put(v, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 10:50:35 +00:00
|
|
|
s1 := spew.Sprint(tree.Values())
|
2019-03-19 11:15:54 +00:00
|
|
|
s2 := spew.Sprint(godsavl.Values())
|
|
|
|
|
|
|
|
if s1 != s2 {
|
|
|
|
t.Error(godsavl.String())
|
2019-03-21 10:50:35 +00:00
|
|
|
t.Error(tree.debugString())
|
2019-03-19 11:15:54 +00:00
|
|
|
t.Error(content, n)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-15 11:58:05 +00:00
|
|
|
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-15 11:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
2019-03-21 10:50:35 +00:00
|
|
|
v, ok := tree.Get(v)
|
2019-03-15 11:58:05 +00:00
|
|
|
if !ok {
|
|
|
|
t.Error("the val not found ", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 10:50:35 +00:00
|
|
|
if v, ok := tree.Get(10000); ok {
|
2019-03-15 11:58:05 +00:00
|
|
|
t.Error("the val(1000) is not in tree, but is found", v)
|
|
|
|
}
|
2019-03-20 11:20:06 +00:00
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-22 10:00:21 +00:00
|
|
|
func TestGetRange(t *testing.T) {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-22 10:00:21 +00:00
|
|
|
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-22 10:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// t.Error(tree.debugString())
|
|
|
|
// t.Error(tree.getArountNode(20))
|
|
|
|
// t.Error(tree.Values())
|
|
|
|
|
|
|
|
result := tree.GetRange(0, 20)
|
|
|
|
if spew.Sprint(result) != "[1 2 5 6 8 10 13 17]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tree.GetRange(-5, -1)
|
|
|
|
if spew.Sprint(result) != "[]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tree.GetRange(7, 20)
|
|
|
|
if spew.Sprint(result) != "[8 10 13 17]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tree.GetRange(30, 40)
|
|
|
|
if spew.Sprint(result) != "[30 40]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tree.GetRange(30, 60)
|
|
|
|
if spew.Sprint(result) != "[30 40]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tree.GetRange(40, 40)
|
|
|
|
if spew.Sprint(result) != "[40]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tree.GetRange(50, 60)
|
|
|
|
if spew.Sprint(result) != "[]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tree.GetRange(50, 1)
|
|
|
|
if spew.Sprint(result) != "[40 30 17 13 10 8 6 5 2 1]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = tree.GetRange(30, 20)
|
|
|
|
if spew.Sprint(result) != "[30]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
func TestTravalsal(t *testing.T) {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-22 10:50:22 +00:00
|
|
|
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
|
|
|
tree.Put(v, v)
|
2019-03-20 11:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
var result []interface{}
|
|
|
|
tree.Traversal(func(v interface{}) bool {
|
|
|
|
result = append(result, v)
|
|
|
|
i++
|
|
|
|
if i >= 10 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2019-03-22 10:50:22 +00:00
|
|
|
|
|
|
|
if spew.Sprint(result) != "[1 2 5 6 8 10 13 17 30 40]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-03-15 11:58:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
func TestRemoveAll(t *testing.T) {
|
|
|
|
ALL:
|
|
|
|
for c := 0; c < 5000; c++ {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-18 17:52:34 +00:00
|
|
|
gods := avltree.NewWithIntComparator()
|
|
|
|
var l []int
|
|
|
|
m := make(map[int]int)
|
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
for i := 0; len(l) < 50; i++ {
|
2019-03-18 17:52:34 +00:00
|
|
|
v := randomdata.Number(0, 100000)
|
|
|
|
if _, ok := m[v]; !ok {
|
|
|
|
m[v] = v
|
|
|
|
l = append(l, v)
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-18 17:52:34 +00:00
|
|
|
gods.Put(v, v)
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
for i := 0; i < 50; i++ {
|
2019-03-19 11:15:54 +00:00
|
|
|
|
2019-03-21 10:50:35 +00:00
|
|
|
tree.Remove(l[i])
|
2019-03-18 17:52:34 +00:00
|
|
|
gods.Remove(l[i])
|
2019-03-19 11:15:54 +00:00
|
|
|
|
2019-03-21 10:50:35 +00:00
|
|
|
s1 := spew.Sprint(tree.Values())
|
2019-03-18 17:52:34 +00:00
|
|
|
s2 := spew.Sprint(gods.Values())
|
|
|
|
if s1 != s2 {
|
2019-03-21 10:50:35 +00:00
|
|
|
t.Error("avl remove error", "avlsize = ", tree.Size())
|
|
|
|
t.Error(tree.root, i, l[i])
|
2019-03-18 17:52:34 +00:00
|
|
|
t.Error(s1)
|
|
|
|
t.Error(s2)
|
|
|
|
break ALL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
func TestRemove(t *testing.T) {
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
ALL:
|
2019-03-21 10:50:35 +00:00
|
|
|
for N := 0; N < 5000; N++ {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-18 17:52:34 +00:00
|
|
|
gods := avltree.NewWithIntComparator()
|
|
|
|
|
|
|
|
var l []int
|
|
|
|
m := make(map[int]int)
|
|
|
|
|
2019-03-21 10:50:35 +00:00
|
|
|
for i := 0; len(l) < 20; i++ {
|
2019-03-18 17:52:34 +00:00
|
|
|
v := randomdata.Number(0, 100)
|
|
|
|
if _, ok := m[v]; !ok {
|
|
|
|
l = append(l, v)
|
|
|
|
m[v] = v
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-18 17:52:34 +00:00
|
|
|
gods.Put(v, v)
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-21 10:50:35 +00:00
|
|
|
src1 := tree.String()
|
2019-03-18 17:52:34 +00:00
|
|
|
src2 := gods.String()
|
|
|
|
|
2019-03-21 10:50:35 +00:00
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
tree.Remove(l[i])
|
2019-03-18 17:52:34 +00:00
|
|
|
gods.Remove(l[i])
|
2019-03-21 10:50:35 +00:00
|
|
|
if tree.root != nil && spew.Sprint(gods.Values()) != spew.Sprint(tree.Values()) {
|
2019-03-18 17:52:34 +00:00
|
|
|
t.Error(src1)
|
|
|
|
t.Error(src2)
|
2019-03-21 10:50:35 +00:00
|
|
|
t.Error(tree.debugString())
|
2019-03-18 17:52:34 +00:00
|
|
|
t.Error(gods.String())
|
|
|
|
t.Error(l[i])
|
|
|
|
break ALL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 20:39:25 +00:00
|
|
|
func BenchmarkSkipListGet(b *testing.B) {
|
|
|
|
sl := skiplist.New(skiplist.Int)
|
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
|
|
|
|
|
|
|
for _, v := range l {
|
|
|
|
sl.Set(v, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
execCount := 5
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
for _, v := range l {
|
|
|
|
e := sl.Get(v)
|
|
|
|
var result [50]interface{}
|
|
|
|
for i := 0; i < 50 && e != nil; i++ {
|
|
|
|
result[i] = e.Value
|
|
|
|
e = e.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGetRange(b *testing.B) {
|
2019-03-22 10:00:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkIndexRange(b *testing.B) {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-20 20:39:25 +00:00
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
|
|
|
|
|
|
|
for _, v := range l {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-20 20:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
execCount := 5
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
for range l {
|
2019-03-21 07:27:08 +00:00
|
|
|
tree.IndexRange(i, i+49)
|
2019-03-20 20:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSkipListSet(b *testing.B) {
|
2019-03-22 10:00:21 +00:00
|
|
|
|
2019-03-20 20:39:25 +00:00
|
|
|
l := loadTestData()
|
2019-03-22 10:00:21 +00:00
|
|
|
execCount := 1
|
2019-03-20 20:39:25 +00:00
|
|
|
b.N = len(l) * execCount
|
|
|
|
for i := 0; i < execCount; i++ {
|
2019-03-22 10:00:21 +00:00
|
|
|
sl := skiplist.New(skiplist.Int)
|
2019-03-20 20:39:25 +00:00
|
|
|
for _, v := range l {
|
|
|
|
sl.Set(v, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
func BenchmarkIterator(b *testing.B) {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-19 11:15:54 +00:00
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
|
|
|
|
|
|
|
for _, v := range l {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-19 11:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
iter := tree.Iterator()
|
2019-03-20 11:20:06 +00:00
|
|
|
b.N = 0
|
2019-03-19 11:15:54 +00:00
|
|
|
for iter.Next() {
|
2019-03-20 11:20:06 +00:00
|
|
|
b.N++
|
2019-03-19 11:15:54 +00:00
|
|
|
}
|
|
|
|
for iter.Prev() {
|
2019-03-20 11:20:06 +00:00
|
|
|
b.N++
|
2019-03-19 11:15:54 +00:00
|
|
|
}
|
|
|
|
for iter.Next() {
|
2019-03-20 11:20:06 +00:00
|
|
|
b.N++
|
2019-03-19 11:15:54 +00:00
|
|
|
}
|
2019-03-20 11:20:06 +00:00
|
|
|
b.Log(b.N, len(l))
|
2019-03-19 11:15:54 +00:00
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
func BenchmarkRemove(b *testing.B) {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
l := loadTestData()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
b.N = len(l)
|
|
|
|
for _, v := range l {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-18 17:52:34 +00:00
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
for i := 0; i < len(l); i++ {
|
|
|
|
tree.Remove(l[i])
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
func BenchmarkGodsRemove(b *testing.B) {
|
|
|
|
tree := avltree.NewWithIntComparator()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
l := loadTestData()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
b.N = len(l)
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-18 17:52:34 +00:00
|
|
|
for i := 0; i < len(l); i++ {
|
|
|
|
tree.Remove(l[i])
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
func BenchmarkGodsRBRemove(b *testing.B) {
|
|
|
|
tree := redblacktree.NewWithIntComparator()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
l := loadTestData()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
b.N = len(l)
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
for i := 0; i < len(l); i++ {
|
|
|
|
tree.Remove(l[i])
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
|
|
|
func BenchmarkGet(b *testing.B) {
|
|
|
|
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-15 11:58:05 +00:00
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
2019-03-20 11:20:06 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(l[i], i)
|
2019-03-20 11:20:06 +00:00
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-03-20 11:20:06 +00:00
|
|
|
|
2019-03-22 11:04:11 +00:00
|
|
|
execCount := 10
|
2019-03-20 11:20:06 +00:00
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Get(v)
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
func BenchmarkGodsRBGet(b *testing.B) {
|
|
|
|
tree := redblacktree.NewWithIntComparator()
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tree.Put(l[i], i)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
2019-03-22 11:04:11 +00:00
|
|
|
execCount := 10
|
2019-03-20 11:20:06 +00:00
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Get(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
func BenchmarkGodsAvlGet(b *testing.B) {
|
|
|
|
tree := avltree.NewWithIntComparator()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
l := loadTestData()
|
|
|
|
b.N = len(l)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tree.Put(l[i], i)
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-22 11:04:11 +00:00
|
|
|
execCount := 10
|
2019-03-20 11:20:06 +00:00
|
|
|
b.N = len(l) * execCount
|
2019-03-15 11:58:05 +00:00
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Get(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
|
|
|
|
func BenchmarkPut(b *testing.B) {
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
2019-03-24 14:21:25 +00:00
|
|
|
execCount := 50
|
2019-03-18 09:54:08 +00:00
|
|
|
b.N = len(l) * execCount
|
|
|
|
for i := 0; i < execCount; i++ {
|
2019-03-22 21:01:47 +00:00
|
|
|
tree := New(compare.Int)
|
2019-03-18 09:54:08 +00:00
|
|
|
for _, v := range l {
|
2019-03-22 10:50:22 +00:00
|
|
|
tree.Put(v, v)
|
2019-03-18 09:54:08 +00:00
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutStable(t *testing.T) {
|
|
|
|
|
2019-03-20 11:20:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:58:05 +00:00
|
|
|
func BenchmarkGodsRBPut(b *testing.B) {
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
2019-04-04 10:36:41 +00:00
|
|
|
execCount := 50
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
tree := redblacktree.NewWithIntComparator()
|
|
|
|
for _, v := range l {
|
|
|
|
tree.Put(v, v)
|
|
|
|
}
|
2019-03-15 11:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|