structure/avldup/avldup_test.go

497 lines
8.9 KiB
Go

package avldup
import (
"bytes"
"encoding/gob"
"io/ioutil"
"log"
"os"
"testing"
"github.com/Pallinder/go-randomdata"
"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 = 500000
const NumberMax = 60000000
func TestSave(t *testing.T) {
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
m := make(map[int]int)
for i := 0; len(l) < CompartorSize; i++ {
v := randomdata.Number(0, NumberMax)
if _, ok := m[v]; !ok {
m[v] = v
l = append(l, v)
}
}
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
}
func TestDupKey(t *testing.T) {
tree1 := New(utils.IntComparator)
tree2 := avltree.NewWithIntComparator()
for i := 0; i < CompartorSize/1000; i++ {
v := randomdata.Number(0, NumberMax)
tree1.Put(v)
tree2.Put(v, v)
}
tree1.Put(500)
tree2.Put(500, 500)
tree1.Put(500)
tree2.Put(500, 500)
if tree1.Size() == tree2.Size() {
t.Error("tree size is equal")
}
if tree1.String() == tree2.String() {
t.Error(tree1.String())
}
}
func TestIterator(t *testing.T) {
avl := New(utils.IntComparator)
for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} {
// t.Error(v)
avl.Put(v)
}
// ` AVLTree
// │ ┌── 30
// │ │ └── 21
// │ ┌── 20
// │ │ └── 15
// └── 14
// │ ┌── 7
// │ ┌── 7
// │ │ └── 6
// └── 5
// │ ┌── 4
// │ │ └── 3
// └── 2
// └── 1`
iter := avl.Iterator() // root start point
l := []int{14, 15, 20, 21, 30}
for i := 0; iter.Prev(); i++ {
if iter.Value().(int) != l[i] {
t.Error("iter prev error", iter.Value(), l[i])
}
}
iter.Prev()
if iter.Value().(int) != 30 {
t.Error("prev == false", iter.Value(), iter.Prev(), iter.Value())
}
l = []int{21, 20, 15, 14, 7, 7, 6, 5, 4, 3, 2, 1}
for i := 0; iter.Next(); i++ { // cur is 30 next is 21
if iter.Value().(int) != l[i] {
t.Error(iter.Value())
}
}
if iter.Next() != false {
t.Error("Next is error, cur is tail, val = 1 Next return false")
}
if iter.Value().(int) != 1 { // cur is 1
t.Error("next == false", iter.Value(), iter.Next(), iter.Value())
}
if iter.Prev() != true && iter.Value().(int) != 2 {
t.Error("next to prev is error")
}
}
func TestGetAround(t *testing.T) {
avl := New(utils.IntComparator)
for _, v := range []int{7, 14, 15, 20, 30, 21, 40, 40, 50, 3, 40, 40, 40} {
avl.Put(v)
}
if spew.Sprint(avl.GetAround(30)) != "[40 30 21]" {
t.Error("avl.GetAround(40)) is error", spew.Sprint(avl.GetAround(30)))
}
if spew.Sprint(avl.GetAround(40)) != "[40 40 30]" {
t.Error("avl.GetAround(40)) is error", spew.Sprint(avl.GetAround(50)))
}
if spew.Sprint(avl.GetAround(50)) != "[<nil> 50 40]" {
t.Error("avl.GetAround(40)) is error", spew.Sprint(avl.GetAround(50)))
}
}
// for test error case
func TestPutStable(t *testing.T) {
// avl := New(utils.IntComparator)
// gods := avltree.NewWithIntComparator()
// // 44908, 34985, 62991, 4201, 27210, 30707
// for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
// t.Error(v)
// t.Error(avl.debugString())
// avl.Put(v)
// gods.Put(v, v)
// t.Error(avl.debugString())
// t.Error(gods.String())
// }
}
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) + ","
// t.Error(v)
avl.Put(v)
// t.Error(avl.String())
godsavl.Put(v, v)
}
}
if avl.String() != godsavl.String() {
t.Error(godsavl.String())
t.Error(avl.debugString())
t.Error(content, n)
break
}
}
}
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)
}
result := `
│ ┌── 57092
│ ┌── 39016
│ │ └── 36513
│ ┌── 27897
│ │ │ ┌── 23202
│ │ └── 22574
└── 7666
│ ┌── 3055
└── 2383
└── 1562
`
s1 := avl.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)
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)
}
}
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)
for i := 0; len(l) < 100; i++ {
v := randomdata.Number(0, 100000)
if _, ok := m[v]; !ok {
m[v] = v
l = append(l, v)
avl.Put(v)
gods.Put(v, v)
}
}
for i := 0; i < 100; i++ {
avl.Remove(l[i])
gods.Remove(l[i])
s1 := spew.Sprint(avl.TraversalDepth(-1))
s2 := spew.Sprint(gods.Values())
if s1 != s2 {
t.Error("avl remove error", "avlsize = ", avl.Size())
t.Error(s1)
t.Error(s2)
break ALL
}
}
}
}
func TestRemove(t *testing.T) {
ALL:
for N := 0; N < 500000; 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)
}
}
src1 := avl.String()
src2 := gods.String()
for i := 0; i < 10; i++ {
avl.Remove(l[i])
gods.Remove(l[i])
if spew.Sprint(gods.Values()) != spew.Sprint(avl.TraversalDepth(-1)) && avl.size != 0 {
// 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
}
}
}
}
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() {
}
}
func BenchmarkGodsIterator(b *testing.B) {
tree := avltree.NewWithIntComparator()
l := loadTestData()
b.N = len(l)
for _, v := range l {
tree.Put(v, v)
}
b.ResetTimer()
b.StartTimer()
iter := tree.Iterator()
for iter.Next() {
}
for iter.Prev() {
}
for iter.Next() {
}
}
func BenchmarkRemove(b *testing.B) {
tree := New(utils.IntComparator)
l := loadTestData()
b.N = len(l)
for _, v := range l {
tree.Put(v)
}
b.ResetTimer()
b.StartTimer()
for i := 0; i < len(l); i++ {
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)
}
b.ResetTimer()
b.StartTimer()
for i := 0; i < len(l); i++ {
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)
}
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) {
avl := New(utils.IntComparator)
l := loadTestData()
b.ResetTimer()
b.StartTimer()
b.N = len(l)
for _, v := range l {
avl.Put(v)
}
}
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)
}
}