structure_old/priority_queue/priority_queue_test.go
2019-02-15 18:51:21 +08:00

123 lines
2.0 KiB
Go

package plist
import (
"testing"
"github.com/emirpasic/gods/trees/avltree"
"github.com/emirpasic/gods/utils"
"github.com/Pallinder/go-randomdata"
"github.com/emirpasic/gods/trees/binaryheap"
)
func TestPList(t *testing.T) {
pl := &PriorityList{}
for i := 0; i < 10; i++ {
pl.nodeInsert(nil, randomdata.Number(0, 10))
}
t.Error(pl.String())
cur := pl.head
for i := 0; i < 5; i++ {
cur = cur.next
}
pl.nodeInsert(cur, 11)
t.Error(pl.String())
}
func TestPQ(t *testing.T) {
pq := NewWithIntComparator()
for i := 0; i < 1; i++ {
pq.Push(i)
}
iter := pq.avl.Iterator()
iter.Next()
pl := iter.Value().(*PriorityList)
t.Error(pq.size, pq.avl.Size(), pl.Size(), pl.head == nil, pl.head)
}
func BenchmarkPQ(b *testing.B) {
pq := NewWithIntComparator()
b.N = 1000000
for i := b.N; i > 0; i-- {
pq.Push(i)
}
iter := pq.avl.Iterator()
iter.Next()
pl := iter.Value().(*PriorityList)
b.Log(pq.size, pq.avl.Size(), pl.Size(), pl.head)
b.Log(pl.head.value)
if pl.head.next != nil {
b.Log(pl.head.next.value)
}
// b.Log("all:", pq.avl.Size(), pq.avl.Values())
}
func BenchmarkPList(b *testing.B) {
for i := 0; i < b.N; i++ {
pl := &PriorityList{}
for i2 := 0; i2 < 5; i2++ {
cur := pl.head
for n := 0; n < i2; n++ {
cur = cur.next
}
pl.nodeInsert(cur, i2)
}
}
}
func TestAvl(t *testing.T) {
comparator := func(v1, v2 interface{}) int {
if v1.(int) > v2.(int) {
return 1
} else if v1.(int) < v2.(int) {
return -1
}
return 0
}
avl := avltree.NewWith(comparator)
for _, v := range []int{9, 2, 3, 4, 5, 6, 8, 1} {
avl.Put(v, v)
}
iter := avl.Iterator()
iter.Next()
t.Error(avl.Values(), iter.Value())
f, ok := avl.Floor(10)
if ok {
t.Error("Floor", f)
}
f, ok = avl.Ceiling(10)
if ok {
t.Error("Ceiling", f)
}
}
func TestHeap(t *testing.T) {
heap := binaryheap.NewWithIntComparator()
for i := 0; i < 10; i++ {
heap.Push(randomdata.Number(0, 1000))
}
t.Error(heap.Peek())
t.Error(heap.Values())
utils.Sort(heap.Values(), utils.IntComparator)
}