structure/avlindex/avlindex_test.go

404 lines
7.2 KiB
Go
Raw Normal View History

2019-03-15 11:58:05 +00:00
package avlindex
import (
"bytes"
"encoding/gob"
"io/ioutil"
"log"
"os"
"testing"
"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"
"github.com/emirpasic/gods/utils"
)
2019-03-18 17:52:34 +00:00
const CompartorSize = 1000000
2019-03-18 09:54:08 +00:00
const NumberMax = 50000000
2019-03-15 11:58:05 +00:00
2019-03-19 18:36:16 +00:00
func Save(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)
}
//fmt.Println(userBytes)
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-15 11:58:05 +00:00
for i := 0; len(l) < CompartorSize; i++ {
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-19 11:15:54 +00:00
func TestIterator(t *testing.T) {
2019-03-19 18:36:16 +00:00
// avl := New(utils.IntComparator)
// for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 40, 50, 3, 40, 40, 40, 15} {
// avl.Put(v)
// }
// t.Error(avl.Values())
// t.Error(avl.debugString())
2019-03-19 11:15:54 +00:00
}
func TestGetAround(t *testing.T) {
avl := New(utils.IntComparator)
for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15} {
avl.Put(v)
}
var Result string
Result = spew.Sprint(avl.GetAround(3))
if Result != "[7 3 <nil>]" {
t.Error("avl.GetAround(3)) is error", Result)
}
Result = spew.Sprint(avl.GetAround(40))
if Result != "[40 40 30]" {
t.Error("avl.GetAround(40)) is error", Result)
}
Result = spew.Sprint(avl.GetAround(50))
if Result != "[<nil> 50 40]" {
t.Error("avl.GetAround(50)) is error", Result)
}
}
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++ {
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) + ","
avl.Put(v)
godsavl.Put(v, v)
}
}
s1 := spew.Sprint(avl.Values())
s2 := spew.Sprint(godsavl.Values())
if s1 != s2 {
t.Error(godsavl.String())
t.Error(avl.debugString())
t.Error(content, n)
break
}
}
}
2019-03-15 11:58:05 +00:00
func TestGet(t *testing.T) {
avl := New(utils.IntComparator)
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
avl.Put(v)
}
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
v, ok := avl.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)
}
}
2019-03-18 17:52:34 +00:00
func TestRemoveAll(t *testing.T) {
ALL:
for c := 0; c < 5000; c++ {
avl := New(utils.IntComparator)
gods := avltree.NewWithIntComparator()
var l []int
m := make(map[int]int)
2019-03-19 11:15:54 +00:00
for i := 0; len(l) < 10; 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)
avl.Put(v)
gods.Put(v, v)
}
}
2019-03-15 11:58:05 +00:00
2019-03-19 11:15:54 +00:00
for i := 0; i < 10; i++ {
2019-03-18 17:52:34 +00:00
avl.Remove(l[i])
gods.Remove(l[i])
2019-03-19 11:15:54 +00:00
s1 := spew.Sprint(avl.Values())
2019-03-18 17:52:34 +00:00
s2 := spew.Sprint(gods.Values())
if s1 != s2 {
t.Error("avl remove error", "avlsize = ", avl.Size())
2019-03-19 11:15:54 +00:00
t.Error(avl.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:
for N := 0; N < 50000; 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)
}
}
2019-03-15 11:58:05 +00:00
2019-03-18 17:52:34 +00:00
src1 := avl.String()
src2 := gods.String()
for i := 0; i < 10; i++ {
avl.Remove(l[i])
gods.Remove(l[i])
2019-03-19 11:15:54 +00:00
if avl.root != nil && spew.Sprint(gods.Values()) != spew.Sprint(avl.Values()) {
2019-03-18 17:52:34 +00:00
// 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
}
}
}
}
2019-03-15 11:58:05 +00:00
2019-03-19 11:15:54 +00:00
func BenchmarkIterator(b *testing.B) {
tree := New(utils.IntComparator)
l := loadTestData()
b.N = len(l)
for _, v := range l {
tree.Put(v)
}
b.ResetTimer()
b.StartTimer()
iter := tree.Iterator()
for iter.Next() {
}
for iter.Prev() {
}
for iter.Next() {
}
}
2019-03-15 11:58:05 +00:00
2019-03-18 17:52:34 +00:00
func BenchmarkRemove(b *testing.B) {
tree := New(utils.IntComparator)
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)
}
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
// func BenchmarkGodsRBRemove(b *testing.B) {
// tree := redblacktree.NewWithIntComparator()
// l := loadTestData()
// b.N = len(l)
// for _, v := range l {
// tree.Put(v, v)
// }
// b.ResetTimer()
// b.StartTimer()
// for i := 0; i < len(l); i++ {
// tree.Remove(l[i])
// }
// }
func BenchmarkGet(b *testing.B) {
avl := New(utils.IntComparator)
l := loadTestData()
b.N = len(l)
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
avl.Get(l[i])
}
}
// func BenchmarkGodsRBGet(b *testing.B) {
// tree := redblacktree.NewWithIntComparator()
// l := loadTestData()
// b.N = len(l)
// b.ResetTimer()
// b.StartTimer()
// for i := 0; i < b.N; i++ {
// tree.Get(l[i])
// }
// }
// func BenchmarkGodsAvlGet(b *testing.B) {
// tree := avltree.NewWithIntComparator()
// l := loadTestData()
// b.N = len(l)
// b.ResetTimer()
// b.StartTimer()
// for i := 0; i < b.N; i++ {
// tree.Get(l[i])
// }
// }
func BenchmarkPut(b *testing.B) {
l := loadTestData()
b.ResetTimer()
b.StartTimer()
2019-03-18 09:54:08 +00:00
execCount := 50
b.N = len(l) * execCount
for i := 0; i < execCount; i++ {
avl := New(utils.IntComparator)
for _, v := range l {
avl.Put(v)
}
2019-03-15 11:58:05 +00:00
}
}
func TestPutStable(t *testing.T) {
2019-03-18 09:54:08 +00:00
// l := []int{14, 18, 20, 21, 22, 23, 19}
2019-03-19 18:36:16 +00:00
// var l []int
// for i := 0; len(l) < 10; i++ {
// l = append(l, randomdata.Number(0, 65))
// }
2019-03-15 11:58:05 +00:00
2019-03-19 18:36:16 +00:00
// avl := New(utils.IntComparator)
// for _, v := range l {
// avl.Put(v)
// t.Error(avl.debugString(), v)
// }
// t.Error(avl.Values())
// for _, v := range []int{10, 0, 9, 5, -11, -10, -1, -5} {
// t.Error(avl.Index(v))
// }
2019-03-16 17:41:07 +00:00
// t.Error(len(l), avl.debugString(), "\n", "-----------") // 3 6(4)
2019-03-15 11:58:05 +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)
}
}